fourie_lean
fourie_lean

Reputation: 57

Filtering posts by category on Contentful with GraphQL

I am struggling to find a way to filter tags by category.

I have a category content model that consists of a slug and a title, both are short text fields. This content model is added to my blog posts as a reference object referencing many categories ( if that makes sense ).

On a single blog post page, I want to load more posts that have similar categories, but exclude the current post from the results.

The problem from what I read is, “Filtering by a linked collection entry is not supported right now”.

So I cannot create a graphQL query like this:

{
  articleCollection(
    where: {
      slug_not: "exclude-this-slug"
      categoriesCollection: {
        slug: "category"
      }
    }
  ) {
    items {
      title
    }
  }
}

I can get the articles doing a query like this:

{
  categoriesCollection (
    where: {
      slug: "category"
    }
  ) {
    items {
      linkedFrom {
        articleCollection {
          items {
            title
          }
        }
      }
    }
  }
}

But there seems to be no way to filter out the current post as the only filters I can add to the articleCollection within that query seems to be skip, limit, locale and preview.

Is there any way I can get related posts by using the category as a content model, as I am currently doing or is there perhaps a different solution to this?

Thanks in advance

Upvotes: 1

Views: 1967

Answers (1)

RWD
RWD

Reputation: 1326

With your current content model, you will not be able to achieve the filtering you want with Contentful's GraphQL API due to limitations in the generated schema and supported filtering options.

In the first query, it's because you permit an article to link to many categories, but, as stated as a limitation on the Contentful GraphQL Content API documentation:

It is not possible to filter on fields of Type Array<Link>

In the second query, as you have observed, where is not available on the linkedFrom collection.

Alternative 1: filter in code

Say you want to display 4 related articles, you could use your second query, but request 5 articles (with limit: 5), then in your app code filter out from the articles returned the one that is currently being displayed.

Alternative 2: use tags in the content model

If you change your content model to use tags for the categories (instead of references to another content model), then you will be able to use a variation on your first query.

You would need to choose between slug or title for the category tag, but could still have a category content model with both fields. The drawback is that it's not explicitly linked to the articles, but in the same GraphQL API request, you could also request the other field if you need it.

query {
  articleCollection(
    where: {
      slug_not: "exclude-this-article-slug"
      categories_contains_all: "category-slug"
    }
  ) {
    items {
      title
    }
  }
  categoryCollection(
    where: {
      slug: "category-slug"
    }
  ) {
    items {
      title
    }
  }
}

Upvotes: 2

Related Questions