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 =
|
2020-05-30 20:57:39 +12:00
|
|
|
resource
|
|
|
|
|> api.query
|
|
|
|
|> Ash.Query.limit(limit)
|
|
|
|
|> Ash.Query.offset(offset)
|
2020-06-06 06:41:00 +12:00
|
|
|
|> api.read(opts)
|
2020-05-30 20:57:39 +12:00
|
|
|
|> 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
|