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?