Filtering by field property vs __allFieldLocales property

Describe the issue:

Trying to understand why these two queries return different values.

There’s a model, BlogPost that references another model, BlogCategory. Both are localised. The query’s goal is to filter by locale and to specifically get only the BlogPosts that match a certain value ‘x’ of BlogCategory.

The following query returns 0 values.

query X($locale: String!, $slug: String!) {
  allDatoCmsBlogpost(
    sort: {fields: meta___firstPublishedAt, order: DESC}
    filter: {locales: {eq: $locale}, category: {slug: {eq: $slug}}}
    locale: $locale
  ) {
    edges {
      node {
        title
        slug
        readingTime
        category {
          title
          slug
        }
        meta {
          firstPublishedAt
        }
        content {
            ...(content stuff queried here)
        }
      }
    }
  }
}

If, instead of filtering category: {slug: {eq: $slug}} we use filter: {locales: {eq: $locale}, category: {_allSlugLocales: {elemMatch: {value: {eq: $slug}}}}} , then the expected values are returned.

Why do these two queries have such a difference in regards to what they return?

Thank you!

Hi there @technology,

Is that query using Gatsby or another third-party GraphQL implementation? That doesn’t seem like our GraphQL dialect/schema :frowning:

Unfortunately, our GraphQL implementation doesn’t support filtering on the slug of a linked category field like that, or the slug of allSlugLocales, for that matter.


In our system, if you want to filter by a linked record, you have to provide its ID instead, like this:

query MyQuery {
  allBlogPosts(filter: {category: {eq: "f6HgZL9MTuuwQigId2GbAA"}}) {
    slug
    title
    category {
      slug
      title
    }
  }
}

Where f6HgZL9MTuuwQigId2GbAA is the record ID of that category.

Alternatively, you can also filter categories by its slug, and then use an inverse relationship to fetch the related posts:

First, you’d enable inverse relationships for the category model:

Then you can filter categories and fetch its related posts:

Thank you, we’ll give this a try and come back depending on the outcome :+1:

We are using graphql from Gatsby.

1 Like