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

Fix loading for nested through associations #65

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ if Code.ensure_loaded?(Ecto) do
defp chase_down_queryable([field | fields], schema) do
case schema.__schema__(:association, field) do
%{queryable: queryable} ->
chase_down_queryable(fields, queryable)
chase_down_queryable(fields, queryable)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing an indent here?


%Ecto.Association.HasThrough{through: [through_field | through_fields]} ->
[through_field | through_fields ++ fields]
|> chase_down_queryable(schema)
end
end

Expand Down
10 changes: 10 additions & 0 deletions priv/test_repo/migrations/1_migrate_all.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ defmodule Absinthe.Ecto.TestRepo.Migrations.MigrateAll do
use Ecto.Migration

def change do
create table(:leaderboards) do
add :name, :string
end

create table(:users) do
add :username, :string
add :leaderboard_id, references(:leaderboards)
end

create table(:posts) do
Expand All @@ -16,5 +21,10 @@ defmodule Absinthe.Ecto.TestRepo.Migrations.MigrateAll do
add :user_id, references(:users), null: false
add :post_id, references(:posts), null: false
end

create table(:scores) do
add :leaderboard_id, references(:leaderboards), null: false
add :post_id, references(:posts), null: false
end
end
end
29 changes: 28 additions & 1 deletion test/dataloader/ecto_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Dataloader.EctoTest do
use ExUnit.Case, async: true

alias Dataloader.{User, Post, Like}
alias Dataloader.{User, Post, Like, Score, Leaderboard}
import Ecto.Query
alias Dataloader.TestRepo, as: Repo

Expand Down Expand Up @@ -291,6 +291,33 @@ defmodule Dataloader.EctoTest do
assert length(loaded_posts) == 2
end

test "works with nested has many through", %{loader: loader} do
leaderboard = %Leaderboard{name: "Bestliked"} |> Repo.insert!()
user = %User{username: "Ben Wilson", leaderboard_id: leaderboard.id} |> Repo.insert!()

post = %Post{user_id: user.id} |> Repo.insert!()
_score = %Score{post_id: post.id, leaderboard_id: leaderboard.id} |> Repo.insert!()
_like1 = %Like{post_id: post.id, user_id: user.id} |> Repo.insert!()
_like2 = %Like{post_id: post.id, user_id: user.id} |> Repo.insert!()

loader =
loader
|> Dataloader.load(Test, :awarded_posts, user)
|> Dataloader.load(Test, :likes, user)
|> Dataloader.run()

loaded_posts =
loader
|> Dataloader.get(Test, :awarded_posts, user)
loaded_likes =
loader
|> Dataloader.get(Test, :likes, user)


assert length(loaded_posts) == 1
assert length(loaded_likes) == 2
end

test "preloads aren't used", %{loader: loader} do
user = %User{username: "Ben Wilson"} |> Repo.insert!()

Expand Down
8 changes: 8 additions & 0 deletions test/support/leaderboard.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Dataloader.Leaderboard do
use Ecto.Schema

schema "leaderboards" do
field(:name, :string)
has_many(:scores, Dataloader.Score)
end
end
8 changes: 8 additions & 0 deletions test/support/score.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Dataloader.Score do
use Ecto.Schema

schema "scores" do
belongs_to(:post, Dataloader.Post)
belongs_to(:leaderboard, Dataloader.Leaderboard)
end
end
5 changes: 5 additions & 0 deletions test/support/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ defmodule Dataloader.User do
schema "users" do
field(:username, :string)
has_many(:posts, Dataloader.Post)
belongs_to(:leaderboard, Dataloader.Leaderboard)

has_many(:scores, through: [:leaderboard, :scores])
has_many(:awarded_posts, through: [:scores, :post])
has_many(:likes, through: [:awarded_posts, :likes])
end
end