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

1.1k Views Asked by At

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
        }
      }
    }
  }
}
2

There are 2 best solutions below

2
On BEST ANSWER

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

0
On

According to @khaled's answer, it seems you can query all entries from a content type referencing an entry from another, simply querying the first as a subfield.

So in my case, to query all Albums from an Artist, the query is like this:

query artistQuery($id: String!) {
  contentfulArtist(id: { eq: $id }) {
    id
    artistName
    album {
      id
      title
    }
  }
}