[GraphQL] How to get related data in reverse #13349
-
Hey everybody, The title is a bit cryptic as I am not sure how to describe it in words, so let me explain. In Craft I have 2 entry types:
The artist has some fields like title, description, date of birth. The track also has some fields like title, release date and artists. In the artist field I can select one or more artist entries. Querying the artists and tracks via GraphQL works just fine. Also if I want to get the artists of a certain track I can run this query: query Tracks {
tracksEntries {
... on tracks_track_Entry {
id
title
artists {
id
title
}
}
}
} What I fail to accomplish is to get all tracks that belong to an artist. I have tried it this way but this works more like 2 separate queries returning 1 resultset. The query used: query Artists {
artistsEntries(relatedToEntries: {section: "tracks"}, id:["1133"]) {
... on artists_artist_Entry {
id
title
}
}
tracksEntries {
... on tracks_track_Entry {
id
title,
artists {
id,
title
}
}
}
} The result set: {
"data": {
"artistsEntries": [
{
"id": "1133",
"title": "Leona Lewis"
}
],
"tracksEntries": [
{
"id": "4399",
"title": "Spin me up",
"artists": [
{
"id": "1133",
"title": "Leona Lewis"
},
{
"id": "1142",
"title": "MC Hammer"
}
]
},
{
"id": "6813",
"title": "Blood life",
"artists": [
{
"id": "1133",
"title": "Leona Lewis"
}
]
},
{
"id": "6816",
"title": "Lying Eyes",
"artists": [
{
"id": "3133",
"title": "Eagles"
}
]
}
]
}
} In this case I would like to get only the tracks where Leona Lewis. So my primary question is, can I query the Sure, if I select the tracks in the artist edit page it would be easier but it would mean we would have to duplicate a lot of data/work as the artist is already linked via the track. I have read issue #8954 but I think that is not what I am looking for as I do not want to filter/search but rather link the data. Thanks for any insight. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can accomplish this with Verbb’s Many to Many plugin (free). Create a new Many to Many field called “Tracks”. Set it to pull in entries from your Tracks section, using the Artists field. Then add the Tracks field to your Artists entry type’s field layout. With that in place, you’ll be able to start viewing/editing your artist/track relationships from the opposite side, and you’ll be able to start including artists’ tracks in GraphQL queries: query Artists {
artistsEntries {
... on artists_artist_Entry {
id
title
tracks {
id
title
}
}
}
} |
Beta Was this translation helpful? Give feedback.
You can accomplish this with Verbb’s Many to Many plugin (free).
Create a new Many to Many field called “Tracks”. Set it to pull in entries from your Tracks section, using the Artists field. Then add the Tracks field to your Artists entry type’s field layout.
With that in place, you’ll be able to start viewing/editing your artist/track relationships from the opposite side, and you’ll be able to start including artists’ tracks in GraphQL queries: