Sha256: 8fe017a96e4fe24eafb79e334035deb4e8c9a1a4c52659d23223e50cc140a0d0

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'graphql-pundit/common'

module GraphQL
  module Pundit
    # Scope methods to be included in the used Field class
    module Scope
      def self.prepended(base)
        base.include(GraphQL::Pundit::Common)
      end

      def initialize(*args, before_scope: nil,
                            after_scope: nil,
                            **kwargs, &block)
        @before_scope = before_scope
        @after_scope = after_scope
        super(*args, **kwargs, &block)
      end

      def before_scope(scope = true)
        @before_scope = scope
      end

      def after_scope(scope = true)
        @after_scope = scope
      end

      def resolve_field(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

      private

      def apply_scope(scope, root, arguments, context)
        return root unless scope
        return scope.call(root, arguments, context) if scope.respond_to?(:call)

        scope = infer_scope(root) if scope.equal?(true)
        scope::Scope.new(context[self.class.current_user], root).resolve
      end

      def infer_scope(root)
        infer_from = model?(root) ? root.model : root
        ::Pundit::PolicyFinder.new(infer_from).policy!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
graphql-pundit-387-0.7.1 lib/graphql-pundit/scope.rb
graphql-pundit-0.7.1 lib/graphql-pundit/scope.rb