Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom foreign_key? #30

Closed
bjunc opened this issue Oct 11, 2017 · 4 comments
Closed

Support for custom foreign_key? #30

bjunc opened this issue Oct 11, 2017 · 4 comments

Comments

@bjunc
Copy link

bjunc commented Oct 11, 2017

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

@benwilson512
Copy link
Contributor

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
Copy link
Author

bjunc commented Oct 12, 2017

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
Copy link
Contributor

Cool!

@rmap96
Copy link

rmap96 commented Sep 10, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants