Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL enum union workaround?

Tags:

enums

graphql

I have Major categories and Minor Categories that belong to a Major category. Both are ENUM type. I want client to choose matching minor category ENUM to submit with its Major category. I don't want to include all different minor category ENUMs as fields.

I first tried doing union MinorCategories = Minor1 | Minor2 However this failed because union only works with ObjectTypes

Enforcing minor category depending on the major category is not necessary. I only want to receive one field that can be selective by the client by ENUM. Is there any work around?

like image 236
J.S.C Avatar asked Sep 06 '19 05:09

J.S.C


1 Answers

GraphQL does not support union types for scalar values, only for object types. One option, albeit an ugly one, is to wrap your enums in an object type.

type Minor1Wrapper {
  value: Minor1!
}

type Minor2Wrapper {
  value: Minor2!
}

union MinorCategories = Minor1Wrapper | Minor2Wrapper
like image 178
Gautham Acharya Avatar answered Nov 05 '22 01:11

Gautham Acharya