graphql-spring-boot icon indicating copy to clipboard operation
graphql-spring-boot copied to clipboard

Support for Kotlin VS Schema nullability check

Open pelletier197 opened this issue 2 years ago • 0 comments

I am writing this to discuss an issue I am facing once in a while. This is more an open discussion on the feasibility of this feature, but I think this is something that the framework would greatly benefit to get.

At the moment, it is possible to declare different nullability in your Kotlin Class compared to your GraphQL Schema

For Instance

type Query {
  test(target: String) : Boolean # <= target and output are nullable
}

could be implemented by the following resolver, and it would boot properly without any error

@Component
class GraphQLTestResolver : GraphQLQueryResolver {
    fun test(target: String): Boolean = true // <= target is non-nullable
}

However, when performing the following GraphQL Query,

query {
  test(target: null)
}

I would get the following error

java.lang.NullPointerException: Parameter specified as non-null is null: method com.test.graphql.GraphQLTest.test, parameter target
	at com.test.graphql.GraphQLTest.test(GraphQLTest.kt)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at graphql.kickstart.tools.resolver.MethodFieldResolverDataFetcher.get(MethodFieldResolver.kt:261)
	at graphql.schema.DataFetcherFactories.lambda$wrapDataFetcher$2(DataFetcherFactories.java:37)
	at graphql.validation.schemawiring.FieldValidatorDataFetcher.get(FieldValidatorDataFetcher.java:54)
	at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:279)
	at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:210)
       ....

Which inevitably returns an InternalError

Describe the solution you'd like For me, an ideal solution would be that Spring-GraphQL would perform nullability checks for Kotlin classes at boot time (could also be done for java with @Nullable, but again to discuss), the same way it already validate the schema in many different ways.

Thank you for your help, and mostly, thanks a lot for your incredible work on this framework!

pelletier197 avatar Nov 24 '21 13:11 pelletier197