ngscheurich
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 apayloadcolumn (among others) which is set to themaptype in my Ecto migration. As far as I understand, this configuration means that Ecto will create that column asjsonband encode/decode as appropriate. - The
payloadmap should contain a:valuekey 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
datapointstable withfield(: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. ![]()
Any help is very greatly appreciated!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
Hey @ngscheurich!
The GraphQL type system only permits unions between objects. This leads to the following slightly cumbersome solution:
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:
My confusion is around how to write the query, e.g., what I should sub-select on
payloadhere: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
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:
The addition of
__typenameis 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!
bossyang
I have a similar issue. The MyXQL returns zero dates (
0000-00-00) as atom:zero_date. So the field could be either:datetype or:string? Right now I try to fix the data in the legacy system. Or I should create a new Custom Type.benwilson512
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.
bossyang
Thank you for your suggestion.