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:
https://hexdocs.pm/ecto/Ecto.Schema.html#belongs_to/3-polymorphic-associations

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

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
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement