-
-
Notifications
You must be signed in to change notification settings - Fork 568
feat: Add Support for @oneOf
Input Object Directive
#1715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1235a0d
- Implement the oneOf directive tests, following the graphql-js imple…
jasonbahl 982c3aa
- update Introspection to include isOneOf field
jasonbahl 3cc0c71
- update validation rule to keep updated with suggestions in https://…
jasonbahl e076b38
- update to add pre-coercion of OneOf errors, following this pending …
jasonbahl 3b874ad
- add unreleased changelog entry
jasonbahl b64fdd7
- phpstan corrections
jasonbahl b7a37e1
Update CHANGELOG.md
jasonbahl c0b978d
- merge changes from latest master and resolve merge conflicts
jasonbahl 64be6d2
- add test case ensuring that InputObjectType has at least one or mor…
jasonbahl 646fef1
- revert change to path in coerceInputValue
jasonbahl 82bd7cf
- move isOneOf below description, above fields
jasonbahl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace GraphQL\Validator\Rules; | ||
|
||
use GraphQL\Error\Error; | ||
use GraphQL\Language\AST\NodeKind; | ||
use GraphQL\Language\AST\ObjectValueNode; | ||
use GraphQL\Type\Definition\InputObjectType; | ||
use GraphQL\Type\Definition\Type; | ||
use GraphQL\Validator\QueryValidationContext; | ||
|
||
/** | ||
* OneOf Input Objects validation rule. | ||
* | ||
* Validates that OneOf Input Objects have exactly one non-null field provided. | ||
*/ | ||
class OneOfInputObjectsRule extends ValidationRule | ||
{ | ||
public function getVisitor(QueryValidationContext $context): array | ||
{ | ||
return [ | ||
NodeKind::OBJECT => static function (ObjectValueNode $node) use ($context): void { | ||
$type = $context->getInputType(); | ||
|
||
if ($type === null) { | ||
return; | ||
} | ||
|
||
$namedType = Type::getNamedType($type); | ||
if (! ($namedType instanceof InputObjectType) | ||
|| ! $namedType->isOneOf() | ||
) { | ||
return; | ||
} | ||
|
||
$providedFields = []; | ||
$nullFields = []; | ||
|
||
foreach ($node->fields as $fieldNode) { | ||
$fieldName = $fieldNode->name->value; | ||
$providedFields[] = $fieldName; | ||
|
||
// Check if the field value is explicitly null | ||
if ($fieldNode->value->kind === NodeKind::NULL) { | ||
$nullFields[] = $fieldName; | ||
} | ||
} | ||
|
||
$fieldCount = count($providedFields); | ||
|
||
if ($fieldCount === 0) { | ||
$context->reportError(new Error( | ||
static::oneOfInputObjectExpectedExactlyOneFieldMessage($namedType->name), | ||
[$node] | ||
)); | ||
|
||
return; | ||
} | ||
|
||
if ($fieldCount > 1) { | ||
$context->reportError(new Error( | ||
static::oneOfInputObjectExpectedExactlyOneFieldMessage($namedType->name, $fieldCount), | ||
[$node] | ||
)); | ||
|
||
return; | ||
} | ||
|
||
// At this point, $fieldCount === 1 | ||
if (count($nullFields) > 0) { | ||
// Exactly one field provided, but it's null | ||
$context->reportError(new Error( | ||
static::oneOfInputObjectFieldValueMustNotBeNullMessage($namedType->name, $nullFields[0]), | ||
[$node] | ||
)); | ||
} | ||
}, | ||
]; | ||
} | ||
|
||
public static function oneOfInputObjectExpectedExactlyOneFieldMessage(string $typeName, ?int $providedCount = null): string | ||
{ | ||
if ($providedCount === null) { | ||
return "OneOf input object '{$typeName}' must specify exactly one field."; | ||
} | ||
|
||
return "OneOf input object '{$typeName}' must specify exactly one field, but {$providedCount} fields were provided."; | ||
} | ||
|
||
public static function oneOfInputObjectFieldValueMustNotBeNullMessage(string $typeName, string $fieldName): string | ||
{ | ||
return "OneOf input object '{$typeName}' field '{$fieldName}' must be non-null."; | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.