zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

commit 1ac24e4737c38cb9aa34c21607193a3661c60dd0
parent 3af70d3cf2bbaf6cea0a66c425d8e886353dab58
Author: srfsh <dev@srf.sh>
Date:   Mon, 22 Aug 2022 19:39:41 +0300

Zenflows{Test,}.VF.Agent: add paging support and small improvements

Diffstat:
Msrc/zenflows/vf/agent/domain.ex | 17++++++++++++++---
Msrc/zenflows/vf/agent/resolv.ex | 12++++++++----
Msrc/zenflows/vf/agent/type.ex | 23+++++++++++++++++++++--
Mtest/vf/agent/domain.test.exs | 24+++++++++---------------
4 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/src/zenflows/vf/agent/domain.ex b/src/zenflows/vf/agent/domain.ex @@ -21,11 +21,22 @@ defmodule Zenflows.VF.Agent.Domain do alias Zenflows.DB.Repo alias Zenflows.VF.Agent +@typep repo() :: Ecto.Repo.t() @typep id() :: Zenflows.DB.Schema.id() -@spec by_id(id) :: Agent.t() | nil -def by_id(id) do - Repo.get(Agent, id) +@spec one(repo(), id() | map() | Keyword.t()) :: {:ok, Agent.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(Agent, clauses) do + nil -> {:error, "not found"} + found -> {:ok, found} + end +end + +@spec all(Paging.params()) :: Paging.result(Agent.t()) +def all(params) do + Paging.page(Agent, params) end @spec preload(Agent.t(), :primary_location) :: Agent.t() diff --git a/src/zenflows/vf/agent/resolv.ex b/src/zenflows/vf/agent/resolv.ex @@ -20,15 +20,19 @@ defmodule Zenflows.VF.Agent.Resolv do alias Zenflows.VF.{Agent, Agent.Domain} -def my_agent(_args, %{context: %{req_user: user}}) do +def my_agent(_, %{context: %{req_user: user}}) do {:ok, user} end -def agent(%{id: id}, _info) do - {:ok, Domain.by_id(id)} +def agent(params, _) do + Domain.one(params) end -def primary_location(%Agent{} = agent, _args, _info) do +def agents(params, _) do + Domain.all(params) +end + +def primary_location(%Agent{} = agent, _, _) do agent = Domain.preload(agent, :primary_location) {:ok, agent.primary_location} end diff --git a/src/zenflows/vf/agent/type.ex b/src/zenflows/vf/agent/type.ex @@ -61,6 +61,16 @@ interface :agent do end end +object :agent_edge do + field :cursor, non_null(:id) + field :node, non_null(:agent) +end + +object :agent_connection do + field :page_info, non_null(:page_info) + field :edges, non_null(list_of(non_null(:agent_edge))) +end + object :query_agent do @desc "Loads details of the currently authenticated agent." field :my_agent, :agent do @@ -73,8 +83,17 @@ object :query_agent do resolve &Resolv.agent/2 end - #"Loads all agents publicly registered within this collaboration space." - #agents(start: ID, limit: Int): [Agent!] + @desc """ + Loads all agents publicly registered within this collaboration + space. + """ + field :agents, :agent_connection do + arg :first, :integer + arg :after, :id + arg :last, :integer + arg :before, :id + resolve &Resolv.agents/2 + end end # object :mutation_agent diff --git a/test/vf/agent/domain.test.exs b/test/vf/agent/domain.test.exs @@ -27,9 +27,9 @@ setup do } end -describe "by_id/1" do - test "returns a Person", %{per: per} do - agent = Domain.by_id(per.id) +describe "one/1" do + test "with per's id: finds the Person", %{per: per} do + assert {:ok, agent} = Domain.one(per.id) # common assert agent.id == per.id @@ -47,8 +47,8 @@ describe "by_id/1" do assert agent.classified_as == nil end - test "returns an Organization", %{org: org} do - agent = Domain.by_id(org.id) + test "with org's id: finds the Organization", %{org: org} do + assert {:ok, agent} = Domain.one(org.id) # common assert agent.id == org.id @@ -69,20 +69,14 @@ end describe "preload/2" do test "preloads :primary_location for a Person", %{per: per} do - agent = - per.id - |> Domain.by_id() - |> Domain.preload(:primary_location) - + {:ok, agent} = Domain.one(per.id) + agent = Domain.preload(agent, :primary_location) assert agent.primary_location.id == agent.primary_location_id end test "preloads :primary_location for an Organization", %{org: org} do - agent = - org.id - |> Domain.by_id() - |> Domain.preload(:primary_location) - + {:ok, agent} = Domain.one(org.id) + agent = Domain.preload(agent, :primary_location) assert agent.primary_location.id == agent.primary_location_id end end