craftcms / craftcms/cms

[4.x]: fragments before children return incorrect graphql results

Open
#12,572 0 comments 0 reactions 1 assignee View on GitHub

@brianjhanson is already working on this.

Since Jan 26, 2023.

bug craft4
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

What happened?
Description

When performing a nested graphql query with fragments, the order of operation matters when it shouldn't. First reported in #11918 but fell off the radar a bit.

Steps to reproduce

Given a site with a section called structure and a handful of entries populated

CleanShot 2023-01-24 at 17 51 25@2x

query {
  entries(section:"structure", level:1) {
    ...sharedFields
    children(type:"nested") {
      id
      title
    }
  }
}

fragment sharedFields on EntryInterface {
  id
  title
  typeHandle
}
Expected behavior

Is expected to return

{
  "data": {
    "entries": [
      {
        "children": [
          {
            "id": "47",
            "title": "Nested Type"
          }
        ],
        "id": "39",
        "title": "Top Level Item",
        "typeHandle": "default"
      },
      {
        "children": [],
        "id": "41",
        "title": "Second Top Level Item",
        "typeHandle": "default"
      }
    ]
  }
}
Actual behavior
{
  "data": {
    "entries": [
      {
        "id": "39",
        "title": "Top Level Item",
        "typeHandle": "default",
        "children": [
          {
            "id": "43",
            "title": "Child"
          },
          {
            "id": "45",
            "title": "Child Two"
          },
          {
            "id": "47",
            "title": "Nested Type"
          }
        ]
      },
      {
        "id": "41",
        "title": "Second Top Level Item",
        "typeHandle": "default",
        "children": []
      }
    ]
  }
}

Work around

Moving the fragment above children will cause the correct data to be returned.

{
  entries(section: "structure", level: 1) {
    children(type: "nested") {
      id
      title
    }
    ...sharedFields
  }
}

fragment sharedFields on EntryInterface {
  id
  title
  typeHandle
}

Craft CMS version

4.3.6.1

PHP version

N/A

Operating system and version

N/A

Database type and version

N/A

Image driver and version

N/A

Installed plugins and versions

N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.