Describe the issue:
I’m trying to understand whether it’s possible in the GraphQL API to filter records based on whether they are referenced by other records.
In my example, I want to return only authors that are referenced by at least one blog post. I was expecting something like an exists filter on _allReferencingBlogPosts to work.
query {
allBlogPosts {
title
author {
id
}
}
allAuthors(filter: { _allReferencingBlogPosts: { exists: true } }) {
id
firstName
lastName
_allReferencingBlogPosts {
title
}
}
}
What I’m trying to achieve is: list only authors that have at least one referencing blog post.
What I’ve tried:
Thanks.
This is not something the CDA can currently filter directly at the moment 
_allReferencingBlogPosts is an inverse relationship field. You can query it on each author, and you can also filter, paginate, and order the nested posts inside that field, as shown here: https://www.datocms.com/docs/content-delivery-api/inverse-relationships. What is not supported is using that inverse relationship itself inside the root allAuthors(filter: ...) input, so allAuthors(filter: { _allReferencingBlogPosts: { exists: true } }) will not work.
If you compare the filtering docs at https://www.datocms.com/docs/content-delivery-api/filtering-records with the inverse relationship docs above, the distinction is that inverse relationships are exposed as query fields with their own nested arguments, not as top level collection filters. We also described the same limitation in a related thread here: https://community.datocms.com/t/filter-records-to-only-those-linking-to-a-specific-type/4631
The practical workaround is either to query allBlogPosts and traverse to author, deduping authors in your application, or to query allAuthors and inspect _allReferencingBlogPostsMeta { count }, then keep only authors where the count is greater than zero. For example:
query {
allAuthors {
id
firstName
lastName
_allReferencingBlogPostsMeta {
count
}
_allReferencingBlogPosts {
title
}
}
}
So in practice, _allReferencingXXX can be queried, and its nested results can be filtered, but it cannot currently be used to filter the parent collection by existence