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

Unexpected behaviour when using GraphQL relatedTo vs relatedToCategories #7954

Closed
wuhhh opened this issue May 19, 2021 · 4 comments
Closed

Comments

@wuhhh
Copy link

wuhhh commented May 19, 2021

Description

I am trying to filter a GraphQL entries query using relatedToCategories and relatedToTags. If I set the variable to null for these parameters, results are only returned if entries contain any data at all for the category and tag fields.

For clarity:

With $category set to null, the following query returns eight results, which is the total number available:

query ($offset: Int, $limit: Int, $category: [QueryArgument]) {
  entries(section: "blog", offset: $offset, limit: $limit, relatedTo: $category) {

This seems correct to me, as I'm effectively saying, "don't match against any category in this case, show everything"

However, changing the query to the following produces only 7 results - the missing entry has no category selected:

query ($offset: Int, $limit: Int, $category: [QueryArgument]) {
  entries(section: "blog", offset: $offset, limit: $limit, relatedToCategories: { id: $category }) {

Why don't these produce the same results?
Is there a way to pass null and return all results when using relatedToCategories and relatedToTags?

Additional info

  • Craft version: 3.6.14
  • PHP version: 7.4.16
  • Database driver & version: Ver 14.14 Distrib 5.7.32
@brandonkelly
Copy link
Member

That’s actually working as expected. That query is generating the GraphQL equivalant of this element query in Twig:

{% set entries = craft.entries()
  .relatedTo(craft.categories().id(null))
  .all() %}

Keep in mind that element queries’ id param is null by default. So id(null) actually does nothing there. So the category query is going to end up matching every category. Which means that the top-level entry query is going to fetch every entry that is related to any category. (Consistent with what you’re seeing.)

What you’re really asking for is a way to make the entire relatedToCategories argument optional, which isn’t possible with GraphQL syntax. You’ll need to just execute a separate GraphQL query – one without the relatedToCategories argument – whenever there’s no related category you want to check for.

@mediabeastnz
Copy link

@brandonkelly sorry for commenting on this super old issue but has anything in this area of Craft been updated to allow for this?

Just from a "keeping codebase tidy" point of view having to conditional send queries is not nice.

It would be awesome if somehow you could just do the following and by doing so the filter is just skipped.

No filter....

newsPostEntries(status: "live", relatedToCategories: null, limit: $limit, offset: $offset) { ...

Filter...

newsPostEntries(status: "live", relatedToCategories: {slug: "my-category-slug"}, limit: $limit, offset: $offset) { ...

@brandonkelly
Copy link
Member

@mediabeastnz That’s a little different from the OP, where only the id sub-property of relatedToCategories was getting set to null.

I just added support for setting the entire relatedToCategories argument to null, for Craft 4.5 (eaab984).

@brandonkelly
Copy link
Member

Craft 4.5.0 is out with that change.

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

No branches or pull requests

4 participants