Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager-load fields based on fields actually present in query #15684

Open
wants to merge 1 commit into
base: 5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/base/EagerLoadingFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
interface EagerLoadingFieldInterface
interface EagerLoadingFieldInterface extends FieldInterface
{
/**
* Returns an array that maps source-to-target element IDs based on this custom field.
Expand Down
2 changes: 1 addition & 1 deletion src/base/GqlInlineFragmentFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.3.0
*/
interface GqlInlineFragmentFieldInterface
interface GqlInlineFragmentFieldInterface extends FieldInterface
{
/**
* Returns a GraphQL fragment by its GraphQL fragment name.
Expand Down
14 changes: 14 additions & 0 deletions src/elements/db/ElementQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class ElementQuery extends Query implements ElementQueryInterface

/**
* @var FieldInterface[]|null The fields that may be involved in this query.
* @todo make this private in v6
*/
public ?array $customFields = null;

Expand Down Expand Up @@ -1437,6 +1438,19 @@ public function anyStatus(): static
// Query preparation/execution
// -------------------------------------------------------------------------

/**
* Returns the custom fields that may be involved in this query.
*
* @since 5.5.0
*/
public function getCustomFields(): array
{
if (!isset($this->customFields)) {
$this->customFields = $this->customFields();
}
return $this->customFields;
}

/**
* @inheritdoc
*/
Expand Down
30 changes: 19 additions & 11 deletions src/gql/ElementQueryConditionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use GraphQL\Language\AST\VariableNode;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\WrappingType;
use Illuminate\Support\Arr;
use yii\base\InvalidArgumentException;

/**
Expand Down Expand Up @@ -83,8 +84,13 @@ class ElementQueryConditionBuilder extends Component
*/
private ArgumentManager $_argumentManager;

/**
* @var FieldInterface[]
* @see setCustomFields()
*/
private array $_fields = [];

private array $_fragments;
private array $_eagerLoadableFieldsByContext = [];
private array $_transformableAssetProperties = ['url', 'width', 'height'];
private array $_additionalEagerLoadableNodes;

Expand All @@ -102,15 +108,6 @@ public function __construct($config = [])
}

parent::__construct($config);

// Cache all eager-loadable fields by context
$allFields = Craft::$app->getFields()->getAllFields(false);

foreach ($allFields as $field) {
if ($field instanceof EagerLoadingFieldInterface) {
$this->_eagerLoadableFieldsByContext[$field->context][$field->handle] = $field;
}
}
}

/**
Expand All @@ -135,6 +132,17 @@ public function setArgumentManager(ArgumentManager $argumentManager): void
$this->_argumentManager = $argumentManager;
}

/**
* Sets the custom fields that may be involved in the query.
*
* @param FieldInterface[] $fields
* @since 5.5.0
*/
public function setCustomFields(array $fields): void
{
$this->_fields = $fields;
}

/**
* 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.
Expand Down Expand Up @@ -410,7 +418,7 @@ private function _traversAndBuildPlans(Node $parentNode, EagerLoadPlan $parentPl
// If that's a GraphQL field
if ($subNode instanceof FieldNode) {
/** @var FieldInterface|null $craftContentField */
$craftContentField = $this->_eagerLoadableFieldsByContext[$context][$nodeName] ?? null;
$craftContentField = Arr::first($this->_fields, fn(FieldInterface $field) => $field->handle === $nodeName);

$transformableAssetProperty = ($rootOfAssetQuery || $parentField instanceof AssetField) && in_array($nodeName, $this->_transformableAssetProperties, true);
$isAssetField = $craftContentField instanceof AssetField;
Expand Down
12 changes: 9 additions & 3 deletions src/gql/base/ElementResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use Craft;
use craft\base\EagerLoadingFieldInterface;
use craft\base\ElementInterface;
use craft\base\FieldInterface;
use craft\base\GqlInlineFragmentFieldInterface;
use craft\elements\db\ElementQuery;
use craft\elements\ElementCollection;
use craft\gql\ArgumentManager;
use craft\gql\ElementQueryConditionBuilder;
use craft\helpers\Gql as GqlHelper;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Arr;

/**
* Class ElementResolver
Expand Down Expand Up @@ -92,15 +94,18 @@ protected static function prepareElementQuery(mixed $source, array $arguments, ?
return $query;
}

$fields = $query->getCustomFields();
$parentField = null;

if ($source instanceof ElementInterface) {
$fieldContext = $source->getFieldContext();
$field = Craft::$app->getFields()->getFieldByHandle($fieldName, $fieldContext);
$field = Arr::first($fields, fn(FieldInterface $field) => $field->handle === $fieldName);

// This will happen if something is either dynamically added or is inside an block element that didn't support eager-loading
// and broke the eager-loading chain. In this case Craft has to provide the relevant context so the condition knows where it's at.
if (($fieldContext !== 'global' && $field instanceof GqlInlineFragmentFieldInterface) || $field instanceof EagerLoadingFieldInterface) {
if (
($source->getFieldContext() !== 'global' && $field instanceof GqlInlineFragmentFieldInterface) ||
$field instanceof EagerLoadingFieldInterface
) {
$parentField = $field;
}
}
Expand All @@ -109,6 +114,7 @@ protected static function prepareElementQuery(mixed $source, array $arguments, ?
$conditionBuilder = empty($context['conditionBuilder']) ? Craft::createObject(['class' => ElementQueryConditionBuilder::class]) : $context['conditionBuilder'];
$conditionBuilder->setResolveInfo($resolveInfo);
$conditionBuilder->setArgumentManager($argumentManager);
$conditionBuilder->setCustomFields($fields);

$conditions = $conditionBuilder->extractQueryConditions($parentField);

Expand Down
Loading