The new relatedToEntries goes a long way to writing neater GraphQL queries, but I can't figure out how to make it an "and" search.
In the following example we only want to find entries that are related to "united-kingdom" AND "web-developer" but relatedToEntries seems to work on an "either" basis.
{
# This outputs 1
justProfessions: entryCount(section: "resource", relatedToEntries: {section: "profession", slug: "web-developer"})
# This outputs 2
justCountries: entryCount(section: "resource", relatedToEntries: {section: "country", slug: "united-kingdom"})
# This outputs 2 (but ideally we want it 1)
both: entryCount(section: "resource", relatedToEntries: {section: ["country", "profession"], slug: ["united-kingdom", "web-developer"]})
}
For relatedTo you can do relatedTo: ["and", 1,2,3,4] so I presumed you could do
both: entryCount(section: "resource", relatedToEntries: {section: ["and","country", "profession"], slug: ["and","united-kingdom", "web-developer"]})
In Twig we'd probably write this like...
{% set entries = craft.entries.section('resource').relatedTo(
['and', {'section': 'country', 'slug': 'united-kingdom'}, {'section': 'profession', 'slug': 'web-developer'}]
).all() %}
under the hood relatedTo* gets translated to an element query. Since an entry can't belong to two sections, section: ["and","country", "profession"] is not going to work.
The use case has merit, though. A possible solution would be to allow an array of relatedTo* values, for example:
graphql
both: entryCount(section: "resource", relatedToEntries: [{section: "country", slug: "united-kingdom"}, {section: "profession", slug: "web-developer"})
Alright, added this for the next Craft 3.6 release!
Craft 3.6.7 is out now with this change ✨
Most helpful comment
Craft 3.6.7 is out now with this change ✨