-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add authorization and response handlers (#5)
- Loading branch information
1 parent
d3790b2
commit 999fa11
Showing
4 changed files
with
75 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Phantom::Graphql::Authorization | ||
class AuthorizeExtension < GraphQL::Schema::FieldExtension | ||
def resolve(context:, object:, arguments:, **_rest) | ||
name, action = options.split("#") | ||
|
||
authorized = name.constantize.new(context[:current_session]).send(action) | ||
|
||
raise GraphQL::ExecutionError, "Unauthorized" unless authorized | ||
|
||
yield object, arguments | ||
end | ||
end | ||
|
||
def initialize(*args, authorize: nil, **kwargs, &block) | ||
extensions = (kwargs[:extensions] ||= []) | ||
|
||
extensions << { Phantom::Graphql::Authorization::AuthorizeExtension => authorize } if authorize.present? | ||
|
||
super(*args, **kwargs, &block) | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Phantom::Graphql::Response | ||
module ExceptionsHandler | ||
def self.included(base) | ||
base.rescue_from(ActiveRecord::RecordNotFound) { |e| raise GraphQL::ExecutionError, "#{e.model} not found" } | ||
end | ||
end | ||
|
||
module Helpers | ||
def raise_error(error) | ||
raise GraphQL::ExecutionError, error | ||
end | ||
end | ||
end |