Search code examples
graphqlcontentfulgatsby

Is it possible to query all entries from a content type and filter the response based on a subfield?


I'm using Contentful, and I have two content types: Album and Artist.

Album has an "artist" field, referencing an entry of the Artist content type, and I want to perform a query that returns an Artist and all Albums referencing that Artist.

I have something like this, but i don't know how to filter the albums:

query artistQuery($id: String!) {
  contentfulArtist(id: { eq: $id }) {
    id
    artistName
  }
  allContentfulAlbum {
    edges {
      node {
        artist {
          id
          artistName
        }
      }
    }
  }
}

Solution

  • Di you try this query ?

    query artistQuery{
      contentfulArtist{
        id
        artistName
      }
       album {
        id
        artistName
      }
    }
    

    I had a similar use case with author and articles. and I solved with a similar query. See screenshot. enter image description here