-
-
Notifications
You must be signed in to change notification settings - Fork 564
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
Updates to allow printing of Schema Directives #638
Changes from all commits
e7dfe8d
f1b16fb
5741d8e
e590dff
7acfc93
0cff146
4e809bf
4497606
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
namespace GraphQL\Utils; | ||
|
||
use GraphQL\Error\Error; | ||
use GraphQL\Language\AST\InterfaceTypeDefinitionNode; | ||
use GraphQL\Language\AST\NodeList; | ||
use GraphQL\Language\AST\ObjectTypeDefinitionNode; | ||
use GraphQL\Language\Printer; | ||
use GraphQL\Type\Definition\Directive; | ||
use GraphQL\Type\Definition\EnumType; | ||
|
@@ -24,6 +27,7 @@ | |
use function count; | ||
use function explode; | ||
use function implode; | ||
use function iterator_to_array; | ||
use function ksort; | ||
use function mb_strlen; | ||
use function preg_match_all; | ||
|
@@ -366,7 +370,7 @@ static function ($i) { | |
: ''; | ||
|
||
return self::printDescription($options, $type) . | ||
sprintf("type %s%s {\n%s\n}", $type->name, $implementedInterfaces, self::printFields($options, $type)); | ||
sprintf("type %s%s%s {\n%s\n}", $type->name, $implementedInterfaces, self::printSchemaDirectives($type), self::printFields($options, $type)); | ||
} | ||
|
||
/** | ||
|
@@ -410,7 +414,7 @@ private static function printDeprecated($fieldOrEnumVal) : string | |
private static function printInterface(InterfaceType $type, array $options) : string | ||
{ | ||
return self::printDescription($options, $type) . | ||
sprintf("interface %s {\n%s\n}", $type->name, self::printFields($options, $type)); | ||
sprintf("interface %s%s {\n%s\n}", $type->name, self::printSchemaDirectives($type), self::printFields($options, $type)); | ||
} | ||
|
||
/** | ||
|
@@ -487,4 +491,46 @@ public static function printIntrospectionSchema(Schema $schema, array $options = | |
$options | ||
); | ||
} | ||
|
||
public static function printSchemaDirectives(Type $type) : string | ||
{ | ||
if ($type->astNode === null) { | ||
return ''; | ||
} | ||
|
||
if ($type->astNode instanceof ObjectTypeDefinitionNode) { | ||
$directives = $type->astNode->directives; | ||
} elseif ($type->astNode instanceof InterfaceTypeDefinitionNode) { | ||
$directives = $type->astNode->directives; | ||
} else { | ||
return ''; | ||
} | ||
|
||
if ($directives instanceof NodeList) { | ||
$count = $directives->count(); | ||
$directives = iterator_to_array($directives->getIterator()); | ||
} else { | ||
$count = count($directives); | ||
} | ||
|
||
return $count > 0 ? (' ' . implode( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd invert the condition so |
||
' ', | ||
array_map( | ||
static function ($directive) : string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add some type hint for |
||
$directiveString = '@' . $directive->name->value; | ||
if ($directive->arguments->count() > 0) { | ||
$directiveString .= '('; | ||
foreach ($directive->arguments as $argument) { | ||
$directiveString .= $argument->name->value . ': '; | ||
$directiveString .= Printer::doPrint($argument->value); | ||
} | ||
$directiveString .= ')'; | ||
} | ||
|
||
return $directiveString; | ||
}, | ||
$directives | ||
) | ||
)) : ''; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1237,4 +1237,203 @@ enum __TypeKind { | |||||
EOT; | ||||||
self::assertEquals($introspectionSchema, $output); | ||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveNoArgs() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd on OBJECT | ||||||
|
||||||
type Bar @sd { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveWithStringArgs() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd(field: String!) on OBJECT | ||||||
|
||||||
type Bar @sd(field: "String") { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveWithNumberArgs() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd(field: Int!) on OBJECT | ||||||
|
||||||
type Bar @sd(field: 1) { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveWithArrayArgs() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd(field: [Int!]) on OBJECT | ||||||
|
||||||
type Bar @sd(field: [1, 2, 3, 4]) { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveWithTypeArgs() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd(field: Foo) on OBJECT | ||||||
|
||||||
type Bar @sd(field: {bar: "test"}) { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Foo { | ||||||
bar: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveOptionalArgs() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd(field: String) on OBJECT | ||||||
|
||||||
type Bar @sd(field: "Testing") { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Foo @sd { | ||||||
bar: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
bar: Foo | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintMultipleSchemaDirectives() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd(field: [Int!]) on OBJECT | ||||||
|
||||||
directive @sdb on OBJECT | ||||||
|
||||||
type Bar @sd(field: [1, 2, 3, 4]) @sdb { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveOnClassWithInterface() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd on OBJECT | ||||||
|
||||||
type Bar implements Foo @sd { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
interface Foo { | ||||||
bar: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
public function testPrintSchemaDirectiveOnInterface() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
$exceptedSdl = ' | ||||||
directive @sd on INTERFACE | ||||||
|
||||||
type Bar implements Foo { | ||||||
foo: String | ||||||
} | ||||||
|
||||||
interface Foo @sd { | ||||||
bar: String | ||||||
} | ||||||
|
||||||
type Query { | ||||||
foo: Bar | ||||||
} | ||||||
'; | ||||||
|
||||||
$schema = BuildSchema::build($exceptedSdl); | ||||||
$actual = $this->printForTest($schema); | ||||||
|
||||||
self::assertEquals($exceptedSdl, $actual); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First two codnitions can be merged