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

Allow array as return type for prepareElementQuery #12271

Closed
wants to merge 1 commit into from

Conversation

engram-design
Copy link
Contributor

There's an issue with GQL queries where many of the resolvers return an array.

Which shown as per the below

/** @var ElementQuery $query */
$query = static::prepareQuery($source, $arguments, $fieldName);
// If that's already preloaded, then, uhh, skip the preloading?
if (!$query instanceof ElementQuery) {
return $query;
}

If prepareQuery() doesn't return an ElementQuery then return it.

Otherwise, you'll get the following error: craft\\gql\\base\\ElementResolver::prepareElementQuery(): Return value must be of type craft\\elements\\db\\ElementQuery|Illuminate\\Support\\Collection, array returned

I suppose you could either enforce the resolvers to return a collection, or tighten up the prepareQuery() return types - take your pick!

@brandonkelly
Copy link
Member

@brianjhanson Changing the return type would be a breaking change to any resolvers that override that method, so we need to tread carefully here.

My understanding is that the only time these would not be element queries is if they were eager-loaded, in which case they should be a collection, not an array. So we should look into why arrays are cropping up—maybe a bug further up the chain?

Even if there ends up being a very good reason for them being arrays, it might make more sense to wrap them in a collection rather than changing the return type.

@engram-design
Copy link
Contributor Author

More than happy to go with what you recommend, but as it is, Craft itself will throw this error, due to your resolvers returning an array.

if (!GqlHelper::canQueryUsers()) {
return [];
}

if (!GqlHelper::canQueryAssets()) {
return [];
}

if (!GqlHelper::canQueryCategories()) {
return [];
}

Might I suggest changing these to return Collection's instead?

@brandonkelly
Copy link
Member

Thanks for the pointer! Fixed via #12275.

@brandonkelly
Copy link
Member

Craft 4.3.2 is out with that fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants