Packages
potionx
0.5.2
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.1.1
0.1.0
Potionx is a set of generators and functions that speeds up the process of setting up and deploying a full-stack application that uses Elixir with GraphQL for the server-side component and Vue for the frontend component.
Current section
Files
Jump to
Current section
Files
priv/templates/potion.gen.gql_for_model/service.ex
defmodule <%= module_name_data %>.<%= context_name %>.<%= model_name %>Service do
alias <%= potion_name %>.Context.Service
alias <%= module_name_data %>.<%= context_name %>.<%= model_name %>
alias <%= module_name_data %>.Repo
import Ecto.Query
def count(%Service{} = ctx) do
from(item in query(ctx))
|> select([i], count(i.id))
|> exclude(:order_by)
|> Repo.one!
end
def delete(%Service{} = ctx) do
query(ctx)
|> Repo.one
|> case do
nil -> {:error, "not_found"}
entry ->
entry
|> Repo.delete
end
end
def mutation(%Service{filters: %{id: id}} = ctx) when not is_nil(id) do
query(ctx)
|> Repo.one
|> case do
nil -> {:error, "not_found"}
entry ->
<%= model_name %>.changeset(entry, ctx.changes)
|> Repo.update
end
end
def mutation(%Service{} = ctx) do
%<%= model_name %>{}
|> <%= model_name %>.changeset(ctx.changes)
|> Repo.insert
end
def one(%Service{} = ctx) do
query(ctx)
|> Repo.one
end
def query(%Service{} = ctx) do
<%= model_name %>
|> search(ctx)
|> where(
^(
ctx.filters
|> Map.to_list
)
)
|> order_by([desc: :id])
end
def query(q, _args), do: q
@doc """
A search function that searches all string columns by default.
"""
def search(query, %Service{search: nil}), do: query
def search(query, %Service{search: ""}), do: query
def search(query, %Service{search: s}) do
clauses =
<%= model_name %>.__schema__(:fields)
|> Enum.reduce(nil, fn field_name, query ->
if (<%= model_name %>.__schema__(:type, field_name) === :string) do
if (query === nil) do
dynamic([p], ilike(field(p, ^field_name), ^"%#{s}%"))
else
dynamic([p], ilike(field(p, ^field_name), ^"%#{s}%") or ^query)
end
else
query
end
end)
from(query, where: ^clauses)
end
end