ash_graphql/lib/ash_graphql.ex

80 lines
2.2 KiB
Elixir
Raw Normal View History

2020-05-02 04:32:56 +12:00
defmodule AshGraphql do
@moduledoc """
2020-08-14 10:55:34 +12:00
AshGraphql is a graphql front extension for the Ash framework.
See the getting started guide for information on setting it up, and
see the `AshGraphql.Resource` documentation for docs on its DSL
2020-05-02 04:32:56 +12:00
"""
2020-08-14 09:39:59 +12:00
defmacro __using__(opts) do
quote bind_quoted: [api: opts[:api]] do
defmodule AshTypes do
2020-08-15 02:20:47 +12:00
@moduledoc false
alias Absinthe.{Blueprint, Phase, Pipeline}
2020-08-14 09:39:59 +12:00
def pipeline(pipeline) do
Pipeline.insert_before(
pipeline,
Phase.Schema.Validation.QueryTypeMustBeObject,
__MODULE__
)
end
def run(blueprint, _opts) do
api = unquote(api)
Code.ensure_compiled(api)
blueprint_with_queries =
api
|> AshGraphql.Api.queries(__MODULE__)
|> Enum.reduce(blueprint, fn query, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootQueryType", query)
end)
blueprint_with_mutations =
api
|> AshGraphql.Api.mutations(__MODULE__)
|> Enum.reduce(blueprint_with_queries, fn mutation, blueprint ->
Absinthe.Blueprint.add_field(blueprint, "RootMutationType", mutation)
end)
new_defs =
List.update_at(blueprint_with_mutations.schema_definitions, 0, fn schema_def ->
%{
schema_def
| type_definitions:
schema_def.type_definitions ++
AshGraphql.Api.type_definitions(api, __MODULE__)
}
end)
{:ok, %{blueprint_with_mutations | schema_definitions: new_defs}}
end
end
2020-05-02 04:32:56 +12:00
2020-08-14 09:39:59 +12:00
@pipeline_modifier AshTypes
end
2020-05-02 04:32:56 +12:00
end
2020-08-15 02:20:47 +12:00
defguard is_digit(x) when x in ?0..?0
def roll(schema) do
Enum.map(schema, fn
<<?d, x, y>> when is_digit(x) and is_digit(y) ->
Enum.random(1..String.to_integer(<<x, y>>))
<<?d, x, y, z>> when is_digit(x) and is_digit(y) and is_digit(z) ->
Enum.random(1..String.to_integer(<<x, y, z>>))
"adv" ->
{:max, roll(["d20", "d20"])}
"dis" ->
{:min, roll(["d20", "d20"])}
x ->
x
end)
end
2020-05-02 04:32:56 +12:00
end