Class: GraphQL::Api::Resolvers::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/api/resolvers/field.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, name) ⇒ Field

Returns a new instance of Field.



5
6
7
8
9
# File 'lib/graphql/api/resolvers/field.rb', line 5

def initialize(model, name)
  @model = model
  @name = name
  @policy_class = "#{model.name}Policy".safe_constantize
end

Instance Method Details

#call(obj, args, ctx) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql/api/resolvers/field.rb', line 11

def call(obj, args, ctx)
  if @policy_class
    policy = @policy_class.new(ctx, obj)
    return policy.unauthorized! unless policy.read?

    if policy.respond_to?("access_#{@name}?")
      return policy.unauthorized_field_access(@name) unless policy.send("access_#{@name}?")
    end
    
    obj.send(@name)
  elsif obj.respond_to?("access_#{@name}?")
    obj.send(@name) if obj.send("access_#{@name}?", ctx)
  else
    obj.send(@name)
  end
end