Contentful GraphQL filter with relational queries and reference fields

Viewed 1005

I'm currently trying to filter entries with a field references, many. In other words, the field takes multiple locations with each locations consisting of city among other fields.

For my query I want to get every entry that has a location with a specific city name set.

The following query filters an entry that takes only on reference for location, how would it look like when having many?

export const NEWS_ARTICLES = gql`
    query GetNewsArticles($location: String) {
        entryCollection(
            where: {
                location: { city: $location }
            }
        ) {
            items {
                location
            }
        }
    }
`;

Pulling GraphQL's schema all I get is locationCollection_exists - is this even possible?

I found the following article, which seems to do what I desire, but for the Filter API and not GraphQL: https://www.contentful.com/developers/docs/concepts/relational-queries/

1 Answers

Contentful DevRel here.

Filtering by a linked collection entry is not supported right now. What you can do though it flip the query around.

export const NEWS_ARTICLES = gql`
    query GetNewsArticles($location: String) {
        cityCollection(
             # your filter
        ) {
            items {
                linkedFrom {
                    # all the entries linking to this entry
                }
            }
        }
    }
`;

You can find more information about this approach in the docs or this blogpost.

Related