absinthe-graphql / absinthe_ecto

DEPRECATED: Use dataloader
MIT License
130 stars 36 forks source link

Support for custom foreign_key? #30

Closed bjunc closed 7 years ago

bjunc commented 7 years ago

It appears that the association is stitching together the field name and custom @primary_key, but ignoring foreign_key.

benwilson512 commented 7 years ago

I'm not sure I understand. Absinthe.Ecto doesn't explicitly use the primary key, or any other key at all. It relies entirely on your Ecto associations.

bjunc commented 7 years ago

Thanks for getting back to me @benwilson512. I believe this was user error. I was attempting to handle associations from a database that started its life as Drupal; which has a mixture of unconventional primary keys and foreign keys.

To simplify things, I created a test scenario that required custom foreign keys, and all went well (eerily well, actually). It looks something like this (loosely mimicking Facebook's Post Graph API):

# ecto schema
schema "posts" do
  field :message, :string
  belongs_to :from, User, foreign_key: :from_user_id
  belongs_to :to, User, foreign_key: :to_user_id

  timestamps()
end
# graphql types
object :user do
  field :id, :id
  field :email, :string
  field :first_name, :string
  field :last_name, :string
  field :wall, list_of(:post), resolve: assoc(:wall)
end

object :post do
  field :id, :id
  field :message, :string
  field :from, :user, resolve: assoc(:from)
end
# graphql query
{
  user (id: 1) {
    wall {
      message
      from {
        firstName
        lastName
      }
    }
  }
}

This results in two queries (one for posts, and one for users). Awesome!

benwilson512 commented 7 years ago

Cool!

rmap96 commented 6 years ago

@bjunc hello, I'm trying something similar, how did you define your "users" schema?