Search code examples
graphqlstrapi

GraphQL query filtering multiple relations


(From Strapi) I am trying to get all "acts" with a certain age (can return multiple) and with a certain place (can return multiple). I can't figure out how to filter that. This is what I am trying in GraphQL-playground (works without the variables), but it says "Unknown argument "age" on field "Act.ages"." (and "place" respectively).

query GetActs ($age:Int, $place:String) {
  acts {
    data {
      id
      attributes {
        Title
        ages (age: $age) {
          data {
            id
            attributes {
              age
            }
          }
        }
        places (place: $place) {
          data {
            id
            attributes {
              place
            }
          }
        }
      }
    }
  }
}


Solution

  • I just ran into this same issue. I can't make out the error you're reporting, but here is what worked for me.

    You can use filter at the collection level to drill down to nested fields for the corresponding attributes. This follows the GraphQL example at the bottom of this Strapi resource on filtering nested fields.

    Solution

    query GetActs ($age:Int, $place:String) {
      acts (filters: {ages: {age: {eq: $age}}, places: {place: {eq: $place}}}) {
        data {
          id
          attributes {
            Title
            ages {
              data {
                id
                attributes {
                  age
                }
              }
            }
            places {
              data {
                id
                attributes {
                  place
                }
              }
            }
          }
        }
      }
    }