Current section

Files

Jump to
caravela priv templates graphql_queries.eex
Raw

priv/templates/graphql_queries.eex

defmodule <%= inspect @queries_module %> do
@moduledoc """
Generated by Caravela from <%= inspect @domain_module %>.
Absinthe query object. Each entity exposes:
* `<entity>` — fetch a single record by id
* `<entities>` — list all records visible to the caller
Context (authorization, tenant) is extracted from the Absinthe
resolution via `extract_context/1`.
Regenerate with `mix caravela.gen.graphql <%= inspect @domain_module %>`.
Custom code placed below the `# --- CUSTOM ---` marker is preserved.
"""
use Absinthe.Schema.Notation
alias <%= inspect @context_module %>
object :<%= @queries_field %> do
<%= for e <- @entities do %> @desc "List <%= e.plural %>"
field <%= inspect e.list_field %>, list_of(<%= inspect e.gql_name %>) do
resolve fn _parent, _args, resolution ->
{:ok, <%= @context_short %>.<%= e.list_fn %>(extract_context(resolution))}
end
end
@desc "Fetch a single <%= e.singular %> by id"
field <%= inspect e.get_field %>, <%= inspect e.gql_name %> do
arg :id, non_null(:id)
resolve fn _parent, %{id: id}, resolution ->
{:ok, <%= @context_short %>.<%= e.get_fn %>(id, extract_context(resolution))}
end
end
<% end %> end
# --- Internal --------------------------------------------------------
defp extract_context(%{context: context}) when is_map(context), do: context
defp extract_context(_), do: %{}
<%= @custom_marker %>