I'm trying to retrieve entries related to the current entry, based on a tag
field called topics
. Simple stuff.
Now, I use Craft's native GraphQL implementation so I'm bound to the possibilities this interface gives me. In twig, it would be quite easily solved as described in the following topic: Search for related entries based on tags
It seems like the relatedTo
property of the entries()
query only supports a single ID, without the ability to scope my results based on a certain field. I would expect to be able to do something like the following:
entries(
section: "article",
limit: 2,
relatedTo: {
sourceElement: 123, // ID of the current entry
field: 'topics'
}
) {
id,
title
}
This just gives me the following error: Expected type [Int], found {sourceElement: 123, field: \"topics\"}.
Now, if I resort to the documentation it indeed describes that only an entry's ID can be used as the value of relatedTo
, like this:
query QueryName {
entries(
section: "article",
limit: 2,
relatedTo: 123 // Current article ID
) {
id,
title
}
}
This does work (it doesn't error), but results in an empty entries
array. Even though there's entries that have the same topics
(tags) assigned to them.
I'm kinda lost on how to actually retrieve related entries. Should I just revert to using the search
param and feed it a concatenated list of the current post's tags?
Any elegant solutions would be greatly appreciated!
Thanks in advance!