1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Cache\CacheProvider; |
6
|
|
|
use Digia\GraphQL\Error\InvariantException; |
7
|
|
|
use Digia\GraphQL\Execution\ExecutionInterface; |
8
|
|
|
use Digia\GraphQL\Execution\ExecutionProvider; |
9
|
|
|
use Digia\GraphQL\Execution\ExecutionResult; |
10
|
|
|
use Digia\GraphQL\Language\LanguageProvider; |
11
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
12
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
13
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
14
|
|
|
use Digia\GraphQL\Language\Node\TypeNodeInterface; |
15
|
|
|
use Digia\GraphQL\Language\Node\ValueNodeInterface; |
16
|
|
|
use Digia\GraphQL\Language\ParserInterface; |
17
|
|
|
use Digia\GraphQL\Language\PrinterInterface; |
18
|
|
|
use Digia\GraphQL\Language\Source; |
19
|
|
|
use Digia\GraphQL\SchemaBuilder\SchemaBuilderInterface; |
20
|
|
|
use Digia\GraphQL\SchemaBuilder\SchemaBuilderProvider; |
21
|
|
|
use Digia\GraphQL\SchemaValidator\SchemaValidatorInterface; |
22
|
|
|
use Digia\GraphQL\SchemaValidator\SchemaValidatorProvider; |
23
|
|
|
use Digia\GraphQL\Type\CoercerProvider; |
24
|
|
|
use Digia\GraphQL\Type\DirectivesProvider; |
25
|
|
|
use Digia\GraphQL\Type\IntrospectionProvider; |
26
|
|
|
use Digia\GraphQL\Type\ScalarTypesProvider; |
27
|
|
|
use Digia\GraphQL\Type\SchemaInterface; |
28
|
|
|
use Digia\GraphQL\Util\UtilityProvider; |
29
|
|
|
use Digia\GraphQL\Validation\RulesProvider; |
30
|
|
|
use Digia\GraphQL\Validation\ValidationProvider; |
31
|
|
|
use Digia\GraphQL\Validation\ValidatorInterface; |
32
|
|
|
use League\Container\Container; |
33
|
|
|
use League\Container\ContainerInterface; |
34
|
|
|
|
35
|
|
|
class GraphQL |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
public const BOOLEAN = 'GraphQLBoolean'; |
39
|
|
|
public const FLOAT = 'GraphQLFloat'; |
40
|
|
|
public const INT = 'GraphQLInt'; |
41
|
|
|
public const ID = 'GraphQLID'; |
42
|
|
|
public const STRING = 'GraphQLString'; |
43
|
|
|
|
44
|
|
|
public const DEPRECATED_DIRECTIVE = 'GraphQLDeprecatedDirective'; |
45
|
|
|
public const INCLUDE_DIRECTIVE = 'GraphQLIncludeDirective'; |
46
|
|
|
public const SKIP_DIRECTIVE = 'GraphQLSkipDirective'; |
47
|
|
|
|
48
|
|
|
public const SCHEMA_INTROSPECTION = '__Schema'; |
49
|
|
|
public const DIRECTIVE_INTROSPECTION = '__Directive'; |
50
|
|
|
public const DIRECTIVE_LOCATION_INTROSPECTION = '__DirectiveLocation'; |
51
|
|
|
public const TYPE_INTROSPECTION = '__Type'; |
52
|
|
|
public const FIELD_INTROSPECTION = '__Field'; |
53
|
|
|
public const INPUT_VALUE_INTROSPECTION = '__InputValue'; |
54
|
|
|
public const ENUM_VALUE_INTROSPECTION = '__EnumValue'; |
55
|
|
|
public const TYPE_KIND_INTROSPECTION = '__TypeKind'; |
56
|
|
|
|
57
|
|
|
public const SCHEMA_META_FIELD_DEFINITION = 'SchemaMetaFieldDefinition'; |
58
|
|
|
public const TYPE_META_FIELD_DEFINITION = 'TypeMetaFieldDefinition'; |
59
|
|
|
public const TYPE_NAME_META_FIELD_DEFINITION = 'TypeNameMetaFieldDefinition'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private static $providers = [ |
65
|
|
|
CacheProvider::class, |
66
|
|
|
LanguageProvider::class, |
67
|
|
|
SchemaBuilderProvider::class, |
68
|
|
|
SchemaValidatorProvider::class, |
69
|
|
|
CoercerProvider::class, |
70
|
|
|
IntrospectionProvider::class, |
71
|
|
|
ScalarTypesProvider::class, |
72
|
|
|
DirectivesProvider::class, |
73
|
|
|
RulesProvider::class, |
74
|
|
|
ValidationProvider::class, |
75
|
|
|
ExecutionProvider::class, |
76
|
|
|
UtilityProvider::class, |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var GraphQL |
81
|
|
|
*/ |
82
|
|
|
private static $instance; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var Container |
86
|
|
|
*/ |
87
|
|
|
protected $container; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* GraphQL constructor. |
91
|
|
|
*/ |
92
|
|
|
private function __construct() |
93
|
|
|
{ |
94
|
|
|
$container = new Container(); |
95
|
|
|
|
96
|
|
|
$this->registerProviders($container); |
97
|
|
|
|
98
|
|
|
$this->container = $container; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return GraphQL |
103
|
|
|
*/ |
104
|
|
|
public static function getInstance(): self |
105
|
|
|
{ |
106
|
|
|
if (null === static::$instance) { |
|
|
|
|
107
|
|
|
static::$instance = new static(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return static::$instance; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $id |
115
|
|
|
* @param array $args |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public static function make(string $id, array $args = []) |
119
|
|
|
{ |
120
|
|
|
return static::getInstance() |
121
|
|
|
->getContainer() |
122
|
|
|
->get($id, $args); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string|Source $source |
127
|
|
|
* @param array $resolverMaps |
128
|
|
|
* @param array $options |
129
|
|
|
* @return SchemaInterface |
130
|
|
|
* @throws InvariantException |
131
|
|
|
*/ |
132
|
|
|
public static function buildSchema($source, array $resolverMaps = [], array $options = []): SchemaInterface |
133
|
|
|
{ |
134
|
|
|
return static::make(SchemaBuilderInterface::class) |
135
|
|
|
->build( |
136
|
|
|
static::parse($source, $options), |
137
|
|
|
$resolverMaps, |
138
|
|
|
$options |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param SchemaInterface $schema |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public static function validateSchema(SchemaInterface $schema): array |
147
|
|
|
{ |
148
|
|
|
return static::make(SchemaValidatorInterface::class) |
149
|
|
|
->validate($schema); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string|Source $source |
154
|
|
|
* @param array $options |
155
|
|
|
* @return DocumentNode |
156
|
|
|
* @throws InvariantException |
157
|
|
|
*/ |
158
|
|
|
public static function parse($source, array $options = []): DocumentNode |
159
|
|
|
{ |
160
|
|
|
return static::make(ParserInterface::class) |
161
|
|
|
->parse(static::lex($source, $options)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string|Source $source |
166
|
|
|
* @param array $options |
167
|
|
|
* @return ValueNodeInterface |
168
|
|
|
* @throws InvariantException |
169
|
|
|
*/ |
170
|
|
|
public static function parseValue($source, array $options = []): ValueNodeInterface |
171
|
|
|
{ |
172
|
|
|
return static::make(ParserInterface::class) |
173
|
|
|
->parseValue(static::lex($source, $options)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string|Source $source |
178
|
|
|
* @param array $options |
179
|
|
|
* @return TypeNodeInterface |
180
|
|
|
* @throws InvariantException |
181
|
|
|
*/ |
182
|
|
|
public static function parseType($source, array $options = []): TypeNodeInterface |
183
|
|
|
{ |
184
|
|
|
return static::make(ParserInterface::class) |
185
|
|
|
->parseType(static::lex($source, $options)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param SchemaInterface $schema |
190
|
|
|
* @param DocumentNode $document |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
public static function validate(SchemaInterface $schema, DocumentNode $document): array |
194
|
|
|
{ |
195
|
|
|
return static::make(ValidatorInterface::class) |
196
|
|
|
->validate($schema, $document); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param SchemaInterface $schema |
201
|
|
|
* @param DocumentNode $document |
202
|
|
|
* @param null $rootValue |
|
|
|
|
203
|
|
|
* @param null $contextValue |
204
|
|
|
* @param array $variableValues |
205
|
|
|
* @param null $operationName |
206
|
|
|
* @param callable|null $fieldResolver |
207
|
|
|
* @return ExecutionResult |
208
|
|
|
*/ |
209
|
|
|
public static function execute( |
210
|
|
|
SchemaInterface $schema, |
211
|
|
|
DocumentNode $document, |
212
|
|
|
$rootValue = null, |
213
|
|
|
$contextValue = null, |
214
|
|
|
array $variableValues = [], |
215
|
|
|
$operationName = null, |
216
|
|
|
callable $fieldResolver = null |
217
|
|
|
): ExecutionResult { |
218
|
|
|
return static::make(ExecutionInterface::class) |
219
|
|
|
->execute( |
220
|
|
|
$schema, |
221
|
|
|
$document, |
222
|
|
|
$rootValue, |
223
|
|
|
$contextValue, |
224
|
|
|
$variableValues, |
225
|
|
|
$operationName, |
226
|
|
|
$fieldResolver |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param NodeInterface $node |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public static function print(NodeInterface $node): string |
235
|
|
|
{ |
236
|
|
|
return static::make(PrinterInterface::class)->print($node); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string|Source $source |
241
|
|
|
* @param array $options |
242
|
|
|
* @return LexerInterface |
243
|
|
|
* @throws InvariantException |
244
|
|
|
*/ |
245
|
|
|
public static function lex($source, array $options = []): LexerInterface |
246
|
|
|
{ |
247
|
|
|
return static::make(LexerInterface::class) |
248
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
249
|
|
|
->setOptions($options); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return Container |
254
|
|
|
*/ |
255
|
|
|
public function getContainer(): Container |
256
|
|
|
{ |
257
|
|
|
return $this->container; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Registers the service provides with the container. |
262
|
|
|
*/ |
263
|
|
|
protected function registerProviders(ContainerInterface $container): void |
264
|
|
|
{ |
265
|
|
|
foreach (static::$providers as $className) { |
|
|
|
|
266
|
|
|
$container->addServiceProvider($className); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|