cms icon indicating copy to clipboard operation
cms copied to clipboard

Unexpected errors with relatedTo, relatedToCategories with GraphQl structure children

Open degraaf opened this issue 2 years ago • 5 comments

What happened?

Description

Having unexpected and inconsistent behaviour when using relatedTo or relatedToCategories to get structure children.

First problem was that relatedTo would only work for children of level 1. Nested children (i.e. levels 3 and 4) would return all entries and ignore any arguments in the children() subquery. For example, children(type: "navItem") would also be ignored.

I also tried changing to use relatedToCategories but found that it would only work for level 1 queries and would throw an error if used in any of the children() subquery. Should relatedToCategories work in a children() subquery?

Steps to reproduce

The regions field in the query below is a category field. A "nav item" structure entry has only a title, a category and a link field (not included in example queries below).

# Variables:
{
  "site": "default",
  "mainNavSection": "mainNav",
  "regions": [{ "id": "12345" }]
}
# Query:
query ($site: [String], $mainNavSection: [String], $regions: [QueryArgument]) {
  mainNav: entries(site: $site, section: $mainNavSection, level: 1, relatedTo: $regions) {
    title
    type: typeHandle
    regions {
      id
      title
    }
    ... on default_mainNav_navItem_Entry {
      link: navigation__link
    }
    children(relatedTo: $regions) {
      title
      type: typeHandle
      ... on default_mainNav_navItem_Entry {
        link: navigation__link
      }
      regions {
        id
        title
      }
      children(relatedTo: $regions) {
        # ... fields as above
        children(relatedTo: $regions) {
          # ... fields as above
        }
      }
    }
  }
}

Second attempt with relatedToCategories:

Works:
query ($site: [String], $mainNavSection: [String], $regions: [CategoryCriteriaInput]) {
  mainNav: entries(site: $site, section: $mainNavSection, level: 1, relatedToCategories: $regions) {
    title
    type: typeHandle
    regions {
      id
      title
    }
}

Does not work:
query ($site: [String], $mainNavSection: [String], $regions: [CategoryCriteriaInput]) {
  mainNav: entries(
    site: $site, section: $mainNavSection, level: 1, relatedToCategories: $regions) {
    title
    type: typeHandle
    regions {
      id
      title
    }
    children(relatedToCategories: $regions) {
      title
      type: typeHandle
      children(relatedToCategories: $regions) {
        title
        type: typeHandle
        regions {
          id
          title
        }
        children(relatedToCategories: $regions) {
          # ... fields as above 
        }
      }
    }
  }
}

Errors found via debugger:

yii\base\UnknownPropertyException: Setting unknown property: craft\elements\db\EntryQuery::relatedToCategories

Expected behavior

  1. Correct children and fields returned according to subquery in both relatedTo and relatedToCategories

Actual behavior

  1. All children returned, or error.

Craft CMS version

3.7.53.1

PHP version

8.0.16

Operating system and version

Linux 5.10.104-linuxkit

Database type and version

MySQL 10.7.3 (MariaDB)

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.0-16)

Installed plugins and versions

Amazon S3 1.3.0 Content Stats 2.1.2 CP Clear Cache 1.2.1 Event Scheduler 1.0.0 Expanded Singles 1.2.0 Formie 1.6.12 Icon Picker 1.1.14 Reasons 2.3.1 Redactor 2.10.10 Scout 2.7.2 Sprout Redirects 1.5.3 Super Table 2.7.2 Typed link field 1.0.25

  • a number of our own custom plugins

degraaf avatar Sep 12 '22 07:09 degraaf

UPDATE: I found that moving the children() statement above the statement ... on default_mainNav_navItem_Entry { link: navigation__link } seems to work as expected.

The statement ordering did not make a difference to the relatedToCategories error.

degraaf avatar Sep 12 '22 07:09 degraaf

Is this fully working as expected for you now with the updated statement order, or are you still getting an error? I can’ tell from your last reply.

brandonkelly avatar Sep 13 '22 17:09 brandonkelly

Ah sure, was trying to cover a few different variations of things I tried (and accidentally closed the ticket).

So...

  • relatedTo only works if the ... on default_mainNav_navItem_Entry {} statements are added below the children() statements. Is this normal and expected?

  • relatedToCategories does not work at all within children() statements. The entire query crashes with the UnknownProperty exception.

degraaf avatar Sep 14 '22 00:09 degraaf

We’ve added support for relatedTo* arguments to children fields, for the next Craft 3 and 4 releases.

We haven’t yet been able to identify the culprit of the ... on field order bug, but at least that one is easy to work around.

brandonkelly avatar Sep 19 '22 12:09 brandonkelly

Craft 3.7.55 and 4.2.5 are out with that children(relatedTo*) fix.

brandonkelly avatar Sep 20 '22 16:09 brandonkelly

@degraaf are you still experiencing the ... on order bug? I tried to replicate this morning but wasn't able to, so I'm curious if it was actually fixed by this update.

brianjhanson avatar Oct 03 '22 17:10 brianjhanson

Will close this due to inactivity, but we can reopen if needed.

brandonkelly avatar Oct 10 '22 18:10 brandonkelly