Answers.Body: <p>OK, found out how to do it through a ton of trial and error:</p>
  <pre><code>query ($slug: [String]) {
    song: entry(section: &quot;songs&quot;, slug: $slug) {
      slug
      title
    }
    relatedAlbums: entries(
      section: &quot;albums&quot;
      relatedToEntries: [
        { section: &quot;songs&quot;, slug: $slug, relatedViaField: &quot;tracklist&quot; }
      ]
    ) {
      slug
      title
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-09-25T16:52:39.863Z
Answers.LastActivityDate: 2024-09-25T16:52:39.863Z
Answers.UserDisplayName: sea_monster
Body: <p>I'm trying to create a GraphQL query to find nested entries stored in a Matrix field. In my case, I want to find all the albums that have a particular song in its tracklist. According to <a href="https://github.com/craftcms/cms/issues/10242#issuecomment-2274456527" rel="nofollow noreferrer">this comment</a>, Craft 5.x has full support for this.</p>
  <p>I'm having trouble finding documentation or examples, though. There's a <a href="https://craftcms.com/docs/5.x/system/relations.html#relations-via-matrix" rel="nofollow noreferrer">short section on the docs page</a> that provides a Twig example using <code>target</code> and <code>field</code>, but the <a href="https://craftcms.com/docs/5.x/development/graphql.html" rel="nofollow noreferrer">GraphQL docs</a> suggest using <code>relatedToEntries</code> <code>relatedViaField</code>. I also found a <a href="https://github.com/craftcms/cms/pull/15508#issue-2456702295" rel="nofollow noreferrer">GitHub commit comment</a> with an example which looks a lot like what I want, but it's returning all the albums, not just the ones with the target song in the tracklist.</p>
  <pre><code># Note: $slug here is the slug of the song
  
  query ($slug: [String]) {
    entries(
      section: &quot;albums&quot;
      relatedToEntries: {
        section: &quot;songs&quot;
        slug: $slug
        relatedViaField: &quot;tracklist&quot; &lt;- The album's matrix field
      }
    )
  }
  </code></pre>
  <p>What am I missing here?</p>
  
Tags: <query><graphql>
Title: Query nested Matrix entry relations via GraphQL
ThreadText: # Query nested Matrix entry relations via GraphQL
  
  (Asked by: username_0 on 2024-09-23)
  
  I'm trying to create a GraphQL query to find nested entries stored in a Matrix field. In my case, I want to find all the albums that have a particular song in its tracklist. According to [this comment](https://github.com/craftcms/cms/issues/10242#issuecomment-2274456527), Craft 5.x has full support for this.
  
  I'm having trouble finding documentation or examples, though. There's a [short section on the docs page](https://craftcms.com/docs/5.x/system/relations.html#relations-via-matrix) that provides a Twig example using `target` and `field`, but the [GraphQL docs](https://craftcms.com/docs/5.x/development/graphql.html) suggest using `relatedToEntries` `relatedViaField`. I also found a [GitHub commit comment](https://github.com/craftcms/cms/pull/15508#issue-2456702295) with an example which looks a lot like what I want, but it's returning all the albums, not just the ones with the target song in the tracklist.
  
  ```
  # Note: $slug here is the slug of the song
  
  query ($slug: [String]) {
    entries(
      section: "albums"
      relatedToEntries: {
        section: "songs"
        slug: $slug
        relatedViaField: "tracklist" <- The album's matrix field
      }
    )
  }
  
  ```
  
  What am I missing here?
  
  **Tags:** query, graphql
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-09-25. Score: 1
  
  OK, found out how to do it through a ton of trial and error:
  
  ```
  query ($slug: [String]) {
    song: entry(section: "songs", slug: $slug) {
      slug
      title
    }
    relatedAlbums: entries(
      section: "albums"
      relatedToEntries: [
        { section: "songs", slug: $slug, relatedViaField: "tracklist" }
      ]
    ) {
      slug
      title
    }
  }
  
  ```
  
  
  ---
  
  