Commit aee14626 authored by Antonis Kalou's avatar Antonis Kalou

Add support for track/trace batching and add graphql resolvers

parent ecdc2672
......@@ -36,7 +36,6 @@ defmodule ValueFlows.Hydration do
inventoried_economic_resources: [
resolve: &ValueFlows.Observation.EconomicResource.GraphQL.agent_resources/3
],
}
%{
......@@ -138,16 +137,29 @@ defmodule ValueFlows.Hydration do
in_scope_of: [
resolve: &CommonResolver.context_edge/3
],
input_of: [
resolve: &ValueFlows.Observation.EconomicEvent.GraphQL.fetch_input_of_edge/3
],
output_of: [
resolve: &ValueFlows.Observation.EconomicEvent.GraphQL.fetch_output_of_edge/3
],
resource_inventoried_as: [
resolve:
&ValueFlows.Observation.EconomicEvent.GraphQL.fetch_resource_inventoried_as_edge/3
],
to_resource_inventoried_as: [
resolve:
&ValueFlows.Observation.EconomicEvent.GraphQL.fetch_to_resource_inventoried_as_edge/3
],
resource_classified_as: [
resolve: &ValueFlows.Util.GraphQL.fetch_classifications_edge/3
],
at_location: [
resolve: &ValueFlows.Util.GraphQL.at_location_edge/3
],
triggered_by: [
resolve: &ValueFlows.Observation.EconomicEvent.GraphQL.fetch_triggered_by_edge/3
],
tags: [
resolve: &CommonsPub.Tag.GraphQL.TagResolver.tags_edges/3
],
......@@ -180,6 +192,18 @@ defmodule ValueFlows.Hydration do
onhand_quantity: [
resolve: &ValueFlows.Util.GraphQL.onhand_quantity_edge/3
],
primary_accountable: [
resolve: &ValueFlows.Observation.EconomicResource.GraphQL.fetch_primary_accountable_edge/3
],
unit_of_effort: [
resolve: &ValueFlows.Observation.EconomicResource.GraphQL.fetch_unit_of_effort_edge/3
],
contained_in: [
resolve: &ValueFlows.Observation.EconomicResource.GraphQL.fetch_contained_in_edge/3
],
conforms_to: [
resolve: &ValueFlows.Observation.EconomicResource.GraphQL.fetch_conforms_to_edge/3
],
tags: [
resolve: &CommonsPub.Tag.GraphQL.TagResolver.tags_edges/3
],
......
......@@ -229,12 +229,22 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
events_filter_next([param_remove], filter_add, page_opts, filters_acc)
end
def track(event, _, _) do
EconomicEvents.track(event)
def track(event, _, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_track,
context: event,
info: info
})
end
def trace(event, _, _) do
EconomicEvents.trace(event)
def trace(event, _, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_trace,
context: event,
info: info
})
end
## fetchers
......@@ -306,9 +316,8 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
})
end
def fetch_resource_inventoried_as_edge(%{resource_inventoried_as: {:error, error}}, _, _) do
{:error, error}
end
def fetch_resource_inventoried_as_edge(%{resource_inventoried_as: {:error, _} = error}, _, _),
do: error
def fetch_resource_inventoried_as_edge(%{resource_inventoried_as_id: id} = thing, _, _)
when not is_nil(id) do
......@@ -320,7 +329,56 @@ defmodule ValueFlows.Observation.EconomicEvent.GraphQL do
{:ok, nil}
end
# FIXME: duplication!
def fetch_to_resource_inventoried_as_edge(%{to_resource_inventoried_as: {:error, _} = error}, _, _),
do: error
def fetch_to_resource_inventoried_as_edge(%{to_resource_inventoried_as_id: id} = thing, _, _)
when not is_nil(id) do
thing = Repo.preload(thing, :to_resource_inventoried_as)
{:ok, Map.get(thing, :to_resource_inventoried_as)}
end
def fetch_to_resource_inventoried_as_edge(_, _, _) do
{:ok, nil}
end
def fetch_output_of_edge(%{output_of_id: id} = thing, _, _) when is_binary(id) do
thing = Repo.preload(thing, :output_of)
{:ok, Map.get(thing, :output_of)}
end
def fetch_output_of_edge(_, _, _) do
{:ok, nil}
end
def fetch_input_of_edge(%{input_of_id: id} = thing, _, _) when is_binary(id) do
thing = Repo.preload(thing, :input_of)
{:ok, Map.get(thing, :input_of)}
end
def fetch_input_of_edge(_, _, _) do
{:ok, nil}
end
def fetch_triggered_by_edge(%{triggered_by_id: id} = thing, _, _) when is_binary(id) do
thing = Repo.preload(thing, :triggered_by)
{:ok, Map.get(thing, :triggered_by)}
end
def fetch_triggered_by_edge(_, _, _) do
{:ok, nil}
end
def fetch_track(_, event) do
EconomicEvents.track(event)
end
def fetch_trace(_, event) do
EconomicEvents.trace(event)
end
# Mutations
def create_event(%{event: event_attrs} = params, info) do
Repo.transact_with(fn ->
with {:ok, user} <- GraphQL.current_user_or_not_logged_in(info),
......
......@@ -76,7 +76,6 @@ defmodule ValueFlows.Observation.EconomicEvent.Queries do
def filter(q, :default) do
filter(q, [:deleted])
# filter q, [:deleted, {:preload, :provider}, {:preload, :receiver}]
end
def filter(q, :offer) do
......@@ -237,7 +236,6 @@ defmodule ValueFlows.Observation.EconomicEvent.Queries do
where(q, [event: c], c.resource_inventoried_as_id == ^id)
end
def filter(q, {:to_resource_inventoried_as_id, ids}) when is_list(ids) do
where(q, [event: c], c.to_resource_inventoried_as_id in ^ids)
end
......
......@@ -72,6 +72,24 @@ defmodule ValueFlows.Observation.Process.GraphQL do
Processes.many([:default])
end
def track(process, _, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_track_process,
context: process,
info: info
})
end
def trace(process, _, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_trace_process,
context: process,
info: info
})
end
def processes_filtered(page_opts, _ \\ nil) do
# IO.inspect(processes_filtered: page_opts)
processes_filter(page_opts, [])
......@@ -124,11 +142,11 @@ defmodule ValueFlows.Observation.Process.GraphQL do
processes_filter_next([param_remove], filter_add, page_opts, filters_acc)
end
def track(process, _, _) do
def fetch_track_process(_, process) do
Processes.track(process)
end
def trace(process, _, _) do
def fetch_trace_process(_, process) do
Processes.trace(process)
end
......
......@@ -96,10 +96,6 @@ defmodule ValueFlows.Observation.Process.Processes do
EconomicEvents.many([:default, input_of_id: id])
end
def inputs(_, _) do
{:ok, nil}
end
def outputs(%{id: id}, action_id \\ nil) when not is_nil(action_id) do
EconomicEvents.many([:default, output_of_id: id, action_id: action_id])
end
......
......@@ -16,7 +16,6 @@ defmodule ValueFlows.Observation.EconomicResource.GraphQL do
alias CommonsPub.GraphQL.{
ResolveField,
# ResolveFields,
# ResolvePage,
ResolvePages,
ResolveRootPage,
......@@ -76,6 +75,24 @@ defmodule ValueFlows.Observation.EconomicResource.GraphQL do
EconomicResources.many([:default])
end
def track(%{id: id}, _, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_track_resource,
context: id,
info: info
})
end
def trace(%{id: id}, _, info) do
ResolveField.run(%ResolveField{
module: __MODULE__,
fetcher: :fetch_trace_resource,
context: id,
info: info
})
end
def resources_filtered(page_opts, _ \\ nil) do
# IO.inspect(resources_filtered: page_opts)
resources_filter(page_opts, [])
......@@ -311,12 +328,41 @@ defmodule ValueFlows.Observation.EconomicResource.GraphQL do
{:ok, nil}
end
def track(resource, _, _) do
EconomicResources.track(resource)
def fetch_unit_of_effort_edge(%{unit_of_effort_id: id} = thing, _, info)
when is_binary(id) do
thing = Repo.preload(thing, :unit_of_effort)
{:ok, Map.get(thing, :unit_of_effort)}
end
def fetch_unit_of_effort_edge(_, _, _) do
{:ok, nil}
end
def fetch_contained_in_edge(%{contained_in_id: id} = thing, _, info)
when is_binary(id) do
thing = Repo.preload(thing, :contained_in)
{:ok, Map.get(thing, :contained_in)}
end
def fetch_contained_in_edge(_, _, _) do
{:ok, nil}
end
def fetch_conforms_to_edge(%{conforms_to_id: id} = thing, _, _) when is_binary(id) do
thing = Repo.preload(thing, :conforms_to)
{:ok, Map.get(thing, :conforms_to)}
end
def fetch_conforms_to_edge(_, _, _) do
{:ok, nil}
end
def fetch_track_resource(_, id) do
EconomicResources.track(id)
end
def trace(resource, _, _) do
EconomicResources.trace(resource)
def fetch_trace_resource(_, id) do
EconomicResources.trace(id)
end
def create_resource(%{new_inventoried_resource: resource_attrs}, info) do
......
......@@ -70,6 +70,10 @@ defmodule ValueFlows.Observation.EconomicResource.Queries do
join(q, jq, [resource: c], t in assoc(c, :unit_of_effort), as: :unit_of_effort)
end
def join_to(q, :contained_in, jq) do
join(q, jq, [resource: c], t in assoc(c, :contained_in), as: :contained_in)
end
# def join_to(q, :primary_accountable, jq) do
# join q, jq, [follow: f], c in assoc(f, :primary_accountable), as: :pointer
# end
......@@ -96,7 +100,12 @@ defmodule ValueFlows.Observation.EconomicResource.Queries do
def filter(q, :default) do
#filter(q, [:deleted])
filter q, [:deleted, {:preload, :primary_accountable}, {:preload, :unit_of_effort}]
filter q, [
:deleted,
# FIXME: use hydration
# preload: :primary_accountable,
# preload: :unit_of_effort,
]
end
def filter(q, :offer) do
......@@ -305,6 +314,12 @@ defmodule ValueFlows.Observation.EconomicResource.Queries do
# preload(q, [geolocation: g], current_location: g)
end
def filter(q, {:preload, :contained_in}) do
q
|> join_to(:contained_in)
|> preload([contained_in: cin], contained_in: cin)
end
# pagination
def filter(q, {:limit, limit}) do
......
......@@ -99,10 +99,6 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
EconomicEvents.many([:default, track_resource: id])
end
def track(_) do
{:ok, nil}
end
def trace(%{id: id}) do
trace(id)
end
......@@ -111,10 +107,6 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
EconomicEvents.many([:default, trace_resource: id])
end
def trace(_) do
{:ok, nil}
end
def preload_all(resource) do
Repo.preload(resource, [
:accounting_quantity,
......
......@@ -198,7 +198,6 @@ defmodule ValueFlows.Observation.EconomicEvent.EventsGraphQLTest do
conn = user_conn(user)
assert event = grumble_post_key(q, conn, :economic_event, %{id: event.id})
IO.inspect(event)
assert Enum.count(event["track"]) == 3
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment