joshsmith

joshsmith

How to query a union of types with Absinthe.Relay.Connection.from_query

I’m trying to do cursor-based pagination across a union of several different types, each representing different Ecto schemas like so:

union :event do
  types([:identify, :page, :track])

  resolve_type(fn
    %Events.Identify{}, _ -> :identify
    %Events.Page{}, _ -> :page
    %Events.Track{}, _ -> :track
  end)
end

connection(node_type: :event)

I’m trying to figure out a way to use Absinthe.Relay.Connection.from_query/4 so I can pass the pagination_args into my query and honestly not sure how to even approach the problem.

Has anyone dealt with this before or can point me in the right direction?

The way I have this working right now is to simply execute three different SQL queries and then sort them in Elixir (results = identifies ++ pages ++ tracks), then do Absinthe.Relay.Connection.from_list(results, pagination_args). This obviously means I have to do three very large queries in order to retrieve everything and is not ideal.

I attempted an approach using Ecto.Query.union/2 but really am not sure how this would work either.

Thanks in advance!

First Post!

venkatd

venkatd

I would setup a table named something like events with a polymorphic association to the identify, page, track tables. This table would have identify_id, page_id, track_id columns.

See here for more details:

You’ll have a bit more complexity to deal with in maintaining this 4th events table, but it should simplify your query logic and result in better performance.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

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
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
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement