Skip to content

Commit 21c5996

Browse files
spawniasimPod
andauthored
Implement BuildClientSchema (#539)
* wip * wip * Finish implementation * Add Introspection::fromSchema * wip test * format * types * Use array instead of stdClass * Add tests * Mention TypeKind constants change in UPGRADE.md * Adress review comments * Some codestyle fixes * Fix codestyle and static analysis * Build some tests * Add more tests * Fix test and implementation * Fix codestyle * Change wording * Revert implementation of defaultValueExists * Fix codestyle * Call instance method not statically * Fix codestyle * Make callable functions public * Update src/Type/Introspection.php Co-Authored-By: Šimon Podlipský <simon@podlipsky.net> * Update tests/Utils/BuildClientSchemaTest.php Co-Authored-By: Šimon Podlipský <simon@podlipsky.net> * Iterable * Revert change in Utils.php * Re-add minimal docs fix Co-authored-by: Šimon Podlipský <simon@podlipsky.net>
1 parent 5314bf6 commit 21c5996

File tree

7 files changed

+1609
-15
lines changed

7 files changed

+1609
-15
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
### Breaking (major): dropped deprecations
44
- dropped deprecated `GraphQL\Schema`. Use `GraphQL\Type\Schema`.
55

6+
### Breaking: change TypeKind constants
7+
The constants in `\GraphQL\Type\TypeKind` were partly renamed and their values
8+
have been changed to match their name instead of a numeric index.
9+
610
## Upgrade v0.12.x > v0.13.x
711

812
### Breaking (major): minimum supported version of PHP

src/Type/Introspection.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace GraphQL\Type;
66

77
use Exception;
8+
use GraphQL\GraphQL;
89
use GraphQL\Language\DirectiveLocation;
910
use GraphQL\Language\Printer;
1011
use GraphQL\Type\Definition\Directive;
@@ -184,6 +185,34 @@ public static function getTypes()
184185
];
185186
}
186187

188+
/**
189+
* Build an introspection query from a Schema
190+
*
191+
* Introspection is useful for utilities that care about type and field
192+
* relationships, but do not need to traverse through those relationships.
193+
*
194+
* This is the inverse of BuildClientSchema::build(). The primary use case is outside
195+
* of the server context, for instance when doing schema comparisons.
196+
*
197+
* Options:
198+
* - descriptions
199+
* Whether to include descriptions in the introspection result.
200+
* Default: true
201+
*
202+
* @param array<string, bool> $options
203+
*
204+
* @return array<string, array<mixed>>|null
205+
*/
206+
public static function fromSchema(Schema $schema, array $options = []) : ?array
207+
{
208+
$result = GraphQL::executeQuery(
209+
$schema,
210+
self::getIntrospectionQuery($options)
211+
);
212+
213+
return $result->data;
214+
}
215+
187216
public static function _schema()
188217
{
189218
if (! isset(self::$map['__Schema'])) {
@@ -263,7 +292,7 @@ public static function _type()
263292
'resolve' => static function (Type $type) {
264293
switch (true) {
265294
case $type instanceof ListOfType:
266-
return TypeKind::LIST_KIND;
295+
return TypeKind::LIST;
267296
case $type instanceof NonNull:
268297
return TypeKind::NON_NULL;
269298
case $type instanceof ScalarType:
@@ -275,7 +304,7 @@ public static function _type()
275304
case $type instanceof InputObjectType:
276305
return TypeKind::INPUT_OBJECT;
277306
case $type instanceof InterfaceType:
278-
return TypeKind::INTERFACE_KIND;
307+
return TypeKind::INTERFACE;
279308
case $type instanceof UnionType:
280309
return TypeKind::UNION;
281310
default:
@@ -408,7 +437,7 @@ public static function _typeKind()
408437
'description' => 'Indicates this type is an object. `fields` and `interfaces` are valid fields.',
409438
],
410439
'INTERFACE' => [
411-
'value' => TypeKind::INTERFACE_KIND,
440+
'value' => TypeKind::INTERFACE,
412441
'description' => 'Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.',
413442
],
414443
'UNION' => [
@@ -424,7 +453,7 @@ public static function _typeKind()
424453
'description' => 'Indicates this type is an input object. `inputFields` is a valid field.',
425454
],
426455
'LIST' => [
427-
'value' => TypeKind::LIST_KIND,
456+
'value' => TypeKind::LIST,
428457
'description' => 'Indicates this type is a list. `ofType` is a valid field.',
429458
],
430459
'NON_NULL' => [

src/Type/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private function collectAllTypes()
238238
*/
239239
public function getDirectives()
240240
{
241-
return $this->config->directives ?: GraphQL::getStandardDirectives();
241+
return $this->config->directives ?? GraphQL::getStandardDirectives();
242242
}
243243

244244
/**

src/Type/TypeKind.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
class TypeKind
88
{
9-
const SCALAR = 0;
10-
const OBJECT = 1;
11-
const INTERFACE_KIND = 2;
12-
const UNION = 3;
13-
const ENUM = 4;
14-
const INPUT_OBJECT = 5;
15-
const LIST_KIND = 6;
16-
const NON_NULL = 7;
9+
const SCALAR = 'SCALAR';
10+
const OBJECT = 'OBJECT';
11+
const INTERFACE = 'INTERFACE';
12+
const UNION = 'UNION';
13+
const ENUM = 'ENUM';
14+
const INPUT_OBJECT = 'INPUT_OBJECT';
15+
const LIST = 'LIST';
16+
const NON_NULL = 'NON_NULL';
1717
}

0 commit comments

Comments
 (0)