JosipSylo

JosipSylo

Many to many with an extra column using Absinthe

By reading through previous questions and issues I converted my schemas from using a “many_to_many” to “has_many” and “belongs_to” in order to get the extra field from the join table. Now I’m a little bit stuck and not sure how to access it using Absinthe.
I have “users” and a “feedbacks”. It is a many to many relationship through the “users__feedbacks” table.
“users__feedbacks” table also has an extra field called “is_published”. How can I get that field in my graphql query?

These are my schemas:

  schema "users" do
    field :first_name, :string
    has_many :users__feedbacks, App.Feedbacks.Users_Feedback
    has_many :feedbacks_as_reviewer, through: [:users__feedbacks, :feedback]

    timestamps()
  end
  schema "feedbacks" do
    field :title, :string
    has_many :users__feedbacks, App.Feedbacks.Users_Feedback
    has_many :reviewers, through: [:users__feedbacks, :user]
    timestamps()
  end
  schema "users__feedbacks" do
    belongs_to :user, Accounts.User
    belongs_to :feedback, Feedbacks.Feedback
    field :is_published, :boolean

    timestamps()
  end

These are my graphql query types

  object :user do
    field :id, non_null(:id)
    field :first_name, :string
    field :feedbacks_as_reviewer, list_of(:feedback), resolve: dataloader(:db)
  end
  object :feedback do
    field :id, non_null(:id)
    field :title, non_null(:string)
    field :reviewers, list_of(:user), resolve: dataloader(:db)
  end

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @JosipSylo welcome!

I think your best option is to create an intermediate object with a name that reflects what’s going on. Based on your sketch of the domain, I think the users__fedbacks table is really a “feedback_reviews” table. This doesn’t mean you need to rename it, but you can name your associations better, and expose GraphQL objects named that way:

object :feedback_review do
  field :is_published, :boolean
  field :reviewer, :user
  field :feedback, :feedback
end

object :user do
  field :feedback_reviews, list_of(:feedback_review)
  # other fields here
end

You can make your life easier by naming your ecto associations that way too, they don’t need to match the table name, you can do: has_many :feedback_reviews, App.Feedbacks.Users_Feedback. and so on.

Also Liked

JosipSylo

JosipSylo

Thank you for the feedback @benwilson512 . I ended up restructuring things as you suggested and it made things a lot more readable.

Where Next?

Popular in Questions Top

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
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
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
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
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement