Created
July 18, 2017 01:32
-
-
Save BruOp/a8a1311ac7542dcca71ea23fed45088b to your computer and use it in GitHub Desktop.
Absinthe Auth
This file contains hidden or 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
defmodule Api.Graphql.AuthMiddleware do | |
@behaviour Absinthe.Middleware | |
def call(resolution, _config) do | |
case resolution.context[:authenticated] do | |
true -> resolution | |
_ -> Absinthe.Resolution.put_result(resolution, {:error, "Unauthorized"}); | |
end | |
end | |
end |
This file contains hidden or 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
defmodule Api.Graphql.Schema do | |
use Absinthe.Schema | |
use Absinthe.Relay.Schema | |
alias Api.Graphql.{Types, AuthMiddleware} | |
import_types Types.ViewerType | |
# Relay | |
node interface do | |
resolve_type fn | |
%{}, _ -> :viewer | |
_, _ -> nil | |
end | |
end | |
def middleware(middleware, _field, %Absinthe.Type.Object{identifier: :user}) do | |
[AuthMiddleware | middleware] | |
end | |
def middleware(middleware, _field, _) do | |
middleware | |
end | |
end |
This file contains hidden or 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
defmodule Api.Graphql.Types.User do | |
use Api.Graphql.Types | |
node object :user do | |
field :id, :id | |
field :name, :string | |
end | |
connection node_type: :user | |
end |
This file contains hidden or 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
defmodule Api.Graphql.Types.ViewerType do | |
use Api.Graphql.Types | |
alias Api.Graphql.Types | |
import_types Types.User | |
node object :viewer do | |
field :id, :id | |
# User | |
field :user, type: :user do | |
arg :id, non_null(:id) | |
resolve parsing_node_ids(&Resolver.User.find/2, id: :user) | |
end | |
connection field :users, node_type: :user do | |
resolve &Resolver.User.all/2 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment