Class: GraphQL::Guard

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/guard.rb,
lib/graphql/guard/version.rb

Constant Summary collapse

NotAuthorizedError =
Class.new(StandardError)
ANY_FIELD_NAME =
:'*'
DEFAULT_NOT_AUTHORIZED =
->(type, field) do
  raise NotAuthorizedError.new("Not authorized to access: #{type}.#{field}")
end
MASKING_FILTER =
->(schema_member, ctx) do
  mask = schema_member.graphql_definition.[:mask]
  mask ? mask.call(ctx) : true
end
VERSION =
"2.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy_object: nil, not_authorized: DEFAULT_NOT_AUTHORIZED) ⇒ Guard

Returns a new instance of Guard.



23
24
25
26
# File 'lib/graphql/guard.rb', line 23

def initialize(policy_object: nil, not_authorized: DEFAULT_NOT_AUTHORIZED)
  @policy_object = policy_object
  @not_authorized = not_authorized
end

Instance Attribute Details

#not_authorizedObject (readonly)

Returns the value of attribute not_authorized.



21
22
23
# File 'lib/graphql/guard.rb', line 21

def not_authorized
  @not_authorized
end

#policy_objectObject (readonly)

Returns the value of attribute policy_object.



21
22
23
# File 'lib/graphql/guard.rb', line 21

def policy_object
  @policy_object
end

Instance Method Details

#find_guard_proc(type, field) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/graphql/guard.rb', line 46

def find_guard_proc(type, field)
  return unless type.respond_to?(:type_class)

  inline_guard(field) ||
    policy_object_guard(type.type_class, field.name.to_sym) ||
    inline_guard(type) ||
    policy_object_guard(type.type_class, ANY_FIELD_NAME)
end

#trace(event, trace_data) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/graphql/guard.rb', line 38

def trace(event, trace_data)
  if event == 'execute_field'
    ensure_guarded(trace_data) { yield }
  else
    yield
  end
end

#use(schema_definition) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/graphql/guard.rb', line 28

def use(schema_definition)
  if schema_definition.interpreter?
    schema_definition.tracer(self)
  else
    raise "Please use the graphql gem version >= 1.10 with GraphQL::Execution::Interpreter"
  end

  add_schema_masking!(schema_definition)
end