1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Butler\Graphql\Concerns; |
4
|
|
|
|
5
|
|
|
use Butler\Graphql\DataLoader; |
6
|
|
|
use Exception; |
7
|
|
|
use GraphQL\Error\DebugFlag; |
8
|
|
|
use GraphQL\Error\Error as GraphqlError; |
9
|
|
|
use GraphQL\Error\FormattedError; |
10
|
|
|
use GraphQL\Executor\ExecutionResult; |
11
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
12
|
|
|
use GraphQL\GraphQL; |
13
|
|
|
use GraphQL\Language\AST\DocumentNode; |
14
|
|
|
use GraphQL\Language\AST\InterfaceTypeDefinitionNode; |
15
|
|
|
use GraphQL\Language\AST\TypeDefinitionNode; |
16
|
|
|
use GraphQL\Language\AST\UnionTypeDefinitionNode; |
17
|
|
|
use GraphQL\Language\Parser; |
18
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
19
|
|
|
use GraphQL\Type\Schema; |
20
|
|
|
use GraphQL\Utils\BuildSchema; |
21
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
22
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
23
|
|
|
use Illuminate\Http\Request; |
24
|
|
|
use Illuminate\Support\Str; |
25
|
|
|
use Illuminate\Validation\ValidationException; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
27
|
|
|
|
28
|
|
|
use function Amp\call; |
29
|
|
|
|
30
|
|
|
trait HandlesGraphqlRequests |
31
|
|
|
{ |
32
|
|
|
private $classCache; |
33
|
|
|
private $namespaceCache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Invoke the Graphql request handler. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Http\Request $request |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
27 |
|
public function __invoke(Request $request) |
42
|
|
|
{ |
43
|
27 |
|
$this->classCache = []; |
44
|
27 |
|
$this->namespaceCache = null; |
45
|
|
|
|
46
|
27 |
|
$loader = app(DataLoader::class); |
47
|
|
|
|
48
|
27 |
|
$query = $request->input('query'); |
49
|
27 |
|
$variables = $request->input('variables'); |
50
|
27 |
|
$operationName = $request->input('operationName'); |
51
|
|
|
|
52
|
|
|
try { |
53
|
27 |
|
$schema = BuildSchema::build($this->schema(), [$this, 'decorateTypeConfig']); |
54
|
|
|
|
55
|
26 |
|
$source = Parser::parse($query); |
56
|
|
|
|
57
|
25 |
|
$this->beforeExecutionHook($schema, $source, $operationName, $variables); |
58
|
|
|
|
59
|
|
|
/** @var \GraphQL\Executor\ExecutionResult */ |
60
|
25 |
|
$result = null; |
61
|
|
|
|
62
|
25 |
|
GraphQL::promiseToExecute( |
63
|
25 |
|
app(PromiseAdapter::class), |
64
|
|
|
$schema, |
65
|
|
|
$source, |
66
|
25 |
|
null, // root |
67
|
25 |
|
compact('loader'), // context |
68
|
|
|
$variables, |
69
|
|
|
$operationName, |
70
|
25 |
|
[$this, 'resolveField'], |
71
|
25 |
|
null // validationRules |
72
|
25 |
|
)->then(function ($value) use (&$result) { |
73
|
25 |
|
$result = $value; |
74
|
25 |
|
}); |
75
|
|
|
|
76
|
25 |
|
$loader->run(); |
77
|
2 |
|
} catch (GraphqlError $e) { |
78
|
2 |
|
$result = new ExecutionResult(null, [$e]); |
79
|
|
|
} |
80
|
|
|
|
81
|
27 |
|
$result->setErrorFormatter([$this, 'errorFormatter']); |
82
|
|
|
|
83
|
27 |
|
return $this->decorateResponse($result->toArray($this->debugFlags())); |
84
|
|
|
} |
85
|
|
|
|
86
|
24 |
|
public function beforeExecutionHook( |
87
|
|
|
Schema $schema, |
88
|
|
|
DocumentNode $query, |
89
|
|
|
string $operationName = null, |
90
|
|
|
$variables = null |
91
|
|
|
): void { |
92
|
24 |
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
12 |
|
public function errorFormatter(GraphqlError $graphqlError) |
96
|
|
|
{ |
97
|
12 |
|
$formattedError = FormattedError::createFromException($graphqlError); |
98
|
12 |
|
$throwable = $graphqlError->getPrevious(); |
99
|
|
|
|
100
|
12 |
|
$this->reportException( |
101
|
12 |
|
$throwable instanceof Exception ? $throwable : $graphqlError |
|
|
|
|
102
|
|
|
); |
103
|
|
|
|
104
|
12 |
|
if ($throwable instanceof HttpException) { |
105
|
1 |
|
return array_merge($formattedError, [ |
106
|
1 |
|
'message' => $throwable->getMessage(), |
107
|
|
|
'extensions' => [ |
108
|
1 |
|
'category' => 'client', |
109
|
1 |
|
'code' => $throwable->getStatusCode(), |
110
|
|
|
], |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
11 |
|
if ($throwable instanceof ModelNotFoundException) { |
115
|
1 |
|
return array_merge($formattedError, [ |
116
|
1 |
|
'message' => class_basename($throwable->getModel()) . ' not found.', |
117
|
|
|
'extensions' => [ |
118
|
|
|
'category' => 'client', |
119
|
|
|
], |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
if ($throwable instanceof ValidationException) { |
124
|
1 |
|
return array_merge($formattedError, [ |
125
|
1 |
|
'message' => $throwable->getMessage(), |
126
|
|
|
'extensions' => [ |
127
|
1 |
|
'category' => 'validation', |
128
|
1 |
|
'validation' => $throwable->errors(), |
129
|
|
|
], |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
9 |
|
return $formattedError; |
134
|
|
|
} |
135
|
|
|
|
136
|
12 |
|
public function reportException(Exception $exception) |
137
|
|
|
{ |
138
|
12 |
|
app(ExceptionHandler::class)->report($exception); |
139
|
12 |
|
} |
140
|
|
|
|
141
|
25 |
|
public function schema() |
142
|
|
|
{ |
143
|
25 |
|
return file_get_contents($this->schemaPath()); |
144
|
|
|
} |
145
|
|
|
|
146
|
25 |
|
public function schemaPath() |
147
|
|
|
{ |
148
|
25 |
|
return config('butler.graphql.schema'); |
149
|
|
|
} |
150
|
|
|
|
151
|
26 |
|
public function decorateTypeConfig(array $config, TypeDefinitionNode $typeDefinitionNode) |
152
|
|
|
{ |
153
|
26 |
|
if ($this->shouldDecorateWithResolveType($typeDefinitionNode)) { |
154
|
23 |
|
$config['resolveType'] = [$this, 'resolveType']; |
155
|
|
|
} |
156
|
26 |
|
return $config; |
157
|
|
|
} |
158
|
|
|
|
159
|
26 |
|
protected function shouldDecorateWithResolveType(TypeDefinitionNode $typeDefinitionNode) |
160
|
|
|
{ |
161
|
26 |
|
return $typeDefinitionNode instanceof InterfaceTypeDefinitionNode |
162
|
26 |
|
|| $typeDefinitionNode instanceof UnionTypeDefinitionNode; |
163
|
|
|
} |
164
|
|
|
|
165
|
27 |
|
public function debugFlags() |
166
|
|
|
{ |
167
|
27 |
|
$flags = 0; |
168
|
27 |
|
if (config('butler.graphql.include_debug_message')) { |
169
|
8 |
|
$flags |= DebugFlag::INCLUDE_DEBUG_MESSAGE; |
170
|
|
|
} |
171
|
27 |
|
if (config('butler.graphql.include_trace')) { |
172
|
3 |
|
$flags |= DebugFlag::INCLUDE_TRACE; |
173
|
|
|
} |
174
|
27 |
|
return $flags; |
175
|
|
|
} |
176
|
|
|
|
177
|
24 |
|
public function resolveField($source, $args, $context, ResolveInfo $info) |
178
|
|
|
{ |
179
|
24 |
|
$field = $this->fieldFromResolver($source, $args, $context, $info) |
180
|
10 |
|
?? $this->fieldFromArray($source, $args, $context, $info) |
181
|
15 |
|
?? $this->fieldFromObject($source, $args, $context, $info); |
182
|
|
|
|
183
|
15 |
|
return call(static function () use ($field, $source, $args, $context, $info) { |
184
|
15 |
|
return $field instanceof \Closure |
185
|
1 |
|
? $field($source, $args, $context, $info) |
186
|
15 |
|
: $field; |
187
|
15 |
|
}); |
188
|
|
|
} |
189
|
|
|
|
190
|
4 |
|
public function resolveType($source, $context, ResolveInfo $info) |
191
|
|
|
{ |
192
|
4 |
|
return $this->typeFromArray($source, $context, $info) |
193
|
4 |
|
?? $this->typeFromObject($source, $context, $info) |
194
|
4 |
|
?? $this->typeFromParentResolver($source, $context, $info) |
195
|
4 |
|
?? $this->typeFromBaseClass($source, $context, $info); |
196
|
|
|
} |
197
|
|
|
|
198
|
24 |
|
public function fieldFromResolver($source, $args, $context, ResolveInfo $info) |
199
|
|
|
{ |
200
|
24 |
|
$className = $this->resolveClassName($info); |
201
|
24 |
|
$methodName = $this->resolveFieldMethodName($info); |
202
|
|
|
|
203
|
24 |
|
if ($resolver = $this->make($className)) { |
204
|
23 |
|
if (method_exists($resolver, $methodName)) { |
205
|
23 |
|
return $resolver->{$methodName}($source, $args, $context, $info); |
206
|
|
|
} |
207
|
|
|
} |
208
|
10 |
|
} |
209
|
|
|
|
210
|
10 |
|
public function fieldFromArray($source, $args, $context, ResolveInfo $info) |
211
|
|
|
{ |
212
|
10 |
|
if (is_array($source) || $source instanceof \ArrayAccess) { |
213
|
8 |
|
return collect($this->propertyNames($info)) |
214
|
8 |
|
->map(function ($propertyName) use ($source) { |
215
|
8 |
|
return $source[$propertyName] ?? null; |
216
|
8 |
|
}) |
217
|
8 |
|
->reject(function ($value) { |
218
|
8 |
|
return is_null($value); |
219
|
8 |
|
}) |
220
|
8 |
|
->first(); |
221
|
|
|
} |
222
|
7 |
|
} |
223
|
|
|
|
224
|
7 |
|
public function fieldFromObject($source, $args, $context, ResolveInfo $info) |
225
|
|
|
{ |
226
|
7 |
|
if (is_object($source)) { |
227
|
7 |
|
return collect($this->propertyNames($info)) |
228
|
7 |
|
->map(function ($propertyName) use ($source) { |
229
|
7 |
|
return $source->{$propertyName} ?? null; |
230
|
7 |
|
}) |
231
|
7 |
|
->reject(function ($value) { |
232
|
7 |
|
return is_null($value); |
233
|
7 |
|
}) |
234
|
7 |
|
->first(); |
235
|
|
|
} |
236
|
1 |
|
} |
237
|
|
|
|
238
|
4 |
|
public function typeFromArray($source, $context, ResolveInfo $info) |
239
|
|
|
{ |
240
|
4 |
|
if (is_array($source) || $source instanceof \ArrayAccess) { |
241
|
4 |
|
return $source['__typename'] ?? null; |
242
|
|
|
} |
243
|
2 |
|
} |
244
|
|
|
|
245
|
4 |
|
public function typeFromObject($source, $context, ResolveInfo $info) |
246
|
|
|
{ |
247
|
4 |
|
if (is_object($source)) { |
248
|
2 |
|
return $source->__typename ?? null; |
249
|
|
|
} |
250
|
4 |
|
} |
251
|
|
|
|
252
|
4 |
|
public function typeFromParentResolver($source, $context, ResolveInfo $info) |
253
|
|
|
{ |
254
|
4 |
|
$className = $this->resolveClassName($info); |
255
|
4 |
|
$methodName = $this->resolveTypeMethodName($info); |
256
|
|
|
|
257
|
4 |
|
if ($resolver = $this->make($className)) { |
258
|
4 |
|
if (method_exists($resolver, $methodName)) { |
259
|
4 |
|
return $resolver->{$methodName}($source, $context, $info); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
public function typeFromBaseClass($source, $context, ResolveInfo $info) |
265
|
|
|
{ |
266
|
1 |
|
if (is_object($source)) { |
267
|
1 |
|
return class_basename($source); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
10 |
|
public function propertyNames(ResolveInfo $info): array |
272
|
|
|
{ |
273
|
10 |
|
return collect([ |
274
|
10 |
|
Str::snake($info->fieldName), |
275
|
10 |
|
Str::camel($info->fieldName), |
276
|
10 |
|
Str::kebab(Str::camel($info->fieldName)), |
277
|
10 |
|
])->unique()->toArray(); |
278
|
|
|
} |
279
|
|
|
|
280
|
24 |
|
protected function resolveClassName(ResolveInfo $info): string |
281
|
|
|
{ |
282
|
24 |
|
if ($info->parentType->name === 'Query') { |
283
|
23 |
|
return $this->queriesNamespace() . Str::studly($info->fieldName); |
284
|
|
|
} |
285
|
|
|
|
286
|
11 |
|
if ($info->parentType->name === 'Mutation') { |
287
|
1 |
|
return $this->mutationsNamespace() . Str::studly($info->fieldName); |
288
|
|
|
} |
289
|
|
|
|
290
|
11 |
|
return $this->typesNamespace() . Str::studly($info->parentType->name); |
291
|
|
|
} |
292
|
|
|
|
293
|
24 |
|
public function resolveFieldMethodName(ResolveInfo $info): string |
294
|
|
|
{ |
295
|
24 |
|
if (in_array($info->parentType->name, ['Query', 'Mutation'])) { |
296
|
24 |
|
return '__invoke'; |
297
|
|
|
} |
298
|
|
|
|
299
|
11 |
|
return Str::camel($info->fieldName); |
300
|
|
|
} |
301
|
|
|
|
302
|
4 |
|
public function resolveTypeMethodName(ResolveInfo $info): string |
303
|
|
|
{ |
304
|
4 |
|
if (in_array($info->parentType->name, ['Query', 'Mutation'])) { |
305
|
2 |
|
return 'resolveType'; |
306
|
|
|
} |
307
|
|
|
|
308
|
2 |
|
return 'resolveTypeFor' . ucfirst(Str::camel($info->fieldName)); |
309
|
|
|
} |
310
|
|
|
|
311
|
24 |
|
public function namespace(): string |
312
|
|
|
{ |
313
|
24 |
|
return $this->namespaceCache ?? $this->namespaceCache = config('butler.graphql.namespace'); |
314
|
|
|
} |
315
|
|
|
|
316
|
23 |
|
public function queriesNamespace(): string |
317
|
|
|
{ |
318
|
23 |
|
return $this->namespace() . 'Queries\\'; |
319
|
|
|
} |
320
|
|
|
|
321
|
1 |
|
public function mutationsNamespace(): string |
322
|
|
|
{ |
323
|
1 |
|
return $this->namespace() . 'Mutations\\'; |
324
|
|
|
} |
325
|
|
|
|
326
|
11 |
|
public function typesNamespace(): string |
327
|
|
|
{ |
328
|
11 |
|
return $this->namespace() . 'Types\\'; |
329
|
|
|
} |
330
|
|
|
|
331
|
27 |
|
public function decorateResponse(array $data): array |
332
|
|
|
{ |
333
|
27 |
|
if (app()->bound('debugbar') && app('debugbar')->isEnabled()) { |
|
|
|
|
334
|
1 |
|
$data['debug'] = app('debugbar')->getData(); |
|
|
|
|
335
|
|
|
} |
336
|
27 |
|
return $data; |
337
|
|
|
} |
338
|
|
|
|
339
|
24 |
|
protected function make(string $className) |
340
|
|
|
{ |
341
|
24 |
|
if (array_key_exists($className, $this->classCache)) { |
342
|
10 |
|
return $this->classCache[$className]; |
343
|
|
|
} |
344
|
|
|
|
345
|
24 |
|
$class = app()->has($className) || class_exists($className) |
346
|
24 |
|
? app($className) |
347
|
23 |
|
: null; |
348
|
|
|
|
349
|
23 |
|
return $this->classCache[$className] = $class; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|