graphqlcontentful

Filtering a nested relation in GraphQL


I have the following GraphQL query; "categories" is a one-to-many relationship, each containing a name and a slug. I want to get all Articles with category slug of "derp". Any thoughts?

{
allContentfulArticle(
  sort: { order: DESC, fields: [createdAt] }
) {
  edges {
    node {
      title
      slug
      datePublished
      createdAt
      categories {
        name
        slug
      }
    }
  }
}

}


Solution

  • I just prototyped your scenario and this query should do it.

    query {
      categoryCollection(where: { slug_contains: "schnitzel" }) {
        items {
          linkedFrom {
            entryCollection {
              items {
                ...on Article {
                  title 
                }
              }
            }
          }
        }
      }
    }
    

    You can find more info in the docs about links to a specific entry.