module GraphQL::Pundit::Scope

Scope methods to be included in the used Field class

Public Class Methods

new(*args, policy: nil, record: nil, before_scope: nil, after_scope: nil, **kwargs, &block) click to toggle source

rubocop:disable Metrics/ParameterLists

Calls superclass method
# File lib/graphql-pundit/scope.rb, line 14
def initialize(*args, policy: nil,
               record: nil,
               before_scope: nil,
               after_scope: nil,
               **kwargs, &block)
  @before_scope = before_scope
  @after_scope = after_scope
  @policy = policy
  @record = record
  super(*args, **kwargs, &block)
end
prepended(base) click to toggle source
# File lib/graphql-pundit/scope.rb, line 9
def self.prepended(base)
  base.include(GraphQL::Pundit::Common)
end

Public Instance Methods

after_scope(scope = true) click to toggle source
# File lib/graphql-pundit/scope.rb, line 32
def after_scope(scope = true)
  @after_scope = scope
end
before_scope(scope = true) click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/graphql-pundit/scope.rb, line 28
def before_scope(scope = true)
  @before_scope = scope
end
resolve(obj, args, ctx) click to toggle source
Calls superclass method
# File lib/graphql-pundit/scope.rb, line 36
def resolve(obj, args, ctx)
  before_scope_return = apply_scope(@before_scope, obj, args, ctx)
  field_return = super(before_scope_return, args, ctx)
  apply_scope(@after_scope, field_return, args, ctx)
end
Also aliased as: resolve_field
resolve_field(obj, args, ctx)
Alias for: resolve

Private Instance Methods

apply_scope(scope, root, arguments, context) click to toggle source
# File lib/graphql-pundit/scope.rb, line 46
def apply_scope(scope, root, arguments, context)
  return root unless scope

  record = @record || root
  return scope.call(record, arguments, context) if scope.respond_to?(:call)

  scope = infer_scope(record) if scope.equal?(true)
  scope::Scope.new(context[self.class.current_user], record).resolve
end
infer_scope(root) click to toggle source
# File lib/graphql-pundit/scope.rb, line 56
def infer_scope(root)
  infer_from = model?(root) ? root.model : root
  ::Pundit::PolicyFinder.new(infer_from).policy!
end