Skip to content

Commit f7248de

Browse files
committed
Ability to override internal types (using types option of Schema class) #174
1 parent d46ad09 commit f7248de

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

src/Type/Schema.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public function __construct($config)
110110
);
111111

112112
$this->config = $config;
113-
$this->resolvedTypes = Type::getInternalTypes() + Introspection::getTypes();
114113
$this->resolvedTypes[$config->query->name] = $config->query;
115114

116115
if ($config->mutation) {
@@ -119,6 +118,20 @@ public function __construct($config)
119118
if ($config->subscription) {
120119
$this->resolvedTypes[$config->subscription->name] = $config->subscription;
121120
}
121+
if (is_array($this->config->types)) {
122+
foreach ($this->resolveAdditionalTypes() as $type) {
123+
if (isset($this->resolvedTypes[$type->name])) {
124+
Utils::invariant(
125+
$type === $this->resolvedTypes[$type->name],
126+
"Schema must contain unique named types but contains multiple types named \"$type\" ".
127+
"(see http://webonyx.github.io/graphql-php/type-system/#type-registry)."
128+
);
129+
}
130+
$this->resolvedTypes[$type->name] = $type;
131+
}
132+
}
133+
$this->resolvedTypes += Type::getInternalTypes() + Introspection::getTypes();
134+
122135
if (!$this->config->typeLoader) {
123136
// Perform full scan of the schema
124137
$this->getTypeMap();
@@ -209,8 +222,11 @@ private function collectAllTypes()
209222
foreach ($this->resolvedTypes as $type) {
210223
$typeMap = TypeInfo::extractTypes($type, $typeMap);
211224
}
212-
foreach ($this->resolveAdditionalTypes() as $type) {
213-
$typeMap = TypeInfo::extractTypes($type, $typeMap);
225+
// When types are set as array they are resolved in constructor
226+
if (is_callable($this->config->types)) {
227+
foreach ($this->resolveAdditionalTypes() as $type) {
228+
$typeMap = TypeInfo::extractTypes($type, $typeMap);
229+
}
214230
}
215231
return $typeMap;
216232
}

tests/StarWarsIntrospectionTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ public function testAllowsQueryingTheSchemaForTypes()
2626
$expected = [
2727
'__schema' => [
2828
'types' => [
29-
['name' => 'ID'],
29+
['name' => 'Query'],
30+
['name' => 'Episode'],
31+
['name' => 'Character'],
3032
['name' => 'String'],
33+
['name' => 'Human'],
34+
['name' => 'Droid'],
35+
['name' => 'ID'],
3136
['name' => 'Float'],
3237
['name' => 'Int'],
3338
['name' => 'Boolean'],
@@ -39,11 +44,6 @@ public function testAllowsQueryingTheSchemaForTypes()
3944
['name' => '__EnumValue'],
4045
['name' => '__Directive'],
4146
['name' => '__DirectiveLocation'],
42-
['name' => 'Query'],
43-
['name' => 'Episode'],
44-
['name' => 'Character'],
45-
['name' => 'Human'],
46-
['name' => 'Droid'],
4747
]
4848
]
4949
];

tests/Type/DefinitionTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
require_once __DIR__ . '/TestClasses.php';
55

6+
use GraphQL\Type\Definition\CustomScalarType;
67
use GraphQL\Type\Schema;
78
use GraphQL\Type\Definition\EnumType;
89
use GraphQL\Type\Definition\InputObjectType;
@@ -713,5 +714,21 @@ public function testInfersNameFromClassname()
713714
$otherCustom = new OtherCustom();
714715
$this->assertEquals('OtherCustom', $otherCustom->name);
715716
}
716-
}
717717

718+
public function testAllowsOverridingInternalTypes()
719+
{
720+
$idType = new CustomScalarType([
721+
'name' => 'ID',
722+
'serialize' => function() {},
723+
'parseValue' => function() {},
724+
'parseLiteral' => function() {}
725+
]);
726+
727+
$schema = new Schema([
728+
'query' => new ObjectType(['name' => 'Query', 'fields' => []]),
729+
'types' => [$idType]
730+
]);
731+
732+
$this->assertSame($idType, $schema->getType('ID'));
733+
}
734+
}

tests/Type/IntrospectionTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,32 @@ function testExecutesAnIntrospectionQuery()
4242
),
4343
'types' =>
4444
array (
45+
array (
46+
'kind' => 'OBJECT',
47+
'name' => 'QueryRoot',
48+
'inputFields' => NULL,
49+
'interfaces' =>
50+
array (
51+
),
52+
'enumValues' => NULL,
53+
'possibleTypes' => NULL,
54+
'fields' => array (
55+
array (
56+
'name' => 'a',
57+
'args' => array(),
58+
'type' => array(
59+
'kind' => 'SCALAR',
60+
'name' => 'String',
61+
'ofType' => null
62+
),
63+
'isDeprecated' => false,
64+
'deprecationReason' => null,
65+
)
66+
)
67+
),
4568
array (
4669
'kind' => 'SCALAR',
47-
'name' => 'ID',
70+
'name' => 'String',
4871
'fields' => NULL,
4972
'inputFields' => NULL,
5073
'interfaces' => NULL,
@@ -53,7 +76,7 @@ function testExecutesAnIntrospectionQuery()
5376
),
5477
array (
5578
'kind' => 'SCALAR',
56-
'name' => 'String',
79+
'name' => 'ID',
5780
'fields' => NULL,
5881
'inputFields' => NULL,
5982
'interfaces' => NULL,
@@ -975,29 +998,6 @@ function testExecutesAnIntrospectionQuery()
975998
),
976999
'possibleTypes' => NULL,
9771000
),
978-
array (
979-
'kind' => 'OBJECT',
980-
'name' => 'QueryRoot',
981-
'inputFields' => NULL,
982-
'interfaces' =>
983-
array (
984-
),
985-
'enumValues' => NULL,
986-
'possibleTypes' => NULL,
987-
'fields' => array (
988-
array (
989-
'name' => 'a',
990-
'args' => array(),
991-
'type' => array(
992-
'kind' => 'SCALAR',
993-
'name' => 'String',
994-
'ofType' => null
995-
),
996-
'isDeprecated' => false,
997-
'deprecationReason' => null,
998-
)
999-
)
1000-
),
10011001
),
10021002
'directives' =>
10031003
array (

0 commit comments

Comments
 (0)