commit ed4eac2ed707067e2525dfb20467f84939505715
parent 82e3944b262951f8cf5d34ef9dba85699dab5711
Author: srfsh <dev@srf.sh>
Date: Tue, 23 Aug 2022 09:15:39 +0300
Zenflows{Test,}.VF.RecipeExchange: add paging support and small improvements
Diffstat:
5 files changed, 128 insertions(+), 94 deletions(-)
diff --git a/src/zenflows/vf/recipe_exchange/domain.ex b/src/zenflows/vf/recipe_exchange/domain.ex
@@ -20,35 +20,48 @@ defmodule Zenflows.VF.RecipeExchange.Domain do
alias Ecto.Multi
alias Zenflows.DB.Repo
+alias Zenflows.GQL.Paging
alias Zenflows.VF.RecipeExchange
@typep repo() :: Ecto.Repo.t()
@typep chgset() :: Ecto.Changeset.t()
-@typep changes() :: Ecto.Multi.changes()
@typep id() :: Zenflows.DB.Schema.id()
@typep params() :: Zenflows.DB.Schema.params()
-@spec by_id(repo(), id()) :: RecipeExchange.t() | nil
-def by_id(repo \\ Repo, id) do
- repo.get(RecipeExchange, id)
+@spec one(repo(), id() | map() | Keyword.t())
+ :: {:ok, RecipeExchange.t()} | {:error, String.t()}
+def one(repo \\ Repo, _)
+def one(repo, id) when is_binary(id), do: one(repo, id: id)
+def one(repo, clauses) do
+ case repo.get_by(RecipeExchange, clauses) do
+ nil -> {:error, "not found"}
+ found -> {:ok, found}
+ end
+end
+
+@spec all(Paging.params()) :: Paging.result(RecipeExchange.t())
+def all(params) do
+ Paging.page(RecipeExchange, params)
end
@spec create(params()) :: {:ok, RecipeExchange.t()} | {:error, chgset()}
def create(params) do
Multi.new()
- |> Multi.insert(:rec_exch, RecipeExchange.chgset(params))
+ |> Multi.insert(:insert, RecipeExchange.chgset(params))
|> Repo.transaction()
|> case do
- {:ok, %{rec_exch: re}} -> {:ok, re}
+ {:ok, %{insert: re}} -> {:ok, re}
{:error, _, cset, _} -> {:error, cset}
end
end
-@spec update(id(), params()) :: {:ok, RecipeExchange.t()} | {:error, chgset()}
+@spec update(id(), params())
+ :: {:ok, RecipeExchange.t()} | {:error, String.t() | chgset()}
def update(id, params) do
Multi.new()
- |> Multi.run(:get, multi_get(id))
- |> Multi.update(:update, &RecipeExchange.chgset(&1.get, params))
+ |> Multi.put(:id, id)
+ |> Multi.run(:one, &one/2)
+ |> Multi.update(:update, &RecipeExchange.chgset(&1.one, params))
|> Repo.transaction()
|> case do
{:ok, %{update: re}} -> {:ok, re}
@@ -56,27 +69,17 @@ def update(id, params) do
end
end
-@spec delete(id()) :: {:ok, RecipeExchange.t()} | {:error, chgset()}
+@spec delete(id())
+ :: {:ok, RecipeExchange.t()} | {:error, String.t() | chgset()}
def delete(id) do
Multi.new()
- |> Multi.run(:get, multi_get(id))
- |> Multi.delete(:delete, &(&1.get))
+ |> Multi.put(:id, id)
+ |> Multi.run(:one, &one/2)
+ |> Multi.delete(:delete, & &1.one)
|> Repo.transaction()
|> case do
{:ok, %{delete: re}} -> {:ok, re}
{:error, _, msg_or_cset, _} -> {:error, msg_or_cset}
end
end
-
-# Returns a RecipeExchange in ok-err tuple from given ID. Used inside
-# Ecto.Multi.run/5 to get a record in transaction.
-@spec multi_get(id()) :: (repo(), changes() -> {:ok, RecipeExchange.t()} | {:error, String.t()})
-defp multi_get(id) do
- fn repo, _ ->
- case by_id(repo, id) do
- nil -> {:error, "not found"}
- re -> {:ok, re}
- end
- end
-end
end
diff --git a/src/zenflows/vf/recipe_exchange/resolv.ex b/src/zenflows/vf/recipe_exchange/resolv.ex
@@ -20,23 +20,27 @@ defmodule Zenflows.VF.RecipeExchange.Resolv do
alias Zenflows.VF.RecipeExchange.Domain
-def recipe_exchange(%{id: id}, _info) do
- {:ok, Domain.by_id(id)}
+def recipe_exchange(params, _) do
+ Domain.one(params)
end
-def create_recipe_exchange(%{recipe_exchange: params}, _info) do
+def recipe_exchanges(params, _) do
+ Domain.all(params)
+end
+
+def create_recipe_exchange(%{recipe_exchange: params}, _) do
with {:ok, rec_exch} <- Domain.create(params) do
{:ok, %{recipe_exchange: rec_exch}}
end
end
-def update_recipe_exchange(%{recipe_exchange: %{id: id} = params}, _info) do
+def update_recipe_exchange(%{recipe_exchange: %{id: id} = params}, _) do
with {:ok, rec_exch} <- Domain.update(id, params) do
{:ok, %{recipe_exchange: rec_exch}}
end
end
-def delete_recipe_exchange(%{id: id}, _info) do
+def delete_recipe_exchange(%{id: id}, _) do
with {:ok, _} <- Domain.delete(id) do
{:ok, true}
end
diff --git a/src/zenflows/vf/recipe_exchange/type.ex b/src/zenflows/vf/recipe_exchange/type.ex
@@ -39,10 +39,6 @@ object :recipe_exchange do
field :note, :string
end
-object :recipe_exchange_response do
- field :recipe_exchange, non_null(:recipe_exchange)
-end
-
input_object :recipe_exchange_create_params do
@desc @name
field :name, non_null(:string)
@@ -61,11 +57,33 @@ input_object :recipe_exchange_update_params do
field :note, :string
end
+object :recipe_exchange_response do
+ field :recipe_exchange, non_null(:recipe_exchange)
+end
+
+object :recipe_exchange_edge do
+ field :cursor, non_null(:id)
+ field :node, non_null(:recipe_exchange)
+end
+
+object :recipe_exchange_connection do
+ field :page_info, non_null(:page_info)
+ field :edges, non_null(list_of(non_null(:recipe_exchange_edge)))
+end
+
object :query_recipe_exchange do
field :recipe_exchange, :recipe_exchange do
arg :id, non_null(:id)
resolve &Resolv.recipe_exchange/2
end
+
+ field :recipe_exchanges, :recipe_exchange_connection do
+ arg :first, :integer
+ arg :after, :id
+ arg :last, :integer
+ arg :before, :id
+ resolve &Resolv.recipe_exchanges/2
+ end
end
object :mutation_recipe_exchange do
diff --git a/test/vf/recipe_exchange/domain.test.exs b/test/vf/recipe_exchange/domain.test.exs
@@ -27,45 +27,54 @@ setup do
name: Factory.uniq("name"),
note: Factory.uniq("note"),
},
- recipe_exchange: Factory.insert!(:recipe_exchange),
+ inserted: Factory.insert!(:recipe_exchange),
}
end
-test "by_id/1 returns a RecipeExchange", %{recipe_exchange: rec_exch} do
- assert %RecipeExchange{} = Domain.by_id(rec_exch.id)
+describe "one/1" do
+ test "with good id: finds the RecipeExchange", %{inserted: %{id: id}} do
+ assert {:ok, %RecipeExchange{}} = Domain.one(id)
+ end
+
+ test "with bad id: doesn't find the RecipeExchange" do
+ assert {:error, "not found"} = Domain.one(Factory.id())
+ end
end
describe "create/1" do
- test "creates a RecipeExchange with valid params", %{params: params} do
- assert {:ok, %RecipeExchange{} = rec_exch} = Domain.create(params)
-
- assert rec_exch.name == params.name
- assert rec_exch.note == params.note
+ test "with good params: creates a RecipeExchange", %{params: params} do
+ assert {:ok, %RecipeExchange{} = new} = Domain.create(params)
+ assert new.name == params.name
+ assert new.note == params.note
end
- test "doesn't create a RecipeExchange with invalid params" do
+ test "with bad params: doesn't create a Process" do
assert {:error, %Changeset{}} = Domain.create(%{})
end
end
describe "update/2" do
- test "updates a RecipeExchange with valid params", %{params: params, recipe_exchange: old} do
+ test "with good params: updates the RecipeExchange", %{params: params, inserted: old} do
assert {:ok, %RecipeExchange{} = new} = Domain.update(old.id, params)
-
assert new.name == params.name
assert new.note == params.note
end
- test "doesn't update a RecipeExchange", %{recipe_exchange: old} do
+ test "with bad params: doesn't update the RecipeExchange", %{inserted: old} do
assert {:ok, %RecipeExchange{} = new} = Domain.update(old.id, %{})
-
assert new.name == old.name
assert new.note == old.note
end
end
-test "delete/1 deletes a RecipeExchange", %{recipe_exchange: %{id: id}} do
- assert {:ok, %RecipeExchange{id: ^id}} = Domain.delete(id)
- assert Domain.by_id(id) == nil
+describe "delete/1" do
+ test "with good id: deletes the RecipeExchange", %{inserted: %{id: id}} do
+ assert {:ok, %RecipeExchange{id: ^id}} = Domain.delete(id)
+ assert {:error, "not found"} = Domain.one(id)
+ end
+
+ test "with bad id: doesn't delete the RecipeExchange" do
+ assert {:error, "not found"} = Domain.delete(Factory.id())
+ end
end
end
diff --git a/test/vf/recipe_exchange/type.test.exs b/test/vf/recipe_exchange/type.test.exs
@@ -21,77 +21,77 @@ use ZenflowsTest.Help.AbsinCase, async: true
setup do
%{
params: %{
- name: Factory.uniq("name"),
- note: Factory.uniq("note"),
+ "name" => Factory.uniq("name"),
+ "note" => Factory.uniq("note"),
},
- recipe_exchange: Factory.insert!(:recipe_exchange),
+ inserted: Factory.insert!(:recipe_exchange),
}
end
+@frag """
+fragment recipeExchange on RecipeExchange {
+ id
+ name
+ note
+}
+"""
+
describe "Query" do
- test "recipeExchange()", %{recipe_exchange: rec_exch} do
+ test "recipeExchange", %{inserted: new} do
assert %{data: %{"recipeExchange" => data}} =
- query!("""
- recipeExchange(id: "#{rec_exch.id}") {
- id
- name
- note
+ run!("""
+ #{@frag}
+ query ($id: ID!) {
+ recipeExchange(id: $id) {...recipeExchange}
}
- """)
+ """, vars: %{"id" => new.id})
- assert data["id"] == rec_exch.id
- assert data["name"] == rec_exch.name
- assert data["note"] == rec_exch.note
+ assert data["id"] == new.id
+ assert data["name"] == new.name
+ assert data["note"] == new.note
end
end
describe "Mutation" do
- test "createRecipeExchange()", %{params: params} do
+ test "createRecipeExchange", %{params: params} do
assert %{data: %{"createRecipeExchange" => %{"recipeExchange" => data}}} =
- mutation!("""
- createRecipeExchange(recipeExchange: {
- name: "#{params.name}"
- note: "#{params.note}"
- }) {
- recipeExchange {
- id
- name
- note
+ run!("""
+ #{@frag}
+ mutation ($recipeExchange: RecipeExchangeCreateParams!) {
+ createRecipeExchange(recipeExchange: $recipeExchange) {
+ recipeExchange {...recipeExchange}
}
}
- """)
+ """, vars: %{"recipeExchange" => params})
assert {:ok, _} = Zenflows.DB.ID.cast(data["id"])
- assert data["name"] == params.name
- assert data["note"] == params.note
+ assert data["name"] == params["name"]
+ assert data["note"] == params["note"]
end
- test "updateRecipeExchange()", %{params: params, recipe_exchange: rec_exch} do
+ test "updateRecipeExchange", %{params: params, inserted: old} do
assert %{data: %{"updateRecipeExchange" => %{"recipeExchange" => data}}} =
- mutation!("""
- updateRecipeExchange(recipeExchange: {
- id: "#{rec_exch.id}"
- name: "#{params.name}"
- note: "#{params.note}"
- }) {
- recipeExchange {
- id
- name
- note
+ run!("""
+ #{@frag}
+ mutation ($recipeExchange: RecipeExchangeUpdateParams!) {
+ updateRecipeExchange(recipeExchange: $recipeExchange) {
+ recipeExchange {...recipeExchange}
}
}
- """)
+ """, vars: %{"recipeExchange" => Map.put(params, "id", old.id)})
- assert data["id"] == rec_exch.id
- assert data["name"] == params.name
- assert data["note"] == params.note
+ assert data["id"] == old.id
+ assert data["name"] == params["name"]
+ assert data["note"] == params["note"]
end
- test "deleteRecipeExchange()", %{recipe_exchange: %{id: id}} do
+ test "deleteRecipeExchange", %{inserted: %{id: id}} do
assert %{data: %{"deleteRecipeExchange" => true}} =
- mutation!("""
- deleteRecipeExchange(id: "#{id}")
- """)
+ run!("""
+ mutation ($id: ID!) {
+ deleteRecipeExchange(id: $id)
+ }
+ """, vars: %{"id" => id})
end
end
end