-
Notifications
You must be signed in to change notification settings - Fork 705
Expand file tree
/
Copy pathElementQueryConditionBuilder.php
More file actions
524 lines (430 loc) · 22.5 KB
/
Copy pathElementQueryConditionBuilder.php
File metadata and controls
524 lines (430 loc) · 22.5 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
declare(strict_types=1);
namespace CraftCms\Cms\Gql;
use ArrayObject;
use CraftCms\Cms\Component\Component;
use CraftCms\Cms\Element\Data\EagerLoadPlan;
use CraftCms\Cms\Element\Element;
use CraftCms\Cms\Field\Assets as AssetField;
use CraftCms\Cms\Field\BaseRelationField;
use CraftCms\Cms\Field\Contracts\EagerLoadingFieldInterface;
use CraftCms\Cms\Field\Contracts\FieldInterface;
use CraftCms\Cms\Field\Entries as EntryField;
use CraftCms\Cms\Field\Field;
use CraftCms\Cms\Field\Users as UserField;
use CraftCms\Cms\Gql\Contracts\GqlInlineFragmentFieldInterface;
use CraftCms\Cms\Gql\Events\RegisterGqlEagerLoadableFields;
use CraftCms\Cms\Gql\Gql as GqlService;
use CraftCms\Cms\Gql\Interfaces\Elements\Asset as AssetInterface;
use CraftCms\Cms\Support\Arr;
use CraftCms\Cms\Support\Facades\Fields;
use CraftCms\Cms\Support\Str;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\ObjectFieldNode;
use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use InvalidArgumentException;
class ElementQueryConditionBuilder extends Component
{
public const string LOCALIZED_NODENAME = 'localized';
private ?ResolveInfo $_resolveInfo;
private ArgumentManager $_argumentManager;
private array $_fragments;
private array $_eagerLoadableFieldsByContext = [];
private array $_transformableAssetProperties = ['url', 'width', 'height'];
private array $_additionalEagerLoadableNodes;
public function __construct($config = [])
{
$this->_resolveInfo = Arr::pull($config, 'resolveInfo');
if ($this->_resolveInfo) {
$this->_fragments = $this->_resolveInfo->fragments;
}
parent::__construct($config);
// Cache all eager-loadable fields by context
$allFields = Fields::getAllFields(false);
foreach ($allFields as $field) {
if ($field instanceof EagerLoadingFieldInterface) {
/** @var EagerLoadingFieldInterface&Field $field */
$this->_eagerLoadableFieldsByContext[$field->context][$field->handle] = $field;
}
}
}
public function setResolveInfo(ResolveInfo $resolveInfo): void
{
$this->_resolveInfo = $resolveInfo;
$this->_fragments = $this->_resolveInfo->fragments;
}
public function setArgumentManager(ArgumentManager $argumentManager): void
{
$this->_argumentManager = $argumentManager;
}
/**
* Extract the query conditions based on the resolve information passed in the constructor.
* Returns an array of [methodName => parameters] to be called on the element query.
*
* @param FieldInterface|null $startingParentField the starting parent field for the extraction, if any
*/
public function extractQueryConditions(?FieldInterface $startingParentField = null): array
{
/** @var ArrayObject $fieldNodes */
$fieldNodes = $this->_resolveInfo->fieldNodes;
if ($fieldNodes->count() === 0 || empty($fieldNodes[0])) {
return [];
}
$startingNode = $fieldNodes[0];
$rootPlan = new EagerLoadPlan;
// Load up all eager loading rules.
$extractedConditions = [
'with' => $this->_traverseAndBuildPlans($startingNode, $rootPlan, $startingParentField, null, $startingParentField ? $startingParentField->context : 'global'),
];
if (! empty($rootPlan->criteria['withTransforms'])) {
$extractedConditions['withTransforms'] = $rootPlan->criteria['withTransforms'];
}
return $extractedConditions;
}
/**
* @param ArgumentNode[]|NodeList $argumentNodes
*/
private function _extractArguments(NodeList|array $argumentNodes): array
{
$arguments = [];
foreach ($argumentNodes as $argumentNode) {
$arguments[$argumentNode->name->value] = $this->_extractArgumentValue($argumentNode);
}
return $arguments;
}
private function _extractArgumentValue(Node $argumentNode): mixed
{
// Deal with a raw object value.
if ($argumentNode->kind === 'ObjectValue') {
/** @var ObjectValueNode $argumentNode */
$extractedValue = [];
foreach ($argumentNode->fields as $fieldNode) {
$extractedValue[$fieldNode->name->value] = $this->_extractArgumentValue($fieldNode);
}
return $extractedValue;
}
if (in_array($argumentNode->kind, ['Argument', 'Variable', 'ListValue', 'ObjectField'], true)) {
/** @var ArgumentNode|VariableNode|ListValueNode|ObjectFieldNode $argumentNode */
$argumentNodeValue = $argumentNode->value;
switch ($argumentNodeValue->kind) {
case 'Variable':
return $this->_resolveInfo->variableValues[$argumentNodeValue->name->value];
case 'ListValue':
$extractedValue = [];
foreach ($argumentNodeValue->values as $value) {
$extractedValue[] = $this->_extractArgumentValue($value);
}
return $extractedValue;
case 'ObjectValue':
$extractedValue = [];
foreach ($argumentNodeValue->fields as $fieldNode) {
$extractedValue[$fieldNode->name->value] = $this->_extractArgumentValue($fieldNode);
}
return $extractedValue;
case 'NullValue':
return null;
default:
return $argumentNodeValue->value;
}
}
$value = $argumentNode->value ?? null;
return $argumentNode->kind === 'IntValue' ? (int) $value : $value;
}
private function _isAdditionalEagerLoadableNode(string $nodeName, mixed $parentField): bool
{
$nodeList = $this->_getKnownSpecialEagerLoadNodes();
if (isset($nodeList[$nodeName])) {
// Top level - anything goes
if ($parentField === null) {
return true;
}
foreach ($nodeList[$nodeName] as $key => $value) {
if ($key === '*' || $value === '*') {
return true;
}
if (is_string($value) && is_a($parentField, $value)) {
return true;
}
}
}
return false;
}
private function _canSpecialFieldBeAliased(string $nodeName): bool
{
$nodeList = $this->_getKnownSpecialEagerLoadNodes();
if (isset($nodeList[$nodeName])) {
return $nodeList[$nodeName]['canBeAliased'] ?? true;
}
return false;
}
private function _getKnownSpecialEagerLoadNodes(): array
{
if (! isset($this->_additionalEagerLoadableNodes)) {
$list = [
'photo' => [UserField::class, 'canBeAliased' => false],
'addresses' => [UserField::class, 'canBeAliased' => true],
'author' => [EntryField::class, 'canBeAliased' => false],
'uploader' => [AssetField::class, 'canBeAliased' => false],
'parent' => [BaseRelationField::class, 'canBeAliased' => false],
'ancestors' => [BaseRelationField::class, 'canBeAliased' => false],
'children' => [BaseRelationField::class, 'canBeAliased' => true],
'descendants' => [BaseRelationField::class, 'canBeAliased' => false],
'currentRevision' => [BaseRelationField::class, 'canBeAliased' => false],
'draftCreator' => [BaseRelationField::class, 'canBeAliased' => false],
'drafts' => [BaseRelationField::class, 'canBeAliased' => false],
'revisions' => [BaseRelationField::class, 'canBeAliased' => false],
'revisionCreator' => [BaseRelationField::class, 'canBeAliased' => false],
self::LOCALIZED_NODENAME => [EntryField::class],
];
event($event = new RegisterGqlEagerLoadableFields(fieldList: $list));
$this->_additionalEagerLoadableNodes = $event->fieldList;
}
return $this->_additionalEagerLoadableNodes;
}
private function _extractTransformDirectiveArguments(Node $node): array
{
$arguments = [];
$directives = $node->directives ?? [];
foreach ($directives as $directive) {
if ($directive->name->value === 'transform') {
$arguments = $this->_extractArguments($directive->arguments ?? []);
break;
}
}
return $arguments;
}
private function _prepareTransformArguments(array $arguments): array
{
if (empty($arguments)) {
return [];
}
return [GqlHelper::prepareTransformArguments($arguments)];
}
private function _isInsideAssetQuery(): bool
{
$namedType = Type::getNamedType($this->_resolveInfo->returnType);
return $namedType !== null && $namedType->name() === AssetInterface::getName();
}
/**
* Traverse child nodes of a GraphQL query formed as an AST.
*
* This method traverses all the child descendant nodes recursively for a GraphQL query AST node,
* keeping track of where in the tree it currently resides to correctly build the `with` clause
* for the resulting element query.
*
* @param Node $parentNode the parent node being traversed.
* @param EagerLoadPlan $parentPlan The parent eager-loading plan
* @param FieldInterface|null $parentField the current parent field, that we are in.
* @param Node|null $wrappingFragment the wrapping fragment node, if any
* @param string $context the context in which to search fields
*/
private function _traverseAndBuildPlans(Node $parentNode, EagerLoadPlan $parentPlan, ?FieldInterface $parentField = null, ?Node $wrappingFragment = null, string $context = 'global'): array
{
$subNodes = $parentNode->selectionSet->selections ?? [];
$plans = [];
$rootOfAssetQuery = $parentField === null && $this->_isInsideAssetQuery();
if ($rootOfAssetQuery) {
// If this is a root asset query that has transform directive defined
// We should eager-load transforms using the directive's arguments
$transformArguments = $this->_prepareTransformArguments($this->_extractTransformDirectiveArguments($parentNode));
if ($transformArguments) {
$parentPlan->criteria['withTransforms'] = $transformArguments;
}
}
$countedHandles = [];
// For each subnode that is a direct descendant
foreach ($subNodes as $subNode) {
$nodeName = $subNode->name->value ?? null;
// If that's a GraphQL field
if ($subNode instanceof FieldNode) {
/** @var FieldInterface|null $craftContentField */
$craftContentField = $this->_eagerLoadableFieldsByContext[$context][$nodeName] ?? null;
$transformableAssetProperty = ($rootOfAssetQuery || $parentField instanceof AssetField) && in_array($nodeName, $this->_transformableAssetProperties, true);
$isAssetField = $craftContentField instanceof AssetField;
$isSpecialField = $this->_isAdditionalEagerLoadableNode((string) $nodeName, $parentField);
$canBeAliased = ! $isSpecialField || $this->_canSpecialFieldBeAliased((string) $nodeName);
$possibleTransforms = $transformableAssetProperty || $isAssetField;
$otherEagerLoadableNode = $nodeName === GqlService::GRAPHQL_COUNT_FIELD;
// That is a Craft field that can be eager-loaded or is a special eager-loadable field
if ($possibleTransforms || $craftContentField || $otherEagerLoadableNode || $isSpecialField) {
$plan = new EagerLoadPlan;
// Any arguments?
$arguments = $this->_extractArguments($subNode->arguments);
$transformEagerLoadArguments = [];
// If it's a place where we can have transforms defined, grab the possible values from directive as well
if ($isAssetField) {
$transformEagerLoadArguments = $this->_extractTransformDirectiveArguments($subNode);
}
if ($transformableAssetProperty) {
$transformEagerLoadArguments = array_merge_recursive($this->_extractTransformDirectiveArguments($subNode), $arguments);
// Also, these can't have any arguments.
$arguments = [];
}
// If we've found any eager-loadable transforms, massage the data.
if (! empty($transformEagerLoadArguments)) {
$transformEagerLoadArguments = $this->_prepareTransformArguments($transformEagerLoadArguments);
// If the property is transformable, then merge into the _parent_ plan.
if ($transformableAssetProperty) {
$parentPlan->criteria['withTransforms'] = array_merge_recursive($parentPlan->criteria['withTransforms'] ?? [], $transformEagerLoadArguments);
} else {
$plan->criteria['withTransforms'] = array_merge_recursive($plan->criteria['withTransforms'] ?? [], $transformEagerLoadArguments);
}
}
// If this a custom Craft content field
if ($craftContentField) {
/** @var EagerLoadingFieldInterface $craftContentField */
$additionalArguments = $craftContentField->getEagerLoadingGqlConditions();
// Load additional requirements enforced by schema, enforcing permissions to see content
if ($additionalArguments === null) {
// If `false` was returned, make sure nothing is returned by setting a constraint that always fails.
$arguments = ['id' => ['and', 1, 2]];
} else {
// Loop through what schema allows for this content type
foreach ($additionalArguments as $argumentName => $argumentValue) {
// If they also want to filter by field that is enforced by schema
if (isset($arguments[$argumentName])) {
if (! is_array($arguments[$argumentName])) {
$arguments[$argumentName] = [$arguments[$argumentName]];
}
// See what remains after we enforce the scope by schema.
$allowed = array_intersect($arguments[$argumentName], $argumentValue);
// If they wanted to filter by values that were not allowed by schema, make it impossible
if (empty($allowed)) {
$arguments = ['id' => ['and', 1, 2]];
break;
}
// Otherwise, allow the overlapping things.
$arguments[$argumentName] = $allowed;
} else {
// Otherwise, just add their filters to the list.
$arguments[$argumentName] = $argumentValue;
}
}
}
// For relational fields, prepare the arguments.
if ($craftContentField instanceof EagerLoadingFieldInterface) {
$arguments = $this->_argumentManager->prepareArguments($arguments);
}
}
// See if the field was aliased in the query
$alias = ($canBeAliased && ! (empty($subNode->alias)) && ! empty($subNode->alias->value)) ? $subNode->alias->value : null;
// If they're angling for the count field, alias it so each count field gets their own eager-load arguments.
if ($nodeName === GqlService::GRAPHQL_COUNT_FIELD) {
$countedHandles[] = $arguments['field'];
continue;
}
// Add this to the eager loading list.
if (! $transformableAssetProperty) {
$plan->handle = $nodeName;
$plan->alias = $alias ?: $nodeName;
/** @var InlineFragmentNode|FragmentDefinitionNode|null $wrappingFragment */
if ($wrappingFragment) {
$plan->when = function (Element $element) use ($wrappingFragment) {
$typeName = $wrappingFragment->typeCondition->name->value;
if (preg_match('/^(\w+)Interface$/', $typeName, $match)) {
return str_ends_with($element->getGqlTypeName(), "_{$match[1]}");
}
return $element->getGqlTypeName() === $typeName;
};
}
$plan->criteria = array_merge_recursive($plan->criteria, $this->_argumentManager->prepareArguments($arguments));
}
// If it has any more selections, build the plans recursively
if (! empty($subNode->selectionSet)) {
if ($craftContentField) {
// Relational fields should reset context to global.
if ($craftContentField instanceof BaseRelationField) {
$traverseContext = 'global';
} else {
$traverseContext = $craftContentField->context;
}
} else {
$traverseContext = $context;
}
$plan->nested = $this->_traverseAndBuildPlans($subNode, $plan, $nodeName === self::LOCALIZED_NODENAME ? $parentField : $craftContentField, $wrappingFragment, $traverseContext);
}
}
// If not, see if it's a fragment
} elseif ($subNode instanceof InlineFragmentNode || $subNode instanceof FragmentSpreadNode) {
$plan = new EagerLoadPlan;
// For named fragments, replace the node with the actual fragment.
if ($subNode instanceof FragmentSpreadNode) {
$subNode = $this->_fragments[$nodeName];
}
$wrappingFragment = $subNode;
$nodeName = $subNode->typeCondition->name->value;
// If we are inside a field that supports different subtypes, it should implement the appropriate interface
if ($parentField instanceof GqlInlineFragmentFieldInterface) {
// Get the Craft entity that correlates to the fragment
// Build the prefix, load the context and proceed in a recursive manner
try {
$gqlFragmentEntity = $parentField->getGqlFragmentEntityByName($nodeName);
$plan->nested = $this->_traverseAndBuildPlans($subNode, $plan, $parentField, $wrappingFragment, $gqlFragmentEntity->getFieldContext());
// Correct the handles and, maybe, aliases.
foreach ($plan->nested as $nestedPlan) {
$newHandle = Str::chopStart($gqlFragmentEntity->getEagerLoadingPrefix().':'.$nestedPlan->handle, ':');
if ($nestedPlan->handle === $nestedPlan->alias) {
$nestedPlan->alias = $newHandle;
}
$nestedPlan->handle = $newHandle;
}
// This is to be expected, depending on whether the fragment is targeted towards the field itself instead of its subtypes.
} catch (InvalidArgumentException) {
$plan->nested = $this->_traverseAndBuildPlans($subNode, $plan, $parentField, $wrappingFragment, $context);
}
// If we are not, just expand the fragment and traverse it as if on the same level in the query tree
} else {
$plan->nested = $this->_traverseAndBuildPlans($subNode, $plan, $parentField, $wrappingFragment, $context);
}
}
if (isset($plan)) {
if (! empty($plan->handle)) {
$plans[] = $plan;
} elseif (! empty($plan->nested)) {
// Unpack plans generated by parsing fragments.
foreach ($plan->nested as $nestedPlan) {
$plans[] = $nestedPlan;
}
}
unset($plan);
}
}
// For each required count
foreach ($countedHandles as $countedHandle) {
$foundPlan = false;
// Check if we can just flag an existing plan to load the count as well
foreach ($plans as $plan) {
if ($plan->handle === $countedHandle) {
$plan->count = true;
$foundPlan = true;
}
}
// If not, create a new plan.
if (! $foundPlan) {
$plans[] = new EagerLoadPlan(
handle: $countedHandle,
alias: $countedHandle,
count: true,
);
}
}
return $plans;
}
public function canNodeBeAliased(string $nodeName, $parentField = null): bool
{
if (! $this->_isAdditionalEagerLoadableNode($nodeName, $parentField)) {
return true;
}
return $this->_canSpecialFieldBeAliased($nodeName);
}
}