Support custom directives in SchemaPrinter - #875
Draft
jbtbnl wants to merge 1 commit into
Draft
Conversation
jbtbnl
force-pushed
the
master
branch
12 times, most recently
from
June 23, 2021 12:25
cd71345 to
a4b9d40
Compare
Collaborator
|
See #552 and graphql/graphql-js#2020 for discussion. |
jbtbnl
force-pushed
the
master
branch
6 times, most recently
from
June 23, 2021 13:13
52864c1 to
dcd255d
Compare
Contributor
Author
|
@spawnia thanks for pointing out related discussions. Whether this PR is the way to go or not, it hopefully at least contributes to keep this to the attention. |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for printing SDL-defined (custom) directives in SchemaPrinter output, enabling a BuildSchema::build(Parser::parse($sdl)) round-trip to preserve directives on type system elements.
Changes:
- Extend
SchemaPrinterto serialize directives found on AST nodes for fields, enum values, and scalars. - Add a PHPUnit test asserting custom field directives survive parse → build → print.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Utils/SchemaPrinter.php |
Adds directive serialization for field/type elements when astNode directives are present. |
tests/Utils/SchemaPrinterTest.php |
Adds a round-trip test verifying custom directives appear in printed SDL. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+449
to
+468
| $serialized .= '(' . implode(', ', array_map(static function (ArgumentNode $argument): string { | ||
| switch ($argument->value->kind) { | ||
| case NodeKind::INT: | ||
| $type = Type::int(); | ||
| break; | ||
| case NodeKind::FLOAT: | ||
| $type = Type::float(); | ||
| break; | ||
| case NodeKind::STRING: | ||
| $type = Type::string(); | ||
| break; | ||
| case NodeKind::BOOLEAN: | ||
| $type = Type::boolean(); | ||
| break; | ||
| default: | ||
| return ''; | ||
| } | ||
|
|
||
| return $argument->name->value . ': ' . Printer::doPrint(AST::astFromValue($argument->value->value, $type)); | ||
| }, iterator_to_array($directive->arguments))) . ')'; |
Comment on lines
+1377
to
+1388
| $SDL = <<<'EOT' | ||
| directive @customDirective(stringArgument: String!, intArgument: Int!, floatArgument: Float!, booleanArgument: Boolean!) on FIELD_DEFINITION | ||
|
|
||
| directive @customDirectiveWithoutArguments on FIELD_DEFINITION | ||
|
|
||
| type Query { | ||
| field1: Boolean @customDirective(stringArgument: "test", intArgument: 42, floatArgument: 0.1, booleanArgument: true) | ||
| field2: Boolean @customDirectiveWithoutArguments | ||
| field3: Boolean | ||
| } | ||
|
|
||
| EOT; |
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.
No description provided.