The fields accessible at the root of your GraphQL query are defined in a GraphQL schema as fields on the root query type, commonly simply named Query
. This type is exactly the same as any other GraphQL Object type in a schema. So this question can be restated as:
Do GraphQL fields have the ability to return different results based on the arguments or argument types passed in?
The short answer is "no". One important property of GraphQL is that a particular field must always return the same type; this means that a field that returns an array must always return an array, and a field that returns an object type must always return that particular type, regardless of the arguments passed in.
However, there are a few ways you can achieve similar results to avoid needing to create too many different named fields:
- GraphQL fields can have optional arguments. So for example if you have a field called
individuals
, you could use optional arguments to supply different kinds of filters, for example: individuals(search: "Dan")
or individuals(status: "admin")
, or individuals
(with no arguments) or individuals(status: "admin", search: "Dan")
(both arguments provided at once.
- GraphQL fields can return a union of several object types. So if your field can return several different types of things, you can use a union. This is represented in the SearchResult example in the docs: http://graphql.org/learn/schema/#union-types Unfortunately that doesn't apply here since you can't have a union of an array with an object type.
In many cases though, you will need to create multiple fields because GraphQL doesn't currently have method overloading or polymorphism.