Nicholas
Nicholas

Reputation: 149

GraphQL filtering based on parent context

I've read the docs and I cannot seem to figure out how to structure my GraphQL for a particular query. For my data I have:

child
  |_ school
       |_ class

A child has schools and schools have classes, but a child is only assigned to specific classes in a school.

I want to query a specific child to get only the classes they are in.

query={
  child(id:$id){
    schools{
      name
      classes{
        name
      }
    }
  }
}

I can technically filter the classes while resolving the schools field in the child type by looking deep down the fields but I wanted to clarify that this is still conforming to GraphQL. Should I be placing the classes as a field in the child type instead?

Upvotes: 2

Views: 1295

Answers (1)

Gershon Papi
Gershon Papi

Reputation: 5106

A child also have classes, so it makes sense to create a classes field in the child object. This, in addition to the classes field in the school object.

Of course you could also filter the classes while resolving it but it's just another extra work with no particular reason.

Since there is a direct relationship between a child and his classes it seems better for a child to have a classes field.

Upvotes: 2

Related Questions