Skip to content

Add missing __Directive.args(includeDeprecated:) #1738

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/Type/Introspection.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static function getIntrospectionQuery(array $options = []): string
directives {
name
{$descriptions}
args {
args(includeDeprecated: true) {
...InputValue
}
{$directiveIsRepeatable}
Expand Down Expand Up @@ -354,15 +354,15 @@ public static function _type(): ObjectType
'type' => Type::listOf(Type::nonNull(self::_field())),
'args' => [
'includeDeprecated' => [
'type' => Type::boolean(),
'type' => Type::nonNull(Type::boolean()),
'defaultValue' => false,
],
],
'resolve' => static function (Type $type, $args): ?array {
if ($type instanceof ObjectType || $type instanceof InterfaceType) {
$fields = $type->getVisibleFields();

if (! ($args['includeDeprecated'] ?? false)) {
if (! $args['includeDeprecated']) {
return array_filter(
$fields,
static fn (FieldDefinition $field): bool => ! $field->isDeprecated()
Expand Down Expand Up @@ -391,15 +391,15 @@ public static function _type(): ObjectType
'type' => Type::listOf(Type::nonNull(self::_enumValue())),
'args' => [
'includeDeprecated' => [
'type' => Type::boolean(),
'type' => Type::nonNull(Type::boolean()),
'defaultValue' => false,
],
],
'resolve' => static function ($type, $args): ?array {
if ($type instanceof EnumType) {
$values = $type->getValues();

if (! ($args['includeDeprecated'] ?? false)) {
if (! $args['includeDeprecated']) {
return array_filter(
$values,
static fn (EnumValueDefinition $value): bool => ! $value->isDeprecated()
Expand All @@ -416,15 +416,15 @@ public static function _type(): ObjectType
'type' => Type::listOf(Type::nonNull(self::_inputValue())),
'args' => [
'includeDeprecated' => [
'type' => Type::boolean(),
'type' => Type::nonNull(Type::boolean()),
'defaultValue' => false,
],
],
'resolve' => static function ($type, $args): ?array {
if ($type instanceof InputObjectType) {
$fields = $type->getFields();

if (! ($args['includeDeprecated'] ?? false)) {
if (! $args['includeDeprecated']) {
return array_filter(
$fields,
static fn (InputObjectField $field): bool => ! $field->isDeprecated(),
Expand Down Expand Up @@ -516,14 +516,14 @@ public static function _field(): ObjectType
'type' => Type::nonNull(Type::listOf(Type::nonNull(self::_inputValue()))),
'args' => [
'includeDeprecated' => [
'type' => Type::boolean(),
'type' => Type::nonNull(Type::boolean()),
'defaultValue' => false,
],
],
'resolve' => static function (FieldDefinition $field, $args): array {
$values = $field->args;

if (! ($args['includeDeprecated'] ?? false)) {
if (! $args['includeDeprecated']) {
return array_filter(
$values,
static fn (Argument $value): bool => ! $value->isDeprecated(),
Expand Down Expand Up @@ -667,7 +667,24 @@ public static function _directive(): ObjectType
],
'args' => [
'type' => Type::nonNull(Type::listOf(Type::nonNull(self::_inputValue()))),
'resolve' => static fn (Directive $directive): array => $directive->args,
'args' => [
'includeDeprecated' => [
'type' => Type::nonNull(Type::boolean()),
'defaultValue' => false,
],
],
'resolve' => static function (Directive $directive, $args): array {
$values = $directive->args;

if (! $args['includeDeprecated']) {
return array_filter(
$values,
static fn (Argument $value): bool => ! $value->isDeprecated(),
);
}

return $values;
},
],
],
]);
Expand Down
142 changes: 129 additions & 13 deletions tests/Type/IntrospectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use GraphQL\GraphQL;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Language\SourceLocation;
use GraphQL\Tests\ErrorHelper;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InputObjectType;
Expand Down Expand Up @@ -235,9 +237,13 @@ public function testExecutesAnIntrospectionQuery(): void
0 => [
'name' => 'includeDeprecated',
'type' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
'kind' => 'NON_NULL',
'name' => null,
'ofType' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
],
],
'defaultValue' => 'false',
'isDeprecated' => false,
Expand Down Expand Up @@ -304,9 +310,13 @@ public function testExecutesAnIntrospectionQuery(): void
0 => [
'name' => 'includeDeprecated',
'type' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
'kind' => 'NON_NULL',
'name' => null,
'ofType' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
],
],
'defaultValue' => 'false',
'isDeprecated' => false,
Expand Down Expand Up @@ -335,9 +345,13 @@ public function testExecutesAnIntrospectionQuery(): void
0 => [
'name' => 'includeDeprecated',
'type' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
'kind' => 'NON_NULL',
'name' => null,
'ofType' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
],
],
'defaultValue' => 'false',
'isDeprecated' => false,
Expand Down Expand Up @@ -476,9 +490,13 @@ public function testExecutesAnIntrospectionQuery(): void
0 => [
'name' => 'includeDeprecated',
'type' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
'kind' => 'NON_NULL',
'name' => null,
'ofType' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
],
],
'defaultValue' => 'false',
'isDeprecated' => false,
Expand Down Expand Up @@ -775,7 +793,23 @@ public function testExecutesAnIntrospectionQuery(): void
],
[
'name' => 'args',
'args' => [],
'args' => [
0 => [
'name' => 'includeDeprecated',
'type' => [
'kind' => 'NON_NULL',
'name' => null,
'ofType' => [
'kind' => 'SCALAR',
'name' => 'Boolean',
'ofType' => null,
],
],
'defaultValue' => 'false',
'isDeprecated' => false,
'deprecationReason' => null,
],
],
'type' => [
'kind' => 'NON_NULL',
'name' => null,
Expand Down Expand Up @@ -1872,4 +1906,86 @@ public function testIsOneOf(): void
$introspection = Introspection::fromSchema($schema);
self::assertTrue($introspection['__schema']['types'][1]['isOneOf']);
}

public function testRespectsTheIncludeDeprecatedParameterForDirectiveArgs(): void
{
$query = new ObjectType([
'name' => 'QueryRoot',
'fields' => [
[
'name' => 'test',
'type' => Type::int(),
],
],
]);

$directive = new Directive([
'name' => 'TestDirective',
'args' => [
'nonDeprecated' => [
'type' => Type::string(),
],
'deprecated' => [
'type' => Type::string(),
'deprecationReason' => 'Removed in 1.0',
],
],
'locations' => [DirectiveLocation::FIELD],
]);

$schema = new Schema([
'query' => $query,
'directives' => [$directive],
]);

$request = '
{
__schema {
directives {
trueArgs: args(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
falseArgs: args(includeDeprecated: false) {
name
}
omittedArgs: args {
name
}
}
}
}
';

$expected = [
[
'trueArgs' => [
[
'name' => 'nonDeprecated',
'isDeprecated' => false,
'deprecationReason' => null,
],
[
'name' => 'deprecated',
'isDeprecated' => true,
'deprecationReason' => 'Removed in 1.0',
],
],
'falseArgs' => [
[
'name' => 'nonDeprecated',
],
],
'omittedArgs' => [
[
'name' => 'nonDeprecated',
],
],
],
];

$result = GraphQL::executeQuery($schema, $request)->toArray();
self::assertSame($expected, $result['data']['__schema']['directives'] ?? null);
}
}
10 changes: 5 additions & 5 deletions tests/Utils/SchemaPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1083,11 +1083,11 @@ public function testPrintIntrospectionSchema(): void
kind: __TypeKind!
name: String
description: String
fields(includeDeprecated: Boolean = false): [__Field!]
fields(includeDeprecated: Boolean! = false): [__Field!]
interfaces: [__Type!]
possibleTypes: [__Type!]
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
inputFields(includeDeprecated: Boolean = false): [__InputValue!]
enumValues(includeDeprecated: Boolean! = false): [__EnumValue!]
inputFields(includeDeprecated: Boolean! = false): [__InputValue!]
ofType: __Type
isOneOf: Boolean
}
Expand Down Expand Up @@ -1123,7 +1123,7 @@ enum __TypeKind {
type __Field {
name: String!
description: String
args(includeDeprecated: Boolean = false): [__InputValue!]!
args(includeDeprecated: Boolean! = false): [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
Expand Down Expand Up @@ -1160,7 +1160,7 @@ enum __TypeKind {
description: String
isRepeatable: Boolean!
locations: [__DirectiveLocation!]!
args: [__InputValue!]!
args(includeDeprecated: Boolean! = false): [__InputValue!]!
}

"A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies."
Expand Down
Loading