Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL Union and Input Type?

Is it possible in GraphQL to have an input type that is also a union?

Something like:

const DynamicInputValueType = new GraphQLUnionType({   name: 'DynamicInputType',   types: [GraphQLString, GraphQLFloat, GraphQLInt] }) 

but also able to be used as a input for a mutation?

like image 295
Alex Smith Avatar asked Jul 24 '17 19:07

Alex Smith


People also ask

Does GraphQL support union types?

The GraphQL type system also supports Unions . Unions are identical to interfaces, except that they don't define a common set of fields. Unions are generally preferred over interfaces when the possible types do not share a logical hierarchy.

What is union type in GraphQL?

A union type is a set of object types which may appear in the same spot. Here's a union, expressed in GraphQL Schema Definition Language (SDL): union MediaItem = AudioClip | VideoClip | Image | TextSnippet. This might be used on a search field, for example: searchMedia(term: "puppies") { ...

What are GraphQL input types?

The input type in a GraphQL schema is a special object type that groups a set of arguments together, and can then be used as an argument to another field. Using input types helps us group and understand arguments, especially for mutations.

Is it possible to use inheritance with GraphQL input types?

No, the spec does not allow input types to implement interfaces. And GraphQL type system in general does not define any form of inheritance (the extends keyword adds fields to an existing type, and isn't for inheritance).


2 Answers

As of September 2017, this is not possible. There is an ongoing discussion around this functionality.

like image 50
Artur Nowak Avatar answered Oct 06 '22 19:10

Artur Nowak


There is a npm module. I haven't tested. https://www.npmjs.com/package/graphql-union-input-type

like image 28
François Romain Avatar answered Oct 06 '22 20:10

François Romain