Enums
#
Recommended background reading#
Enums in RescriptRelayEnums are modelled as polymorphic variants in RescriptRelay. Here's an example. Imagine the following GraphQL schema:
Now, imagine we're writing a component that wants to show whether a particular ticket is rejected or not:
Notice the catch all case _ => React.null
- the way the enum type is shaped in ReScript ensures that we're enforced to add that catch all case. This will help you protect your app against runtime errors if there's a new value added to the enum before you have a chance to update your app.
#
Enums in inputsEnums coming from the server are unsafe in the sense that the server could at any point add or remove members from the enum, before you have a chance to update your code. To tackle this, any enum in responses from the server will enforce having a default, catch-all case, to handle the fact that unknown values can show up.
However, things are different in inputs. While the enum shape might change on the sever before you've had a chance to deploy new code, unknown values can't get passed into your program. Therefore, enums in inputs are considered exact, and are emitted as enum_TheEnumName_input
in the generated code.
#
I need to use the generated enum elsewhere in my programAlways prefer using enum_TheEnumName_input
(the enum suffixed with _input
).
#
Wrapping upNow you know all about enums in RescriptRelay!