Module: GraphQR::Base

Includes:
ApplyScopes
Defined in:
lib/graphqr/base.rb

Overview

The Base module defines some helper methods that can be used once it is included it also includes the basic ApplyScopes library

Constant Summary collapse

DEFAULT_AUTHORIZATION_ERROR =
'You are not authorized to perform this action'

Instance Method Summary collapse

Methods included from ApplyScopes

#apply_scopes

Instance Method Details

#authorize_graphql(record, action, policy_class: nil) ⇒ Object

This method is a wrapper around the Pundit authorize, receiving the same arguments. The only difference is that it turns the Pundit::NotAuthorizedError into a GraphQL::ExecutionError

### Example:

“‘ authorize_graphql User, :index? “`

Raises:

  • (GraphQL::ExecutionError)


21
22
23
24
# File 'lib/graphqr/base.rb', line 21

def authorize_graphql(record, action, policy_class: nil)
  args = { record: record, action: action, policy_class: policy_class }
  raise GraphQL::ExecutionError, DEFAULT_AUTHORIZATION_ERROR unless policy_provider.allowed?(args)
end

#policy_providerObject

This is a helper method to get the policy provider from the context



28
29
30
# File 'lib/graphqr/base.rb', line 28

def policy_provider
  context[:policy_provider]
end