Luke
Luke

Reputation: 21286

GraphQLUnion with GraphQLList

My resolver can either return a list of PostType's or an ErrorType. However, I seem to be unable to describe this scenario in GraphQL:

const PostGetAllResponseType: GraphQLUnionType = new GraphQLUnionType({
  name: 'PostGetAllResponse',
  types: [GraphQLList(PostType), ErrorType],
  resolveType(value) {
    if (_.isArray(value)) {
      return GraphQLList(PostType)
    }

    return ErrorType
  },
})

How can I define a GraphQLUnion where one type is a GraphQLList and the other type is not (a list)?

Upvotes: 1

Views: 95

Answers (1)

Andrew Ingram
Andrew Ingram

Reputation: 5240

You can't, the members of a union must be object types and not wrapping types (list and non-null are both wrapping types).

Upvotes: 1

Related Questions