Class: ActionPolicy::GraphQL::AuthorizedField::PreauthorizeExtension

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

Instance Method Summary collapse

Methods inherited from Extension

#extract_option

Instance Method Details

#applyObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/action_policy/graphql/authorized_field.rb', line 61

def apply
  if options[:with].nil?
    raise ArgumentError, "You must specify the policy for preauthorization: " \
                         "`field :#{field.name}, preauthorize: {with: SomePolicy}`"
  end

  @to = extract_option(:to) do
    if field.type.list?
      ::ActionPolicy::GraphQL.default_preauthorize_list_rule
    else
      ::ActionPolicy::GraphQL.default_preauthorize_node_rule
    end
  end

  @raise = extract_option(:raise) do
    if field.mutation
      ::ActionPolicy::GraphQL.preauthorize_mutation_raise_exception
    else
      ::ActionPolicy::GraphQL.preauthorize_raise_exception
    end
  end
end

#resolve(context:, object:, arguments:, **_rest) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/action_policy/graphql/authorized_field.rb', line 84

def resolve(context:, object:, arguments:, **_rest)
  if @raise
    object.authorize! field.name, to: @to, **options
    yield object, arguments
  elsif object.allowed_to?(@to, field.name, **options)
    yield object, arguments
  end
end