cclark

cclark

How to use Enum and Custom Scalars together in Absinthe?

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.

Most Liked

kokolegorille

kokolegorille

Hello, welcome to the forum.

I use simple atoms, without as when I don’t need to use db, like so

  enum :sort_order do
    value :asc
    value :asc_nulls_last
    value :asc_nulls_first
    value :desc
    value :desc_nulls_last
    value :desc_nulls_first
  end

But if You want to persists Int in the DB and keep nice looking status, I would use a scalar. Nothing really complicate, You just need a parse and a serialize functions.

I use this for json.

  scalar :json do
    parse fn input ->
      case Jason.decode(input.value) do
        {:ok, result} -> result
        _ -> :error
      end
    end
    serialize &Jason.encode!/1
  end

And with your Enum requirement, it would be simple to write those 2 functions. For example…

scalar :status do
  parse fn 1 -> :green; 2 -> :yellow; 3 -> :amber; 4 -> :red; _ -> :error end
  serialize fn :green -> 1; :yellow -> 2; :amber -> 3; :red -> 4; _ -> :error end
end
StefanL

StefanL

You might want to have a look at EctoEnum, it will probably provide the casting from/to integers in ecto that you are searching for.

Where Next?

Popular in Questions Top

beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement