Current section

Files

Jump to
graphqexl lib graphqexl schema.ex
Raw

lib/graphqexl/schema.ex

alias Graphqexl.Schema.{
Dsl,
Enum,
Interface,
Mutation,
Query,
Subscription,
Type,
Union,
}
defmodule Graphqexl.Schema do
defstruct(
enums: [],
interfaces: [],
mutations: [],
queries: [],
str: "",
subscriptions: [],
types: [],
unions: []
)
@type gql :: String.t()
@type t :: %Graphqexl.Schema{
enums: list(Enum.t),
interfaces: list(Interface.t),
mutations: list(Mutation.t),
queries: list(Queries.t),
str: gql,
subscriptions: list(Subscription.t),
types: list(Type.t),
unions: list(Union.t),
}
def gql(str) do
%Graphqexl.Schema{str: str |> Dsl.preprocess}
end
@spec register(
GraphqexlSchema.t,
Enum.t |
Interface.t |
Mutation.t |
Query.t |
Subscription.t |
Type.t |
Union.t
) :: Graphqexl.Schema.t
def register(schema, %Enum{} = enum) do
schema |> register(:enums, enum)
end
def register(schema, %Interface{} = interface) do
schema |> register(:interfacess, interface)
end
def register(schema, %Mutation{} = mutation) do
schema |> register(:mutations, mutation)
end
def register(schema, %Query{} = query) do
schema |> register(:queries, query)
end
def register(schema, %Subscription{} = subscription) do
schema |> register(:subscriptions, subscription)
end
def register(schema, %Type{} = type) do
schema |> register(:types, type)
end
def register(schema, %Union{} = union) do
schema |> register(:unions, union)
end
defp register(schema, key, value) do
schema |> Map.update(key, value, &prepend_list/2)
end
defp prepend_list(list, value) do
list |> List.insert_at(0, value)
end
end