40

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
  • Can you try removing [] from [Int]? Commented Jun 12, 2018 at 4:19
  • Still throws an error, and I want the type to either take an Array of Integers or a String, and isn't array type declared in graphql using []? The error is Union type CustomValues can only include Object types, it cannot include Int. Union type CustomValues can only include Object types, it cannot include String. Commented Jun 12, 2018 at 5:16
  • You need to use [] if you want a list (array), but if you want to return different types on different inputs then you'll have to define how to do that in your resolver function. Commented Jun 12, 2018 at 5:21
  • Did you find an answer? I am currently having a similar issue when I want to create a Union type that accepts an object and an array of this object, and I get an error that it is not possible to use array in a Union type.. Commented Feb 22, 2022 at 13:04

2 Answers 2

58

Follow this https://github.com/facebook/graphql/issues/215, Graphql does not support scalar 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?

Sign up to request clarification or add additional context in comments.

Comments

0

As per Graphql specification,

The member types of a Union type must all be Object base types. Scalar, Interface and Union types must not be member types of a Union.

Then instead of "union", you can use custom scalar to solve this use-case. Since any scalar type is valid JSON, hence we can use a custom JSON scalar here.

You can use this package graphql-type-json for custom scalar json type.

scalar JSON type Data { name: String slug: String selected: Boolean values: JSON } 

In this way, you can associate any scalar values (i.e. Int, Float, String etc..) or its array associations (i.e. [Int], [String], etc..) with member "Data.values" without violating the specifications.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.