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?
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.
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") { ...
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.
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).
As of September 2017, this is not possible. There is an ongoing discussion around this functionality.
There is a npm module. I haven't tested. https://www.npmjs.com/package/graphql-union-input-type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With