-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
663 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defprotocol AthenaWeb.Exception do | ||
@fallback_to_any true | ||
|
||
@spec result(exception :: Exception.t()) :: {:ok, term} | {:error, String.t()} | :unknown | ||
def result(exception) | ||
end | ||
|
||
defimpl AthenaWeb.Exception, for: Any do | ||
@spec result(exception :: Exception.t()) :: {:ok, term} | {:error, String.t()} | :unknown | ||
def result(%{result: result} = _exception), do: result | ||
def result(_exception), do: :unknown | ||
end | ||
|
||
defimpl AthenaWeb.Exception, for: Ecto.NoResultsError do | ||
@spec result(exception :: Exception.t()) :: {:ok, term} | {:error, String.t()} | :unknown | ||
def result(_exception), do: {:ok, nil} | ||
end | ||
|
||
defimpl AthenaWeb.Exception, for: Ecto.Query.CastError do | ||
@spec result(exception :: Exception.t()) :: {:ok, term} | {:error, String.t()} | :unknown | ||
def result(_exception), do: {:ok, nil} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
defmodule AthenaWeb.Middleware.Safe do | ||
@moduledoc false | ||
|
||
alias AthenaWeb.Endpoint | ||
|
||
require Logger | ||
|
||
@spec add_error_handling(spec :: Absinthe.Middleware.spec()) :: Absinthe.Middleware.spec() | ||
def add_error_handling(spec), do: &(spec |> to_fun(&1, &2) |> exec_safely(&1)) | ||
|
||
# Absinthe Node Error | ||
defp to_fun( | ||
{{Absinthe.Relay.Node, :global_id_resolver}, nil}, | ||
%{state: :resolved} = resolution, | ||
_config | ||
) do | ||
fn -> resolution end | ||
end | ||
|
||
defp to_fun({{module, function}, config}, resolution, _config) do | ||
fn -> apply(module, function, [resolution, config]) end | ||
end | ||
|
||
defp to_fun({module, config}, resolution, _config) do | ||
fn -> module.call(resolution, config) end | ||
end | ||
|
||
defp to_fun(module, resolution, config) when is_atom(module) do | ||
fn -> module.call(resolution, config) end | ||
end | ||
|
||
defp to_fun(function, resolution, config) when is_function(function, 2) do | ||
fn -> function.(resolution, config) end | ||
end | ||
|
||
defp exec_safely(function, resolution) when is_function(function, 0) do | ||
function.() | ||
catch | ||
kind, reason -> | ||
full_exception = Exception.format(kind, reason, __STACKTRACE__) | ||
result = AthenaWeb.Exception.result(reason) | ||
|
||
result = | ||
case {result, Endpoint.config(:debug_errors, false)} do | ||
{{:ok, _} = result, _} -> | ||
result | ||
|
||
{{:error, error}, true} -> | ||
{:error, | ||
""" | ||
#{inspect(error, pretty: true)} | ||
DEBUG: | ||
#{full_exception} | ||
"""} | ||
|
||
{{:error, error}, false} -> | ||
{:error, error} | ||
|
||
{:unknown, true} -> | ||
Logger.error(""" | ||
Unknown exception catched: | ||
#{full_exception} | ||
""") | ||
|
||
{:error, | ||
""" | ||
unkown error | ||
DEBUG: | ||
#{full_exception} | ||
"""} | ||
|
||
{:unknown, false} -> | ||
Logger.error(""" | ||
Unknown exception catched: | ||
#{full_exception} | ||
""") | ||
|
||
{:error, "unkown error"} | ||
end | ||
|
||
Absinthe.Resolution.put_result(resolution, result) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
defmodule AthenaWeb.Schema do | ||
@moduledoc """ | ||
Root GraphQL Schema | ||
""" | ||
|
||
use Absinthe.Schema | ||
use Absinthe.Relay.Schema, :modern | ||
|
||
alias AthenaWeb.Middleware.Safe | ||
alias AthenaWeb.Schema.Dataloader, as: RepoDataLoader | ||
alias AthenaWeb.Schema.Resolver | ||
|
||
@impl Absinthe.Schema | ||
@spec context(context :: map) :: map | ||
def context(context), | ||
do: | ||
Map.put( | ||
context, | ||
:loader, | ||
Dataloader.add_source(Dataloader.new(), RepoDataLoader, RepoDataLoader.data()) | ||
) | ||
|
||
@impl Absinthe.Schema | ||
@spec plugins :: [atom] | ||
def plugins, do: [Absinthe.Middleware.Dataloader | Absinthe.Plugin.defaults()] | ||
|
||
@impl Absinthe.Schema | ||
@spec middleware( | ||
[Absinthe.Middleware.spec(), ...], | ||
Absinthe.Type.Field.t(), | ||
Absinthe.Type.Object.t() | ||
) :: [Absinthe.Middleware.spec(), ...] | ||
def middleware(middleware, _field, _object), | ||
do: Enum.map(middleware, &Safe.add_error_handling/1) | ||
|
||
import_types Absinthe.Plug.Types | ||
# import_types AbsintheErrorPayload.ValidationMessageTypes | ||
import_types AthenaWeb.Schema.Event | ||
import_types AthenaWeb.Schema.Scalar.Date | ||
import_types AthenaWeb.Schema.Scalar.Datetime | ||
import_types AthenaWeb.Schema.Scalar.Map | ||
import_types AthenaWeb.Schema.Scalar.Ok | ||
import_types AthenaWeb.Schema.Scalar.URI | ||
|
||
node interface do | ||
end | ||
|
||
interface :resource do | ||
field :id, non_null(:id) | ||
field :inserted_at, non_null(:datetime) | ||
field :updated_at, non_null(:datetime) | ||
|
||
interface :node | ||
end | ||
|
||
query do | ||
node field do | ||
resolve(&Resolver.node/2) | ||
end | ||
|
||
import_fields :event_queries | ||
end | ||
|
||
# mutation do | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule AthenaWeb.Schema.Dataloader do | ||
@moduledoc """ | ||
Absinthe Dataloader | ||
""" | ||
|
||
alias Athena.Repo | ||
|
||
@spec data :: Dataloader.Ecto.t() | ||
def data, do: Dataloader.Ecto.new(Repo) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule AthenaWeb.Schema.Event do | ||
@moduledoc false | ||
|
||
use AthenaWeb, :subschema | ||
|
||
alias Athena.Inventory.Event | ||
|
||
node object(:event) do | ||
field :name, non_null(:string) | ||
|
||
# connection field :locations, node_type: :location | ||
# connection field :item_groups, node_type: :item_group | ||
# connection field :items, node_type: :item | ||
|
||
field :inserted_at, non_null(:datetime) | ||
field :updated_at, non_null(:datetime) | ||
|
||
is_type_of(&match?(%Event{}, &1)) | ||
|
||
interface :resource | ||
end | ||
|
||
connection(node_type: :event, non_null: true) | ||
|
||
object :event_queries do | ||
@desc "Get Event By ID" | ||
field :event, :event do | ||
arg :id, non_null(:id) | ||
|
||
resolve(&Resolver.event/3) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule AthenaWeb.Schema.Event.Resolver do | ||
@moduledoc false | ||
|
||
alias Athena.Inventory | ||
|
||
@spec event(parent :: term(), args :: map(), resolution :: Absinthe.Resolution.t()) :: | ||
{:ok, term()} | {:error, term()} | ||
def event(_parent, %{id: id}, _resolution), do: {:ok, Inventory.get_event!(id)} | ||
end |
Oops, something went wrong.