1

I'm working on a GraphCMS site in React and would like to set up a GraphQL query to filter posts. We have this query setup which works and fetches all posts (adoptions) :

export const allAdoptions = gql`
  query allAdoptions($first: Int!, $skip: Int!) {
    allAdoptions(orderBy: date_DESC, first: $first, skip: $skip) {
        id
        name
        date
        image {
          handle
        }
        desc
        sex
        species
        neutered
    },
    _allAdoptionsMeta {
      count
    }
  }
`

The code is available here : https://github.com/foxreymann/foranimals/blob/master/src/components/Adoptions.js

We'd like to set up a filter component to get posts by species: "Cat" for example, and we've tried just modifying the above query for species: "Cat" with no luck. Any advice much appreciated, thanks

Bergi
  • 630,263
  • 148
  • 957
  • 1,375

1 Answers1

1

Did you check: https://graphcms.com/docs/api_simple/#filtering-entries?

It should work like this:

query {
  allAdoptions(
    filter: {
      species: "maine-coon"
    }
  ) {
    id
    name
  }
}

Michael

  • Thanks for your help Michael. I've tried that but I'm now getting an error : "Argument 'filter' expected type 'AdoptionFilter' but got: {species: \"Cat\"}. Reason: 'species' Enum value expected – Steve Tyler Feb 22 '18 at 16:11
  • I'm using the Apollo client for React and think that's the issue. – Steve Tyler Feb 22 '18 at 16:30
  • Hey! If its an Enum then you need to remove the ' " '. So ...species: Cat... will do the trick. Does species allow multiple values? Then this is not supported at the moment but will land soon. – Michael Lukaszczyk Mar 03 '18 at 21:42