Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do GraphQL fields support polymorphism based on passed in arguments?

I would like to define the following queries

{
// return all individuals
individuals {
    id
  }
}

// return ONE individual by id
individuals(id:"123") {
   id
  }
}

note that the query name is the same, only the parameters are different.

Today, the only workaround I found is to define different query names.

How can I define polymorphic queries? Is it even possible?

like image 625
Olivier Refalo Avatar asked Oct 21 '16 19:10

Olivier Refalo


People also ask

Does GraphQL support inheritance?

GraphQL does not inherently support inheritance. There is no syntax that would help you avoid duplication of fields across multiple types. Barring that, you can also utilize a library like graphql-s2s which allows you to utilize inheritance and generic types.

What are the main operations that GraphQL supports?

There are three types of operations that GraphQL models: query – a read‐only fetch. mutation – a write followed by a fetch. subscription – a long‐lived request that fetches data in response to source events.

How do you pass parameters in GraphQL?

When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.

What are the 2 most common actions in GraphQL?

From the point of view of the client, the most common GraphQL operations are likely to be queries and mutations.


1 Answers

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:

  1. 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.
  2. 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.

like image 157
stubailo Avatar answered Sep 30 '22 17:09

stubailo