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