1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace GraphQL\Utils;
use GraphQL\Error\Error;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\Directive;
/**
* Build instance of `GraphQL\Type\Schema` out of type language definition (string or parsed AST)
* See [section in docs](type-system/type-language.md) for details.
*/
class BuildSchema
{
/**
* This takes the ast of a schema document produced by the parse function in
* GraphQL\Language\Parser.
*
* If no schema definition is provided, then it will look for types named Query
* and Mutation.
*
* Given that AST it constructs a GraphQL\Type\Schema. The resulting schema
* has no resolve methods, so execution will use default resolvers.
*
* Accepts options as a third argument:
*
* - commentDescriptions:
* Provide true to use preceding comments as the description.
*
*
* @api
* @param DocumentNode $ast
* @param callable $typeConfigDecorator
* @param array $options
* @return Schema
* @throws Error
*/
public static function buildAST(DocumentNode $ast, callable $typeConfigDecorator = null, array $options = [])
{
$builder = new self($ast, $typeConfigDecorator, $options);
return $builder->buildSchema();
}
private $ast;
private $nodeMap;
private $typeConfigDecorator;
private $options;
public function __construct(DocumentNode $ast, callable $typeConfigDecorator = null, array $options = [])
{
$this->ast = $ast;
$this->typeConfigDecorator = $typeConfigDecorator;
$this->options = $options;
}
public function buildSchema()
{
/** @var SchemaDefinitionNode $schemaDef */
$schemaDef = null;
$typeDefs = [];
$this->nodeMap = [];
$directiveDefs = [];
foreach ($this->ast->definitions as $d) {
switch ($d->kind) {
case NodeKind::SCHEMA_DEFINITION:
if ($schemaDef) {
throw new Error('Must provide only one schema definition.');
}
$schemaDef = $d;
break;
case NodeKind::SCALAR_TYPE_DEFINITION:
case NodeKind::OBJECT_TYPE_DEFINITION:
case NodeKind::INTERFACE_TYPE_DEFINITION:
case NodeKind::ENUM_TYPE_DEFINITION:
case NodeKind::UNION_TYPE_DEFINITION:
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
$typeName = $d->name->value;
if (!empty($this->nodeMap[$typeName])) {
throw new Error("Type \"$typeName\" was defined more than once.");
}
$typeDefs[] = $d;
$this->nodeMap[$typeName] = $d;
break;
case NodeKind::DIRECTIVE_DEFINITION:
$directiveDefs[] = $d;
break;
}
}
$operationTypes = $schemaDef
? $this->getOperationTypes($schemaDef)
: [
'query' => isset($this->nodeMap['Query']) ? 'Query' : null,
'mutation' => isset($this->nodeMap['Mutation']) ? 'Mutation' : null,
'subscription' => isset($this->nodeMap['Subscription']) ? 'Subscription' : null,
];
$defintionBuilder = new ASTDefinitionBuilder(
$this->nodeMap,
$this->options,
function($typeName) { throw new Error('Type "'. $typeName . '" not found in document.'); },
$this->typeConfigDecorator
);
$directives = array_map(function($def) use ($defintionBuilder) {
return $defintionBuilder->buildDirective($def);
}, $directiveDefs);
// If specified directives were not explicitly declared, add them.
$skip = array_reduce($directives, function ($hasSkip, $directive) {
return $hasSkip || $directive->name == 'skip';
});
if (!$skip) {
$directives[] = Directive::skipDirective();
}
$include = array_reduce($directives, function ($hasInclude, $directive) {
return $hasInclude || $directive->name == 'include';
});
if (!$include) {
$directives[] = Directive::includeDirective();
}
$deprecated = array_reduce($directives, function ($hasDeprecated, $directive) {
return $hasDeprecated || $directive->name == 'deprecated';
});
if (!$deprecated) {
$directives[] = Directive::deprecatedDirective();
}
// Note: While this could make early assertions to get the correctly
// typed values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
$schema = new Schema([
'query' => isset($operationTypes['query'])
? $defintionBuilder->buildType($operationTypes['query'])
: null,
'mutation' => isset($operationTypes['mutation'])
? $defintionBuilder->buildType($operationTypes['mutation'])
: null,
'subscription' => isset($operationTypes['subscription'])
? $defintionBuilder->buildType($operationTypes['subscription'])
: null,
'typeLoader' => function ($name) use ($defintionBuilder) {
return $defintionBuilder->buildType($name);
},
'directives' => $directives,
'astNode' => $schemaDef,
'types' => function () use ($defintionBuilder) {
$types = [];
foreach ($this->nodeMap as $name => $def) {
$types[] = $defintionBuilder->buildType($def->name->value);
}
return $types;
}
]);
return $schema;
}
/**
* @param SchemaDefinitionNode $schemaDef
* @return array
* @throws Error
*/
private function getOperationTypes($schemaDef)
{
$opTypes = [];
foreach ($schemaDef->operationTypes as $operationType) {
$typeName = $operationType->type->name->value;
$operation = $operationType->operation;
if (isset($opTypes[$operation])) {
throw new Error("Must provide only one $operation type in schema.");
}
if (!isset($this->nodeMap[$typeName])) {
throw new Error("Specified $operation type \"$typeName\" not found in document.");
}
$opTypes[$operation] = $typeName;
}
return $opTypes;
}
/**
* A helper function to build a GraphQLSchema directly from a source
* document.
*
* @api
* @param DocumentNode|Source|string $source
* @param callable $typeConfigDecorator
* @param array $options
* @return Schema
*/
public static function build($source, callable $typeConfigDecorator = null, array $options = [])
{
$doc = $source instanceof DocumentNode ? $source : Parser::parse($source);
return self::buildAST($doc, $typeConfigDecorator, $options);
}
}