1

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, 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 that provides a Twig example using target and field, but the GraphQL docs suggest using relatedToEntries relatedViaField. I also found a GitHub commit comment 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?

1 Answer 1

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
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.