DGraph Support for Enums

Hi! I’m working on a schema for my company, we have an entity which could be one of a set of types. An analogous example is cups, if we need to represent an inventory of cups and want to store their color which can only be Blue and Orange, then have a custom UI with special behaviour based on this value, we want an enum. Strings aren’t restrictive enough and allow for values other than Blue and Orange, and I’d prefer to use an enum. According to the post linked below, it seems as though enums aren’t supported by DGraph, though the DGraph GraphQL docs, also linked below, show enum support. My question is, can DGraph schemas define enums? It’s really not clear, the docs say yes, the forum says no and trying to define one results in an error message. If not, is there an alternative solution?

Forum post: Ratel edit schema doesnt support enum
DGraph GraphQL docs: https://dgraph.io/docs/graphql/schema/types/

1 Like

Dgraph’ GraphQL API which is in the core of Dgraph can handle enums in the GraphQL SDL, but the enum controls are not passed down to the DQL schema generation. So it would be possible from the DQL endpoints to mutate and query any strong value, but the GraphQL API would error if it finds a value not conformant to the enum in the GraphQL SDL.

Is it possible to allow enums during schema generation even though they’re not used? I am trying to reconcile a GraphQL schema that I would like to use both for Dgraph and for Apollo. It would seem strange to require managing separate schemas due to this issue.

By Apollo, do you mean GraphQL??

Dgraph has a GraphQL endpoint and if you provide a GraphQL schema, it will alter the DQL schema to match. But any enums in the DQL schema will simply be string fields.

What are you trying to do?

I have a GraphQL schema and am using the gRPC JavaScript library to import the schema and it fails because I have enums in my schema. Also fails with GraphQL comments """ Like these """.

Use the admin endpoint to set the GraphQL schema.

Is there no client support for importing GraphQL schemas? I suppose the alternative is using a raw HTTP connection? And is that endpoint still on the Alpha server, but HTTP port?

The admin endpoint is a GraphQL API, so any GraphQL client will work or you can curl to it or fetch, etc.

Thanks a lot! I’ll give this a go.