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

Add limit & filter to scores #9

Merged
merged 1 commit into from
Jul 22, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/scoreboard/games/games.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@ defmodule Scoreboard.Games do
import Ecto.Query, warn: false
alias Scoreboard.Repo

alias Scoreboard.Games.Player
alias Scoreboard.Games.{Player, Score}

def data() do
Dataloader.Ecto.new(Scoreboard.Repo, query: &query/2)
end

def query(queryable, _params) do
queryable
@doc """
Pattern match to build more specific queries
"""
def query(Score, params) do
params
|> Map.put_new(:limit, 10)
|> Map.to_list()
|> Enum.reduce(Score, &apply_param/2)
end
def query(queryable, _params), do: queryable

@doc """
reduce function
"""
def apply_param({:limit, num}, queryable), do: queryable |> limit(^num)
def apply_param({:player_id, player_id}, queryable), do: queryable |> where([score], score.player_id == ^player_id)
def apply_param(_param, queryable), do: queryable


def get(queryable, id) do
case Repo.get(queryable, id) do
Expand Down
6 changes: 5 additions & 1 deletion lib/scoreboard_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ defmodule ScoreboardWeb.Schema do
object :game do
field(:id, non_null(:id))
field(:name, non_null(:string))
field(:scores, list_of(:score), resolve: dataloader(:games))
field :scores, list_of(:score) do
arg :limit, :integer
arg :player_id, :id
resolve dataloader(:games)
end
end

@desc "A Player"
Expand Down
49 changes: 49 additions & 0 deletions test/scoreboard_web/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,55 @@ defmodule Scoreboard.SchemaTest do
assert get_in(data, ["game", "scores", first, "total"]) == score.total
assert get_in(data, ["game", "scores", first, "player", "name"]) == context.player.name
end

test("passing limit param to scores query", context) do
Repo.insert!(%Score{game_id: context.game.id, player_id: context.player.id, total: 101})

document = """
{
game(id: "#{context.game.id}") {
id
name
scores(limit: 0) {
total
}
}
}
"""

result = Absinthe.run(document, ScoreboardWeb.Schema)

assert {:ok, %{data: data}} = result
assert length(get_in(data, ["game", "scores"])) == 0
end

test("passing limit & player_id param to scores query", context) do
sam = Repo.insert!(%Player{name: "Sam"})
Repo.insert!(%Score{game_id: context.game.id, player_id: context.player.id, total: 101})
Repo.insert!(%Score{game_id: context.game.id, player_id: sam.id, total: 12})

document = """
{
game(id: "#{context.game.id}") {
id
name
scores(limit: 5, player_id: "#{context.player.id}") {
total
player {
id
}
}
}
}
"""

result = Absinthe.run(document, ScoreboardWeb.Schema)

assert {:ok, %{data: data}} = result
for %{"player" => %{"id" => id}} = _score <- get_in(data, ["game", "scores"]) do
assert id == context.player.id
end
end
end

describe("mutation queries") do
Expand Down