Skip to content

Commit eeccd3c

Browse files
committed
Exclude unused standard types from the schema
#964
1 parent fcef003 commit eeccd3c

File tree

8 files changed

+875
-1056
lines changed

8 files changed

+875
-1056
lines changed

src/Type/Introspection.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GraphQL\Language\Printer;
1111
use GraphQL\Type\Definition\Directive;
1212
use GraphQL\Type\Definition\EnumType;
13+
use GraphQL\Type\Definition\EnumValueDefinition;
1314
use GraphQL\Type\Definition\FieldArgument;
1415
use GraphQL\Type\Definition\FieldDefinition;
1516
use GraphQL\Type\Definition\InputObjectField;
@@ -329,7 +330,10 @@ public static function _type(): ObjectType
329330
'fields' => [
330331
'type' => Type::listOf(Type::nonNull(self::_field())),
331332
'args' => [
332-
'includeDeprecated' => ['type' => Type::boolean(), 'defaultValue' => false],
333+
'includeDeprecated' => [
334+
'type' => Type::boolean(),
335+
'defaultValue' => false,
336+
],
333337
],
334338
'resolve' => static function (Type $type, $args): ?array {
335339
if ($type instanceof ObjectType || $type instanceof InterfaceType) {
@@ -339,7 +343,8 @@ public static function _type(): ObjectType
339343
$fields = array_filter(
340344
$fields,
341345
static function (FieldDefinition $field): bool {
342-
return ($field->deprecationReason ?? '') === '';
346+
return $field->deprecationReason === null
347+
|| $field->deprecationReason === '';
343348
}
344349
);
345350
}
@@ -377,13 +382,14 @@ static function (FieldDefinition $field): bool {
377382
],
378383
'resolve' => static function ($type, $args): ?array {
379384
if ($type instanceof EnumType) {
380-
$values = array_values($type->getValues());
385+
$values = $type->getValues();
381386

382387
if (! ($args['includeDeprecated'] ?? false)) {
383-
$values = array_filter(
388+
return array_filter(
384389
$values,
385-
static function ($value): bool {
386-
return ($value->deprecationReason ?? '') === '';
390+
static function (EnumValueDefinition $value): bool {
391+
return $value->deprecationReason === null
392+
|| $value->deprecationReason === '';
387393
}
388394
);
389395
}
@@ -499,7 +505,8 @@ public static function _field(): ObjectType
499505
'isDeprecated' => [
500506
'type' => Type::nonNull(Type::boolean()),
501507
'resolve' => static function (FieldDefinition $field): bool {
502-
return (bool) $field->deprecationReason;
508+
return $field->deprecationReason !== null
509+
&& $field->deprecationReason !== '';
503510
},
504511
],
505512
'deprecationReason' => [
@@ -595,14 +602,15 @@ public static function _enumValue(): ObjectType
595602
],
596603
'isDeprecated' => [
597604
'type' => Type::nonNull(Type::boolean()),
598-
'resolve' => static function ($enumValue): bool {
599-
return (bool) $enumValue->deprecationReason;
605+
'resolve' => static function (EnumValueDefinition $value): bool {
606+
return $value->deprecationReason !== null
607+
&& $value->deprecationReason !== '';
600608
},
601609
],
602610
'deprecationReason' => [
603611
'type' => Type::string(),
604-
'resolve' => static function ($enumValue) {
605-
return $enumValue->deprecationReason;
612+
'resolve' => static function (EnumValueDefinition $enumValue): ?string {
613+
return $enumValue->deprecationReason;
606614
},
607615
],
608616
],
@@ -742,7 +750,6 @@ public static function _directiveLocation(): EnumType
742750
'value' => DirectiveLocation::INPUT_FIELD_DEFINITION,
743751
'description' => 'Location adjacent to an input object field definition.',
744752
],
745-
746753
],
747754
]);
748755
}

src/Type/Schema.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function __construct($config)
135135
);
136136
}
137137

138-
$this->resolvedTypes += Type::getStandardTypes() + Introspection::getTypes();
138+
$this->resolvedTypes += Introspection::getTypes();
139139

140140
if (isset($this->config->typeLoader)) {
141141
return;
@@ -296,7 +296,9 @@ public function getConfig(): SchemaConfig
296296
public function getType(string $name): ?Type
297297
{
298298
if (! isset($this->resolvedTypes[$name])) {
299-
$type = $this->loadType($name);
299+
$type = Type::getStandardTypes()[$name]
300+
?? $this->loadType($name)
301+
?? null;
300302

301303
if ($type === null) {
302304
return null;

tests/Executor/ExecutorLazySchemaTest.php

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,29 @@
2828

2929
class ExecutorLazySchemaTest extends TestCase
3030
{
31-
/** @var ScalarType */
32-
public $someScalarType;
31+
public ScalarType $someScalarType;
3332

34-
/** @var ObjectType */
35-
public $someObjectType;
33+
public ObjectType $someObjectType;
3634

37-
/** @var ObjectType */
38-
public $otherObjectType;
35+
public ObjectType $otherObjectType;
3936

40-
/** @var ObjectType */
41-
public $deeperObjectType;
37+
public ObjectType $deeperObjectType;
4238

43-
/** @var UnionType */
44-
public $someUnionType;
39+
public UnionType $someUnionType;
4540

46-
/** @var InterfaceType */
47-
public $someInterfaceType;
41+
public InterfaceType $someInterfaceType;
4842

49-
/** @var EnumType */
50-
public $someEnumType;
43+
public EnumType $someEnumType;
5144

52-
/** @var InputObjectType */
53-
public $someInputObjectType;
45+
public InputObjectType $someInputObjectType;
5446

55-
/** @var ObjectType */
56-
public $queryType;
47+
public ObjectType $queryType;
5748

58-
/** @var string[] */
59-
public $calls = [];
49+
/** @var array<int, string> */
50+
public array $calls = [];
6051

61-
/** @var bool[] */
62-
public $loadedTypes = [];
52+
/** @var array<string, true> */
53+
public array $loadedTypes = [];
6354

6455
public function testWarnsAboutSlowIsTypeOfForLazySchema(): void
6556
{
@@ -372,36 +363,31 @@ public function testDeepQuery(): void
372363
{
373364
$schema = new Schema([
374365
'query' => $this->loadType('Query'),
375-
'typeLoader' => function ($name) {
366+
'typeLoader' => function (string $name): Type {
376367
return $this->loadType($name, true);
377368
},
378369
]);
379370

380-
$query = '{ object { object { object { string } } } }';
371+
$query = '{ object { object { object { string } } } }';
372+
$rootValue = ['object' => ['object' => ['object' => ['string' => 'test']]]];
373+
381374
$result = Executor::execute(
382375
$schema,
383376
Parser::parse($query),
384-
['object' => ['object' => ['object' => ['string' => 'test']]]]
377+
$rootValue
385378
);
386379

387-
$expected = [
388-
'data' => ['object' => ['object' => ['object' => ['string' => 'test']]]],
389-
];
390-
$expectedLoadedTypes = [
380+
self::assertEquals(['data' => $rootValue], $result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE));
381+
self::assertEquals([
391382
'Query' => true,
392383
'SomeObject' => true,
393384
'OtherObject' => true,
394-
];
395-
396-
self::assertEquals($expected, $result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE));
397-
self::assertEquals($expectedLoadedTypes, $this->loadedTypes);
398-
399-
$expectedExecutorCalls = [
385+
], $this->loadedTypes);
386+
self::assertEquals([
400387
'Query.fields',
401388
'SomeObject',
402389
'SomeObject.fields',
403-
];
404-
self::assertEquals($expectedExecutorCalls, $this->calls);
390+
], $this->calls);
405391
}
406392

407393
public function testResolveUnion(): void

tests/StarWarsIntrospectionTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ public function testAllowsQueryingTheSchemaForTypes(): void
3535
['name' => 'String'],
3636
['name' => 'Human'],
3737
['name' => 'Droid'],
38-
['name' => 'ID'],
39-
['name' => 'Float'],
40-
['name' => 'Int'],
41-
['name' => 'Boolean'],
4238
['name' => '__Schema'],
4339
['name' => '__Type'],
4440
['name' => '__TypeKind'],
41+
['name' => 'Boolean'],
4542
['name' => '__Field'],
4643
['name' => '__InputValue'],
4744
['name' => '__EnumValue'],

0 commit comments

Comments
 (0)