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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
Tee
can someone please explain to me how Enum.reduce works with maps
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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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

Other popular topics Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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

We're in Beta

About us Mission Statement