Skip to content

Validate default values match argument type - #843

Draft
spawnia wants to merge 1 commit into
masterfrom
validate-default-values-match-argument-type
Draft

Validate default values match argument type#843
spawnia wants to merge 1 commit into
masterfrom
validate-default-values-match-argument-type

Conversation

@spawnia

@spawnia spawnia commented Apr 15, 2021

Copy link
Copy Markdown
Collaborator

See graphql/graphql-js#2606 for discussion.

Since the pace of development in graphql-js has not been great lately and we just hit this issue, I think we could charge ahead and pioneer a solution for it 💪

@spawnia spawnia added the bug label Apr 15, 2021
@spawnia

spawnia commented Jul 2, 2021

Copy link
Copy Markdown
Collaborator Author

Relevant: graphql/graphql-js#3049

@spawnia

spawnia commented Oct 18, 2024

Copy link
Copy Markdown
Collaborator Author

See graphql/graphql-js#3814.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 early continue.

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]],
],
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants