Sha256: 93e45d4fabb44968722e2b445565c5f077b5a52f48b3620d66acb76fac948c3f

Contents?: true

Size: 698 Bytes

Versions: 5

Compression:

Stored size: 698 Bytes

Contents

module GraphqlGrpc
  class Resolver
    def initialize(proxy)
      @proxy = proxy
    end

    attr_reader :proxy

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql_grpc-0.1.13 lib/graphql_grpc/resolver.rb
graphql_grpc-0.1.12 lib/graphql_grpc/resolver.rb
graphql_grpc-0.1.11 lib/graphql_grpc/resolver.rb
graphql_grpc-0.1.10 lib/graphql_grpc/resolver.rb
graphql_grpc-0.1.9 lib/graphql_grpc/resolver.rb