Alisson Alisson - 6 days ago 5
React JSX Question

How to list members from an GraphQL's Enum type with ReactJS

I have a GraphQL mutation that accepts an Enum as one of the inputs, I would like to list those enum's members as a select on the UI. How should I do that? Do I need to hard code the members to the select?

Answer

You can use the __type field to get an enum type's values. For example, you could ask for the values of a Color enum:

{
   __type(name: "Color") {
     enumValues {
       name 
     }
   }
}

And it might return a response like:

{
  "data" : {
    "__type" : {
      "enumValues" : [
        { "name" : "WHITE" },
        { "name" : "BLUE" },
        { "name" : "BLACK" },
        { "name" : "RED" },
        { "name" : "GREEN" },
      ]
    }
  }
}

Then, you could use that response to render the UI.

More info about GraphQL Introspection: http://graphql.org/learn/introspection/

Comments