ash_graphql/lib/graphql/resolver.ex

52 lines
1.3 KiB
Elixir
Raw Normal View History

2020-05-02 10:35:12 +12:00
defmodule AshGraphql.Graphql.Resolver do
def resolve(
%{arguments: %{id: id}, context: context} = resolution,
{api, resource, :get, action}
) do
2020-06-06 06:41:00 +12:00
opts =
if api.graphql_authorize?() do
[actor: Map.get(context, :actor), action: action]
else
[action: action]
end
result = api.get(resource, id, opts)
2020-05-02 10:35:12 +12:00
Absinthe.Resolution.put_result(resolution, to_resolution(result))
end
def resolve(
%{arguments: %{limit: limit, offset: offset}, context: context} = resolution,
{api, resource, :read, action}
) do
2020-06-06 06:41:00 +12:00
opts =
if api.graphql_authorize?() do
[actor: Map.get(context, :actor), action: action]
else
[action: action]
end
2020-05-02 10:35:12 +12:00
result =
resource
|> api.query
|> Ash.Query.limit(limit)
|> Ash.Query.offset(offset)
2020-06-06 06:41:00 +12:00
|> api.read(opts)
|> case do
{:ok, results} ->
{:ok, %AshGraphql.Paginator{results: results, limit: limit, offset: offset}}
error ->
error
2020-05-02 10:35:12 +12:00
end
Absinthe.Resolution.put_result(resolution, to_resolution(result))
end
def resolve(resolution, _),
do: Absinthe.Resolution.put_result(resolution, {:error, :unknown_request})
defp to_resolution({:ok, value}), do: {:ok, value}
defp to_resolution({:error, error}), do: {:error, List.wrap(error)}
end