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

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.

2 Likes

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

2 Likes