question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

graphQL query return type does not support custom generic.

See original GitHub issue

Library Version com.expediagroup:graphql-kotlin-spring-server:2.0.0-RC6

Describe the bug i am running a graphQL server. this is my test code

import com.expediagroup.graphql.hooks.SchemaGeneratorHooks
import com.expediagroup.graphql.spring.operations.Query
import graphql.Scalars
import graphql.schema.GraphQLInputObjectField
import graphql.schema.GraphQLInputObjectType
import graphql.schema.GraphQLType
import org.springframework.stereotype.Component
import kotlin.reflect.KType

@Component
class GenericQuery : Query {

    fun getWidgetValue(widget: Widget<Long>): Long {
        return widget.value
    }

}

data class Widget<T>(val value: T)

@Component
class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {

    override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
        Widget::class -> createWidgetGraphQLType(type)
        else -> null
    }

    fun createWidgetGraphQLType(type: KType): GraphQLType {
        return GraphQLInputObjectType
            .newInputObject()
            .name("WidgetInput")
            .field(
                GraphQLInputObjectField.newInputObjectField()
                    .name("value")
                    .type(Scalars.GraphQLString)
                    .build()
            ).build()
    }

}

To Reproduce when i query

query {
    getWidgetValue(
        widget: {
            value: "foo"
        }
    )
}

i got error info:

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class
  at com.expediagroup.graphql.generator.extensions.KParameterExtensionsKt.javaTypeClass(kParameterExtensions.kt:43) ~[graphql-kotlin-schema-generator-2.0.0-RC6.jar:2.0.0-RC6]

Expected behavior i wanna graphQL server return result:

{
  "widget": {
           "value": "1" // string format
   }
}

i am read source code, i think error in kParameterExtensions.kt on 43 line.

i try to fix. it is my demo code.

internal fun KParameter.javaTypeClass(): Class<*> = 

    if (this.isList()) { 
         this.type.getKClass().java  

    } else { 

        val javaType = type.javaType 

        if(javaType is ParameterizedType) { 

            javaType.rawType as Class<*> 

        } else { 

            javaType as Class<*> 

        }  

    } 

point is generic is myself defined. and i expect specific return value. but just can not cast to target.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
dariuszkuccommented, Feb 27, 2020

@Ncaffeine again the underlying problem is that while Kotlin supports generics and you can definitely code whatever, generics currently cannot be represented in GraphQL schema so your Kotlin data model CANNOT be mapped 1:1 to a schema.

0reactions
oooplzcommented, Feb 27, 2020

@Ncaffeine再次提出了潜在的问题,尽管Kotlin支持泛型并且您绝对可以编写任何代码,但泛型当前无法在GraphQL模式中表示,因此您的Kotlin数据模型无法以1:1映射到模式。

thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Any" data type for GraphQL Query Return Type - Stack Overflow
If I change the type to object , I get Property 'data' does not exist on type '{}'. TS2339 but the documentation also...
Read more >
Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >
Generic Types - TypeGraphQL
Generic Types. Type Inheritance is a great way to reduce code duplication by extracting common fields to the base class. But in some...
Read more >
Schemas and Types - GraphQL
Every GraphQL service has a query type and may or may not have a mutation type. These types are the same as a...
Read more >
Exploring the future potential of generic GraphQL error codes
What if GraphQL supported a set of generic error codes? ... to return is not defined in the spec, hence it is customized...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found