I’m new to Elixir, Phoenix and Absinthe but trying to write an API onto an existing dataset. The dataset includes information about trails and they have a status attribute with values of 1, 2, 3 or 4. In the domain these map to Green (1), Yellow(2), Amber(3) and Red(4) so I’d like to use the actual enum values as it would be much more expressive.
For the GraphQL API onto this it would be much nicer if I could write a query like:
query {
trails(havingStatus: RED) {
id
name
status
}
}
In my schema I’ve defined an enum as
@desc "Current status of the trail"
enum :trail_status do
value :green, as: 1, description: "Green: Clear"
value :yellow, as: 2, description: "Yellow: Minor Issue"
value :amber, as: 3, description: "Amber: Significant Issue"
value :red, as: 4, description: "Red: Major Issue"
end
This allows me to query and enforce that the user chooses valid values for havingStatus. However, in my results I see the raw value of
{
"status": 4,
"name": "Lost Loop",
"id": "16601"
}
where I’d really like to have the 3 be returned as RED.
First does this make sense to do? If the client already knows about GREEN, YELLOW, AMBER and RED when making the query then it seems like a failure to make the client know how to receive 1-4 and map it to those values.
It seems like a custom scalar might be one way to get at this. All of the examples I can find are for dates and make sense. But when I have a very specific enumeration of four values I wonder if that is overkill and it seems like I’d lose the nice ability to introspect and see the only valid values. So I think I want a combination of an enum and a custom scalar type?
Any thoughts or pointers to Absinthe/GraphQL articles on the subject would be greatly appreciated.