Skip to content

Conversation

@folone
Copy link
Contributor

@folone folone commented Oct 5, 2021

Hi folks!

Problem

Graphql-java currently fails the document validation for queries that use nullable types for non-nullable schema field arguments with default values. As per spec:

A notable exception to typical variable type compatibility is allowing a variable definition with a nullable type to be provided to a non‐null location as long as either that variable or that location provides a default value.

Here's a discussion from 2018 where this change to the spec was adopted: graphql/graphql-spec#418.

Motivating example:

Schema:

type Query {
  hello(arg: Boolean! = true)
}

Query:

query Test($var: Boolean) {
  hello(arg: $var)
}

This is expected to pass the validation, but currently fails:
ValidationError{validationErrorType=VariableTypeMismatch, queryPath=[hello], message=Validation error of type VariableTypeMismatch: Variable type 'Boolean' doesn't match expected type 'Boolean!' @ 'hello', locations=[SourceLocation{line=3, column=25}], description='Variable type 'Boolean' doesn't match expected type 'Boolean!''}

Solution

This patch fixes the issue by extracting the schema-defined default value for the argument (if present), and checks that in the VariablesTypeMatcher along with the original check. This also adds a regression test to exercise this fix, similar to the test in the reference GraphQL implementation.

George Leontiev added 2 commits October 5, 2021 14:04
… in the query for non-nullable argument with default argument value specified in the schema.
@folone folone marked this pull request as ready for review October 5, 2021 13:37
@folone folone changed the title Bugfix: don't fail document validation when nullable type is provided in the query for non-nullable argument with default argument value specified in the schema. Bugfix: don't fail document validation when nullable type is provided in the query for non-nullable argument with default value specified in the schema. Oct 5, 2021
@jbellenger
Copy link
Contributor

Related to this, here's a test case in graphql-js for this behavior

@folone folone changed the title Bugfix: don't fail document validation when nullable type is provided in the query for non-nullable argument with default value specified in the schema. Bugfix: spec-compliant behaviour for non-nullable types with default values. Oct 6, 2021
schemaDefaultValue = (Value) schemaDefault.get().getValue();
} else if (schemaDefault.isPresent() && schemaDefault.get().isSet()) {
schemaDefaultValue = ValuesResolver.valueToLiteral(schemaDefault.get(), expectedType);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andimarek - can you please double check that the argument get value calls here are correct in the new regime for reading args values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify the code here: there is a method in ValuesResolver ValuesResolver.valueToLiteral which takes care of converting it fo you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @andimarek! I believe that's the exact method I'm using on line 67 there. Am I missing something else?

Copy link
Member

@andimarek andimarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that. Please have a look a the comment I left.

@bbakerman bbakerman added this to the 18.0 milestone Dec 27, 2021
@bbakerman bbakerman merged commit 309fb9e into graphql-java:master Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants