This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
GraphQL.php
370 lines (326 loc) · 11.3 KB
/
GraphQL.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
namespace Digia\GraphQL;
use Digia\GraphQL\Error\Handler\ErrorHandlerInterface;
use Digia\GraphQL\Error\InvariantException;
use Digia\GraphQL\Execution\ExecutionInterface;
use Digia\GraphQL\Execution\ExecutionProvider;
use Digia\GraphQL\Execution\ExecutionResult;
use Digia\GraphQL\Language\LanguageProvider;
use Digia\GraphQL\Language\Node\DocumentNode;
use Digia\GraphQL\Language\Node\NodeInterface;
use Digia\GraphQL\Language\Node\TypeNodeInterface;
use Digia\GraphQL\Language\Node\ValueNodeInterface;
use Digia\GraphQL\Language\NodePrinterInterface;
use Digia\GraphQL\Language\ParserInterface;
use Digia\GraphQL\Language\Source;
use Digia\GraphQL\Language\SyntaxErrorException;
use Digia\GraphQL\Schema\Building\SchemaBuilderInterface;
use Digia\GraphQL\Schema\Building\SchemaBuildingProvider;
use Digia\GraphQL\Schema\Extension\SchemaExtenderInterface;
use Digia\GraphQL\Schema\Extension\SchemaExtensionProvider;
use Digia\GraphQL\Schema\Resolver\ResolverRegistry;
use Digia\GraphQL\Schema\Resolver\ResolverRegistryInterface;
use Digia\GraphQL\Schema\Schema;
use Digia\GraphQL\Schema\Validation\SchemaValidationProvider;
use Digia\GraphQL\Schema\Validation\SchemaValidatorInterface;
use Digia\GraphQL\Type\CoercerProvider;
use Digia\GraphQL\Type\DirectivesProvider;
use Digia\GraphQL\Type\IntrospectionProvider;
use Digia\GraphQL\Type\ScalarTypesProvider;
use Digia\GraphQL\Validation\RulesProvider;
use Digia\GraphQL\Validation\ValidationProvider;
use Digia\GraphQL\Validation\ValidatorInterface;
use League\Container\Container;
class GraphQL
{
public const BOOLEAN = 'GraphQLBoolean';
public const FLOAT = 'GraphQLFloat';
public const INT = 'GraphQLInt';
public const ID = 'GraphQLID';
public const STRING = 'GraphQLString';
public const DEPRECATED_DIRECTIVE = 'GraphQLDeprecatedDirective';
public const INCLUDE_DIRECTIVE = 'GraphQLIncludeDirective';
public const SKIP_DIRECTIVE = 'GraphQLSkipDirective';
public const SCHEMA_INTROSPECTION = '__Schema';
public const DIRECTIVE_INTROSPECTION = '__Directive';
public const DIRECTIVE_LOCATION_INTROSPECTION = '__DirectiveLocation';
public const TYPE_INTROSPECTION = '__Type';
public const FIELD_INTROSPECTION = '__Field';
public const INPUT_VALUE_INTROSPECTION = '__InputValue';
public const ENUM_VALUE_INTROSPECTION = '__EnumValue';
public const TYPE_KIND_INTROSPECTION = '__TypeKind';
public const SCHEMA_META_FIELD_DEFINITION = 'SchemaMetaFieldDefinition';
public const TYPE_META_FIELD_DEFINITION = 'TypeMetaFieldDefinition';
public const TYPE_NAME_META_FIELD_DEFINITION = 'TypeNameMetaFieldDefinition';
/**
* @var array
*/
private static $providers = [
LanguageProvider::class,
SchemaBuildingProvider::class,
SchemaExtensionProvider::class,
SchemaValidationProvider::class,
CoercerProvider::class,
IntrospectionProvider::class,
ScalarTypesProvider::class,
DirectivesProvider::class,
RulesProvider::class,
ValidationProvider::class,
ExecutionProvider::class,
];
/**
* @var GraphQL
*/
private static $instance;
/**
* @var Container
*/
protected $container;
/**
* GraphQL constructor.
*/
private function __construct()
{
$container = new Container();
$this->registerProviders($container);
$this->container = $container;
}
/**
* @return GraphQL
*/
public static function getInstance(): self
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param string $id
* @return mixed
*/
public static function make(string $id)
{
return static::getInstance()
->getContainer()
->get($id);
}
/**
* @param Source $source
* @param array|ResolverRegistryInterface $resolverRegistry
* @param array $options
* @return Schema
*/
public static function buildSchema(Source $source, $resolverRegistry, array $options = []): Schema
{
/** @var SchemaBuilderInterface $schemaBuilder */
$schemaBuilder = static::make(SchemaBuilderInterface::class);
return $schemaBuilder->build(
static::parse($source, $options),
$resolverRegistry instanceof ResolverRegistryInterface
? $resolverRegistry
: new ResolverRegistry($resolverRegistry),
$options
);
}
/**
* @param Schema $schema
* @param Source $source
* @param array|ResolverRegistryInterface $resolverRegistry
* @param array $options
* @return Schema
*/
public static function extendSchema(
Schema $schema,
Source $source,
$resolverRegistry,
array $options = []
): Schema {
/** @var SchemaExtenderInterface $schemaExtender */
$schemaExtender = static::make(SchemaExtenderInterface::class);
return $schemaExtender->extend(
$schema,
static::parse($source, $options),
$resolverRegistry instanceof ResolverRegistryInterface
? $resolverRegistry
: new ResolverRegistry($resolverRegistry),
$options
);
}
/**
* @param Schema $schema
* @return array
*/
public static function validateSchema(Schema $schema): array
{
/** @var SchemaValidatorInterface $schemaValidator */
$schemaValidator = static::make(SchemaValidatorInterface::class);
return $schemaValidator->validate($schema);
}
/**
* @param Source $source
* @param array $options
* @return DocumentNode
*/
public static function parse(Source $source, array $options = []): DocumentNode
{
/** @var ParserInterface $parser */
$parser = static::make(ParserInterface::class);
return $parser->parse($source, $options);
}
/**
* @param Source $source
* @param array $options
* @return ValueNodeInterface
*/
public static function parseValue(Source $source, array $options = []): ValueNodeInterface
{
/** @var ParserInterface $parser */
$parser = static::make(ParserInterface::class);
return $parser->parseValue($source, $options);
}
/**
* @param Source $source
* @param array $options
* @return TypeNodeInterface
*/
public static function parseType(Source $source, array $options = []): TypeNodeInterface
{
/** @var ParserInterface $parser */
$parser = static::make(ParserInterface::class);
return $parser->parseType($source, $options);
}
/**
* @param Schema $schema
* @param DocumentNode $document
* @return array
*/
public static function validate(Schema $schema, DocumentNode $document): array
{
/** @var ValidatorInterface $validator */
$validator = static::make(ValidatorInterface::class);
return $validator->validate($schema, $document);
}
/**
* @param Schema $schema
* @param DocumentNode $document
* @param mixed $rootValue
* @param mixed $contextValue
* @param array $variableValues
* @param string|null $operationName
* @param callable|null $fieldResolver
* @param ErrorHandlerInterface|null $errorHandler
* @return ExecutionResult
*/
public static function execute(
Schema $schema,
DocumentNode $document,
$rootValue = null,
$contextValue = null,
array $variableValues = [],
$operationName = null,
callable $fieldResolver = null,
?ErrorHandlerInterface $errorHandler = null
): ExecutionResult {
/** @var ExecutionInterface $execution */
$execution = static::make(ExecutionInterface::class);
return $execution->execute(
$schema,
$document,
$rootValue,
$contextValue,
$variableValues,
$operationName,
$fieldResolver,
$errorHandler
);
}
/**
* @param Schema $schema
* @param string $source
* @param mixed $rootValue
* @param mixed $contextValue
* @param array $variableValues
* @param null|string $operationName
* @param callable|null $fieldResolver
* @param ErrorHandlerInterface|null $errorHandler
* @return ExecutionResult
* @throws InvariantException
* @throws SyntaxErrorException
*/
public static function process(
Schema $schema,
string $source,
$rootValue = null,
$contextValue = null,
array $variableValues = [],
?string $operationName = null,
?callable $fieldResolver = null,
?ErrorHandlerInterface $errorHandler = null
): ExecutionResult {
$schemaValidationErrors = validateSchema($schema);
if (!empty($schemaValidationErrors)) {
if (null !== $errorHandler) {
foreach ($schemaValidationErrors as $schemaValidationError) {
$errorHandler->handleError($schemaValidationError);
}
}
return new ExecutionResult(null, $schemaValidationErrors);
}
try {
$document = parse($source);
} catch (SyntaxErrorException $error) {
if (null !== $errorHandler) {
$errorHandler->handleError($error);
}
return new ExecutionResult(null, [$error]);
}
$validationErrors = validate($schema, $document);
if (!empty($validationErrors)) {
if (null !== $errorHandler) {
foreach ($validationErrors as $validationError) {
$errorHandler->handleError($validationError);
}
}
return new ExecutionResult(null, $validationErrors);
}
return execute(
$schema,
$document,
$rootValue,
$contextValue,
$variableValues,
$operationName,
$fieldResolver,
$errorHandler
);
}
/**
* @param NodeInterface $node
* @return string
*/
public static function print(NodeInterface $node): string
{
/** @var NodePrinterInterface $nodePrinter */
$nodePrinter = static::make(NodePrinterInterface::class);
return $nodePrinter->print($node);
}
/**
* @return Container
*/
public function getContainer(): Container
{
return $this->container;
}
/**
* Registers the service provides with the container.
*
* @param Container $container
*/
protected function registerProviders(Container $container): void
{
foreach (self::$providers as $className) {
$container->addServiceProvider($className);
}
}
}