Class: KittyPolicy::GraphQL::CanResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/kitty_policy/graphql/can_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(policy:, base_resolver: ::GraphQL::Schema::Resolver, current_user_key: :current_user) ⇒ CanResolver

Returns a new instance of CanResolver.



6
7
8
9
10
# File 'lib/kitty_policy/graphql/can_resolver.rb', line 6

def initialize(policy:, base_resolver: ::GraphQL::Schema::Resolver, current_user_key: :current_user)
  @base_resolver = base_resolver
  @current_user_key = current_user_key
  @policy = policy
end

Instance Method Details

#field_optionsObject



29
30
31
# File 'lib/kitty_policy/graphql/can_resolver.rb', line 29

def field_options
  raise "Can't use `#{self.class.name}` directly as resolver. Use `resolve: #{self.class.name}.perform(action)`"
end

#perform(action, &extract_subject) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kitty_policy/graphql/can_resolver.rb', line 12

def perform(action, &extract_subject)
  policy = @policy
  current_user_key = @current_user_key

  Class.new(@base_resolver) do
    type ::GraphQL::Types::Boolean, null: false

    define_method(:resolve) do
      policy.can?(
        context[current_user_key],
        action,
        extract_subject ? extract_subject.call(object) : object,
      )
    end
  end
end