Class: GraphqlGrpc::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_grpc/resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ Resolver

Returns a new instance of Resolver.



3
4
5
# File 'lib/graphql_grpc/resolver.rb', line 3

def initialize(proxy)
  @proxy = proxy
end

Instance Attribute Details

#proxyObject (readonly)

Returns the value of attribute proxy.



7
8
9
# File 'lib/graphql_grpc/resolver.rb', line 7

def proxy
  @proxy
end

Instance Method Details

#call(_type, field, obj, args, ctx) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphql_grpc/resolver.rb', line 9

def call(_type, field, obj, args, ctx)
  if obj
    field_sym = field.name.to_sym
    # Try to access the field as a method, then with Hash notation using
    # a Symbol and finally as a String.

    value = if obj.is_a?(Hash)
      # Prefer Hash value over method in case Hash keys conflict
      # with method names on Hash.
      obj[field_sym] || obj.try(field_sym)
    else
      obj[field_sym.to_s]
    end
    return value.is_a?(Symbol) ? value.to_s : value
  end
  proxy.invoke(field, args, ctx)
end