graphQL query return type does not support custom generic.
See original GitHub issueLibrary 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:
- Created 4 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
thank you.