ngscheurich

ngscheurich

Polymorphic field in Absinthe

Hi all! My GraphQL education is still in a fairly nascent stage and I’ve run into an issue that seems like it should be easy to solve, yet I can’t figure out a solution. This makes me think I have a flawed mental model and/or a suboptimal implementation.

Here’s a brief rundown of the relevant pieces of my application, with some of my possibly incorrect understandings made explicit:

  • A PostgreSQL table, datapoints, that contains a payload column (among others) which is set to the map type in my Ecto migration. As far as I understand, this configuration means that Ecto will create that column as jsonb and encode/decode as appropriate.
  • The payload map should contain a :value key which is one of data shapes: a boolean value, a string, or a list of strings. I am enforcing this rule via various strategies which I can’t imagine have any bearing on my GraphQL conundrum.
  • An Elixir module that defines an Ecto schema for the datapoints table with field(:payload, :map).
  • An Elixir module that defines an Absinthe schema. It defines an object, :datapoint, as part of the root query object.

My issue comes into play when I try to define the :payload field on the :datapoint object in my schema, i.e.:

object :datapoint do
  field(:id, :id)
  field(:name, :string)
  field(:type, :string)
  field(:payload, # ??? #)
end

How should I tell Absinthe that this field should be one of type :boolean, :string, or list_of(:string)? I’ve gone down quite the rabbit hole of unions, interfaces, and custom scalars, but nothing seems quite right… I can share more code upon request, but I don’t want to start this off with a giant wall of text, and I’m hoping I’m missing something simple. :grimacing:

Any help is very greatly appreciated!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Once again you’re actually 100% on the right path. The GraphQL type system has your back here actually, because you’ve got 3 completely different return types, so it forces you to articulate the branches you want to handle:

query ($elderId: String!, $datapointName: String!, $count: Int) {
  datapointsForElder(elderId: $elderId, datapointName: $datapointName, count: $count) {
    name,
  	type,
    payload {
      ... on BooleanPayload { __typename value }
      ... on StringPayload { __typename value }
      ... on ListPayload { __typename value }
    }
  }
}

The addition of __typename is handy since it will annotate in the result JSON which outcome it was.

And yeah! I’m looking forward to the conference as well, please stop by and say hi!

10
Post #4

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @ngscheurich!

The GraphQL type system only permits unions between objects. This leads to the following slightly cumbersome solution:

object :boolean_value do
  field :value, non_null(:boolean)
end

object :string_value do
  field :value, non_null(:string)
end

object :string_list do
  field :value, list_of(:string)
end

union :payload do
  types [:string_value, :string_list, :boolean_value]

  resolve_type fn payload ->
    #exercise for the reader
  end
end
ngscheurich

ngscheurich

Thanks for the quick response, @benwilson512!

So, it seems like I was sort of on the right path, which is a nice sanity check. Here’s the code I was working on:

 object :boolean_payload do
    field(:value, :boolean)
  end

  object :string_payload do
    field(:value, :string)
  end

  object :list_payload do
    field(:value, list_of(:string))
  end

  union :payload do
    types([:boolean_payload, :string_payload, :list_payload])
    resolve_type(&resolve_payload_value/2)
  end

  defp resolve_payload_value(value, _) when is_boolean(value),
    do: :boolean_payload

  defp resolve_payload_value(value, _) when is_binary(value),
    do: :string_payload

  defp resolve_payload_value(value, _) when is_list(value),
    do: :list_payload

My confusion is around how to write the query, e.g., what I should sub-select on payload here:

query ($elderId: String!, $datapointName: String!, $count: Int) {
  datapointsForElder(elderId: $elderId, datapointName: $datapointName, count: $count) {
    name,
  	type,
    payload {
      ???
    }
  }
}

I’m looking at inline fragments at the moment, like ... on BooleanPayload { value } but something about that doesn’t seem quite right…

It seems like I have more of GraphQL problem, so I’ll study up on that part of the equation—any advice is, of course, appreciated though.

P. S. Looking forward to seeing you at The Big Elixir this week!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is really more of an ecto qustion than an Absinthe question. I’d use a custom ecto type that always returned dates instead of sometimes an atom. Your GraphQL clients shouldn’t know or care that you’re using mysql.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New

We're in Beta

About us Mission Statement