Registry / testing / graphql-union-input-type

graphql-union-input-type

JSON →
library0.4.0jsnpmunverified

Provides a GraphQL union input type for graphql-js, enabling mutations to accept polymorphic input via a single field with type discrimination. Version 0.4.0 is the latest stable release; the package has minimal maintenance. It works by wrapping multiple GraphQLInputObjectType definitions and resolving the concrete type based on a key field, a resolveType function, or AST/value inspection. Unlike core GraphQL, which forbids unions/interfaces on input types, this library allows flexible mutation inputs without manual AST traversal, but requires careful handling of typeKey or resolveType to avoid validation errors.

npm install graphql-union-input-type
INSTALL
IMPORT
SIG · GRAPHQL-UNION-INPU
G
graphql-union-input-type
testingjavascriptv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import UnionInputType from 'graphql-union-input-type'
const UnionInputType = require('graphql-union-input-type')
The package is CommonJS only, but ESM usage is possible with bundlers. Default import yields the factory function.
UnionInputType
const UnionInputType = require('graphql-union-input-type')
import { UnionInputType } from 'graphql-union-input-type'
No named export; the package exports a single factory function as default.
GraphQLUnionInputType (if creating instance)
const MyUnionInput = UnionInputType({ ... })
const MyUnionInput = new UnionInputType({ ... })
UnionInputType is not a class; just call as a function.

Creates a union input type combining Jedi and Sith input types using the typeKey 'side', then uses it in a GraphQL mutation.

const { GraphQLInputObjectType, GraphQLString, GraphQLBoolean, GraphQLSchema, GraphQLObjectType, graphql } = require('graphql'); const UnionInputType = require('graphql-union-input-type'); const JediInputType = new GraphQLInputObjectType({ name: 'jedi', fields: { side: { type: GraphQLString }, name: { type: GraphQLString } } }); const SithInputType = new GraphQLInputObjectType({ name: 'sith', fields: { side: { type: GraphQLString }, name: { type: GraphQLString }, doubleBlade: { type: GraphQLBoolean } } }); const HeroInputType = UnionInputType({ name: 'hero', inputTypes: [JediInputType, SithInputType], typeKey: 'side' }); const mutationType = new GraphQLObjectType({ name: 'Mutation', fields: { addHero: { args: { hero: { type: HeroInputType } }, type: GraphQLString, resolve: (_, { hero }) => `Added hero with side: ${hero.side}` } } }); const schema = new GraphQLSchema({ mutation: mutationType }); graphql(schema, 'mutation { addHero(hero: { side: "jedi", name: "Luke" }) }').then(console.log);
Debug
Known issues
breakinggraphql peer dependency must be <0.13; incompatible with graphql 0.13+.
fix
Downgrade graphql to 0.12.x or earlier, or find an alternative library.
affects: >=0.4.0
deprecatedThe package relies on an unofficial approach; GraphQL now supports @oneOf directive for input unions (graphql-js 17+).
fix
Consider upgrading to graphql-js 17+ and using @oneOf instead of this library.
affects: >=0.4.0
gotchaUsing resolveTypeFromAst or resolveTypeFromValue is error-prone; AST traversal is complex and fragile across graphql versions.
fix
Prefer using typeKey or resolveType with a simple name-to-type mapping for reliability.
affects: >=0.4.0
gotchaThe typeKey field name must match exactly the field used in mutation arguments to differentiate types; otherwise validation fails.
fix
Ensure the key field exists in all input object types, and its value matches the type name specified in inputTypes.
affects: >=0.4.0
Errors
Common errors & fixes
TypeError: (intermediate value).getType is not a function
Passing a GraphQLObjectType instead of GraphQLInputObjectType to inputTypes.
fix
Ensure all types in inputTypes are GraphQLInputObjectType instances, not output types.
Cannot read property 'name' of undefined
The typeKey is set but the mutation argument does not include that key.
fix
Include the required typeKey field (e.g., 'side') in the input argument with a string value matching one of the type names.
Expected a value of type "hero" but received: ...
resolveType function returned null or undefined for a given input type name.
fix
Make sure resolveType handles all possible names returned from the typeKey field; if inputTypes is used, ensure the key values match the type names.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency, must be <0.13
Agent activity
11 hits · last 30 days
node
10
Resources
graphql-union-input-type — npm install graphql-union-input-type · libregistry