Class: ActionPolicy::GraphQL::AuthorizedField::AuthorizeExtension

Inherits:
Extension
  • Object
show all
Defined in:
lib/action_policy/graphql/authorized_field.rb

Constant Summary collapse

DEPRECATION_MESSAGE =
"`authorize: *` for mutation fields is deprecated.  Please use `preauthorize: *` instead."

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Extension

#extract_option

Class Method Details

.show_authorize_mutation_deprecationObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/action_policy/graphql/authorized_field.rb', line 28

def show_authorize_mutation_deprecation
  return if defined?(@authorize_mutation_deprecation_shown)

  if defined?(ActiveSupport::Deprecation)
    ActiveSupport::Deprecation.warn(DEPRECATION_MESSAGE)
  else
    warn(DEPRECATION_MESSAGE)
  end

  @authorize_mutation_deprecation_shown = true
end

Instance Method Details

#after_resolve(value:, context:, object:, **_rest) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/action_policy/graphql/authorized_field.rb', line 48

def after_resolve(value:, context:, object:, **_rest)
  return value if value.nil?

  if @raise
    object.authorize! value, to: @to, **options
    value
  else
    object.allowed_to?(@to, value, **options) ? value : nil
  end
end

#applyObject



41
42
43
44
45
46
# File 'lib/action_policy/graphql/authorized_field.rb', line 41

def apply
  self.class.show_authorize_mutation_deprecation if field.mutation && field.mutation < ::GraphQL::Schema::Mutation

  @to = extract_option(:to) { ::ActionPolicy::GraphQL.default_authorize_rule }
  @raise = extract_option(:raise) { ::ActionPolicy::GraphQL.authorize_raise_exception }
end