javagraphqlgraphql-java-tools

graphql-java-tools: null parameter vs not present parameter


How can I distinguish in java graphQL if a parameter was explicitly set to null, or if it was not provided at all?

The use case that I try to achieve is the following: I have a mutation like this

updateUser(id:Int!, changes:UserChanges!)

#where UserChanges is defined as:
type UserChanges {
    login:String
    email:String
    #etc

}

The idea here is that the user provides only the fields that he wants to change (like react setState for example). So if email is ommited, I want to leave it unchanged.

But what if he/she wants to explicitly set email to null? Is there any way figure this out from my resolver method and act accordingly?

(I use graphql-java-tools library)


Solution

  • I found the answer. In case somebody needs it: An instance of graphql.schema.DataFetchingEnvironment is available to every resolver method.

    This provides methods like getArguments(), hasArgument() etc. Using those methods, we can find out if an argument was an explicit set to null, or if it was not provided at all.