Skip to content
Advertisement

How to define Nested enum type in GraphQL schema

I have an enum defined in graphql schema as below:

enum QueryType {
    NORMAL, RANGE, REGEX, WILDCARD
}

But the WILDCARD query, can be further specified as STARTSWITH, ENDSWITH and CONTAINS,

So can we achieve something like below while defining Schema

 enum QueryType {
        NORMAL, RANGE, REGEX, enum WILDCARD { STARTSWITH, ENDSWITH, CONTAINS}
    }

or

 enum QueryType {
            NORMAL, RANGE, REGEX, WILDCARD: WildCardType
        }
   enum WildCardType { STARTSWITH, ENDSWITH, CONTAINS}

Advertisement

Answer

There are no such nested enums in GraphQL schema. You can consider to model it using two separate fields with separate enums .

enum QueryType {
    NORMAL ,RANGE , REGEX , WILDCARD
}

enum WildcardType {
    STARTSWITH , ENDSWITH ,CONTAINS
}

And suppose an object type call SearchQuery need to contains these info . Its schema may look likes:

type SearchQuery {
    queryType : QueryType!
    wildcardType : WildcardType
}

wildcardType is an optional field which only contains the value if the queryType is WILDCARD. Otherwise , it should be null.

Alternatively you can also consider to model it to be more ‘polymorphism’ style using the interface. Something likes :

interface SearchQuery {
  queryType: QueryType!
}

type WildCardQuery implements  SearchQuery {
    queryType: QueryType!
    wildcardType : WildcardType!
}

type NonWildCardQuery implements SearchQuery{
    queryType: QueryType!
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement