Skip to content

Commit

Permalink
Merge pull request #8 from shamshirz/first-mutation
Browse files Browse the repository at this point in the history
First mutation
  • Loading branch information
shamshirz authored Jul 22, 2018
2 parents 8d69aa5 + 76c4c9a commit 2cf3524
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Browser games and leaderboards. You play them, you love them, let's make a serve
To start your Phoenix server:

* Install dependencies with `mix deps.get`
* Create and migrate your database with `mix ecto.create && mix ecto.migrate`
* Create and migrate your database with `mix ecto.setup`
* Start Phoenix endpoint with `mix phx.server`
* Hit it `um…TODO`

Expand Down Expand Up @@ -134,6 +134,10 @@ TO SELF - Point out verbosity and repeated simple functions of the normal ecto c
Point out reduced line count, show a PR maybe.


### Step 5 - Mutation

Link to this PR.

## Learn more

Add my own deets here
Expand Down
4 changes: 2 additions & 2 deletions lib/scoreboard/games/score.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Scoreboard.Games.Score do
@doc false
def changeset(score, attrs) do
score
|> cast(attrs, [:total])
|> validate_required([:total])
|> cast(attrs, [:total, :player_id, :game_id])
|> validate_required([:total, :player_id, :game_id])
end
end
5 changes: 5 additions & 0 deletions lib/scoreboard_web/resolvers/games.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule ScoreboardWeb.Resolvers.Games do

def submit_score(args, _res), do: Scoreboard.Games.create_score(args)

end
12 changes: 12 additions & 0 deletions lib/scoreboard_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ScoreboardWeb.Schema do
use Absinthe.Schema
import Absinthe.Resolution.Helpers, only: [dataloader: 1]
alias Scoreboard.Games
alias ScoreboardWeb.Resolvers

def context(ctx) do
loader =
Expand Down Expand Up @@ -53,4 +54,15 @@ defmodule ScoreboardWeb.Schema do
end)
end
end

mutation do
@desc "Submit a score"
field :submit_score, type: :score do
arg(:game_id, non_null(:id))
arg(:player_id, non_null(:id))
arg(:total, non_null(:integer))

resolve(&Resolvers.Games.submit_score/2)
end
end
end
15 changes: 13 additions & 2 deletions test/scoreboard/games/games_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,24 @@ defmodule Scoreboard.GamesTest do
end

describe "scores" do
alias Scoreboard.Games.Score
alias Scoreboard.Games.{Game, Player, Score}

@valid_attrs %{total: 42}
@update_attrs %{total: 43}
@invalid_attrs %{total: nil}

def game_and_player() do
game = Repo.insert!(%Game{name: "Code Simulator '08"})
player = Repo.insert!(%Player{name: "Aaron"})
%{game: game, player: player}
end

def score_fixture(attrs \\ %{}) do
%{game: game, player: player} = game_and_player()

{:ok, score} =
attrs
|> Map.merge(%{game_id: game.id, player_id: player.id})
|> Enum.into(@valid_attrs)
|> Games.create_score()

Expand All @@ -150,7 +159,9 @@ defmodule Scoreboard.GamesTest do
end

test "create_score/1 with valid data creates a score" do
assert {:ok, %Score{} = score} = Games.create_score(@valid_attrs)
%{game: game, player: player} = game_and_player()

assert {:ok, %Score{} = score} = Games.create_score(@valid_attrs |> Map.merge(%{game_id: game.id, player_id: player.id}))
assert score.total == 42
end

Expand Down
27 changes: 27 additions & 0 deletions test/scoreboard_web/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,31 @@ defmodule Scoreboard.SchemaTest do
assert get_in(data, ["game", "scores", first, "player", "name"]) == context.player.name
end
end

describe("mutation queries") do
test("create score", context) do
score = 37
document = """
mutation {
submitScore(player_id: "#{context.player.id}", game_id: "#{context.game.id}", total: #{score}) {
id
total
player {
name
}
game {
name
}
}
}
"""

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

assert {:ok, %{data: data}} = result
assert get_in(data, ["submitScore", "game", "name"]) == context.game.name
assert get_in(data, ["submitScore", "player", "name"]) == context.player.name
assert get_in(data, ["submitScore", "total"]) == score
end
end
end

0 comments on commit 2cf3524

Please sign in to comment.