question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No custom enums as types in schema

See original GitHub issue

I’d like to create and register a custom enum, for example:

enum AllowedColor {
    RED
    GREEN
    BLUE
}

and in my schema, to use it as a type:

color: {
    label: 'Color',
    type: AllowedColor,
    input: 'select',
    options: function() {
      // options for the select form control
      return [
        { value: 'RED', label: 'Red' },
        { value: 'GREEN', label: 'Green' },
        { value: 'BLUE', label: 'Blue' },
      ];
    }
  }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-burelcommented, Aug 28, 2019

Hi,

We’ve discussed it with @Apollinaire and it actually seems to be a counter-productive idea, especially when taking values from allowedValues.

The biggest issue is that enums need to have a very specific format, the “Name” format in the GraphQL spec, which disallows most chars excepts letters, while allowedValues can be rich strings.

So the current solution to this issue specifying a custom GraphQL type for a field (not sure if we can do that yet? if not we will add this feature soon) + use the addGraphQLSchema function to define a new type.

We could support an additional field in the schema to directly specify enums but I am not sure the use case is worth it.

0reactions
eric-burelcommented, Oct 28, 2019

I confirm we will add the possibility to provide your own graphQL type in the future.

The current solution should be (not tested):

// trick: use a resolveAs to force a graphql field
myEnum: {
    type: String,
    resolveAs: {
         type: 'MyEnum'
    }
}
// add your enum type manually (we can't generate it for you)
addGraphQLSchema(`
    MyEnum Foobar Hello World
`)

In the future we will add a simpler way to specify a custom GraphQL type, we did not agree on an intuitive field name/syntax yet. I keep the issue open for reference but technically it is solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swagger schema not showing custom value of enum
But the swagger.json is showing DOG and CAT in place of dog and cat . I don't want the user to see the...
Read more >
[Question] How to define a custom enum property and show it ...
Background: There is a way to define enum fields in json-schema and there is a way to define enums in api-platform. Questions:.
Read more >
Enums - Swagger
Reusable enums​​ In OpenAPI 3.0, both operation parameters and data models use a schema , making it easy to reuse the data types....
Read more >
Use the new Enum type in Ecto 3.5 | Dennis Beatty
Enum which allows you to store enums in your database without any other ... EctoEnum library has been the best way to set...
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An enumeration value can also be the empty string ( '' ) or NULL under certain circumstances: If you insert an invalid value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found