Validate default values match argument type - #843
Draft
spawnia wants to merge 1 commit into
Draft
Conversation
Collaborator
Author
|
Relevant: graphql/graphql-js#3049 |
Collaborator
Author
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to add schema-validation for SDL default values so that argument default literals must be coercible to the argument’s declared type (mirroring the behavior discussed in graphql-js#2606).
Changes:
- Adds a new schema validation test asserting that invalid field argument default values are rejected.
- Documents (via comment) that SDL default value coercion may produce
Utils::undefined()and is intended to be validated later. - Refactors directive validation guards in
SchemaValidationContext::validateFields()to avoid earlycontinue.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/Type/ValidationTest.php | Adds coverage for rejecting incorrect default value types on field arguments. |
| src/Validator/DocumentValidator.php | Adds a TODO about introducing an SDL rule related to default values. |
| src/Utils/ASTDefinitionBuilder.php | Adds comments clarifying deferral of default value/type validation. |
| src/Type/SchemaValidationContext.php | Refactors directive validation checks for arguments/fields. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public static function sdlRules() | ||
| { | ||
| if (self::$sdlRules === null) { | ||
| // TODO add rule DefaultValuesMatchArgumentType |
Comment on lines
510
to
+514
| // Ensure argument definition directives are valid | ||
| if (! isset($arg->astNode, $arg->astNode->directives)) { | ||
| continue; | ||
| if (isset($arg->astNode, $arg->astNode->directives)) { | ||
| $this->validateDirectivesAtLocation( | ||
| $arg->astNode->directives, | ||
| DirectiveLocation::ARGUMENT_DEFINITION |
Comment on lines
+1281
to
+1289
| [ | ||
| 'message' => 'Expected foo.int to have a default value of type Int, got "not an int".', | ||
| 'locations' => [['line' => 7, 'column' => 16], ['line' => 11, 'column' => 16]], | ||
| ], | ||
| [ | ||
| 'message' => 'Expected foo.string to have a default value of type String, got 42.', | ||
| 'locations' => [['line' => 7, 'column' => 16], ['line' => 11, 'column' => 16]], | ||
| ], | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See graphql/graphql-js#2606 for discussion.
Since the pace of development in
graphql-jshas not been great lately and we just hit this issue, I think we could charge ahead and pioneer a solution for it 💪