How Do I Specify a Graphql Type That Takes Multiple Types?

I want to create a graphql type that can return either an Array of Integers or String.

I've already tried using union in the form union CustomVal = [Int] | String, but this returns an error.

The schema declaration is:

union CustomValues = [Int] | String

type Data {
    name: String
    slug: String
    selected: Boolean
    values: CustomValues
}

The error is:

node_modules/graphql/error/syntaxError.js:24
return new _GraphQLError.GraphQLError('Syntax Error: ' + description, undefined, source, [position]);
Syntax Error: Expected Name, found [
GraphQL request (81:23)
80: 
81:     union CustomValues = [Int] | String

Is this possible to do in graphql? If not, can you please suggest an alternative to do this.

I ask this as the union documentation says that Note that members of a union type need to be concrete object types; you can't create a union type out of interfaces or other unions.

Any solutions would be highly helpful.

4

1 Answer

Follow this , Graphql does not support scaler union types currently, you can do this

union IntOrString = IntBox | StringBox

type IntBox {
  value: Int
}

type StringBox {
  value: String
}

or you can have your custom type, see this graphql, union scalar type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest