|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
6
|
|
|
use Digia\GraphQL\Error\SchemaValidationException; |
|
7
|
|
|
use Digia\GraphQL\Error\SyntaxErrorException; |
|
8
|
|
|
use Digia\GraphQL\Error\ValidationException; |
|
9
|
|
|
use Digia\GraphQL\Execution\ExecutionResult; |
|
10
|
|
|
use Digia\GraphQL\Language\Node\DocumentNode; |
|
11
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
|
12
|
|
|
use Digia\GraphQL\Language\Source; |
|
13
|
|
|
use Digia\GraphQL\Schema\Resolver\ResolverRegistryInterface; |
|
14
|
|
|
use Digia\GraphQL\Schema\SchemaInterface; |
|
15
|
|
|
use Digia\GraphQL\Util\SerializationInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string|Source $source |
|
19
|
|
|
* @param array|ResolverRegistryInterface $resolverRegistry |
|
20
|
|
|
* @param array $options |
|
21
|
|
|
* @return SchemaInterface |
|
22
|
|
|
* @throws InvariantException |
|
23
|
|
|
*/ |
|
24
|
|
|
function buildSchema($source, $resolverRegistry = [], array $options = []): SchemaInterface |
|
25
|
|
|
{ |
|
26
|
|
|
return GraphQL::buildSchema($source, $resolverRegistry, $options); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param SchemaInterface $schema |
|
31
|
|
|
* @param string|Source $source |
|
32
|
|
|
* @param array|ResolverRegistryInterface $resolverRegistry |
|
33
|
|
|
* @param array $options |
|
34
|
|
|
* @return SchemaInterface |
|
35
|
|
|
* @throws InvariantException |
|
36
|
|
|
*/ |
|
37
|
|
|
function extendSchema(SchemaInterface $schema, $source, $resolverRegistry = [], array $options = []): SchemaInterface |
|
38
|
|
|
{ |
|
39
|
|
|
return GraphQL::extendSchema($schema, $source, $resolverRegistry, $options); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param SchemaInterface $schema |
|
44
|
|
|
* @return SchemaValidationException[] |
|
45
|
|
|
*/ |
|
46
|
|
|
function validateSchema(SchemaInterface $schema): array |
|
47
|
|
|
{ |
|
48
|
|
|
return GraphQL::validateSchema($schema); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string|Source $source |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
* @return NodeInterface|DocumentNode|SerializationInterface |
|
55
|
|
|
* @throws InvariantException |
|
56
|
|
|
* @throws SyntaxErrorException |
|
57
|
|
|
*/ |
|
58
|
|
|
function parse($source, array $options = []): NodeInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return GraphQL::parse($source, $options); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string|Source $source |
|
65
|
|
|
* @param array $options |
|
66
|
|
|
* @return NodeInterface|SerializationInterface |
|
67
|
|
|
* @throws InvariantException |
|
68
|
|
|
* @throws SyntaxErrorException |
|
69
|
|
|
*/ |
|
70
|
|
|
function parseValue($source, array $options = []): NodeInterface |
|
71
|
|
|
{ |
|
72
|
|
|
return GraphQL::parseValue($source, $options); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string|Source $source |
|
77
|
|
|
* @param array $options |
|
78
|
|
|
* @return NodeInterface|SerializationInterface |
|
79
|
|
|
* @throws InvariantException |
|
80
|
|
|
* @throws SyntaxErrorException |
|
81
|
|
|
*/ |
|
82
|
|
|
function parseType($source, array $options = []): NodeInterface |
|
83
|
|
|
{ |
|
84
|
|
|
return GraphQL::parseType($source, $options); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param SchemaInterface $schema |
|
89
|
|
|
* @param DocumentNode $document |
|
90
|
|
|
* @return array|ValidationException[] |
|
91
|
|
|
*/ |
|
92
|
|
|
function validate(SchemaInterface $schema, DocumentNode $document): array |
|
93
|
|
|
{ |
|
94
|
|
|
return GraphQL::validate($schema, $document); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param SchemaInterface $schema |
|
99
|
|
|
* @param DocumentNode $document |
|
100
|
|
|
* @param mixed|null $rootValue |
|
101
|
|
|
* @param mixed|null $contextValue |
|
102
|
|
|
* @param array $variableValues |
|
103
|
|
|
* @param mixed|null $operationName |
|
104
|
|
|
* @param callable|null $fieldResolver |
|
105
|
|
|
* @return ExecutionResult |
|
106
|
|
|
*/ |
|
107
|
|
|
function execute( |
|
108
|
|
|
SchemaInterface $schema, |
|
109
|
|
|
DocumentNode $document, |
|
110
|
|
|
$rootValue = null, |
|
111
|
|
|
$contextValue = null, |
|
112
|
|
|
array $variableValues = [], |
|
113
|
|
|
$operationName = null, |
|
114
|
|
|
callable $fieldResolver = null |
|
115
|
|
|
): ExecutionResult { |
|
116
|
|
|
return GraphQL::execute( |
|
117
|
|
|
$schema, |
|
118
|
|
|
$document, |
|
119
|
|
|
$rootValue, |
|
120
|
|
|
$contextValue, |
|
121
|
|
|
$variableValues, |
|
122
|
|
|
$operationName, |
|
123
|
|
|
$fieldResolver |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param NodeInterface $node |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
function printNode(NodeInterface $node): string |
|
132
|
|
|
{ |
|
133
|
|
|
return GraphQL::print($node); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param SchemaInterface $schema |
|
138
|
|
|
* @param string $source |
|
139
|
|
|
* @param mixed|null $rootValue |
|
140
|
|
|
* @param mixed|null $contextValue |
|
141
|
|
|
* @param array $variableValues |
|
142
|
|
|
* @param mixed|null $operationName |
|
143
|
|
|
* @param callable|null $fieldResolver |
|
144
|
|
|
* @return ExecutionResult |
|
145
|
|
|
* @throws InvariantException |
|
146
|
|
|
*/ |
|
147
|
|
|
function graphql( |
|
148
|
|
|
SchemaInterface $schema, |
|
149
|
|
|
string $source, |
|
150
|
|
|
$rootValue = null, |
|
151
|
|
|
$contextValue = null, |
|
152
|
|
|
array $variableValues = [], |
|
153
|
|
|
$operationName = null, |
|
154
|
|
|
callable $fieldResolver = null |
|
155
|
|
|
): array { |
|
156
|
|
|
$schemaValidationErrors = validateSchema($schema); |
|
157
|
|
|
if (!empty($schemaValidationErrors)) { |
|
158
|
|
|
return (new ExecutionResult([], $schemaValidationErrors))->toArray(); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$document = null; |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
try { |
|
164
|
|
|
$document = parse($source); |
|
165
|
|
|
} catch (SyntaxErrorException $error) { |
|
166
|
|
|
return (new ExecutionResult([], [$error]))->toArray(); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$validationErrors = validate($schema, $document); |
|
170
|
|
|
if (!empty($validationErrors)) { |
|
171
|
|
|
return (new ExecutionResult([], $validationErrors))->toArray(); |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$result = execute( |
|
175
|
|
|
$schema, |
|
176
|
|
|
$document, |
|
177
|
|
|
$rootValue, |
|
178
|
|
|
$contextValue, |
|
179
|
|
|
$variableValues, |
|
180
|
|
|
$operationName, |
|
181
|
|
|
$fieldResolver |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
return $result->toArray(); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|