class GraphqlGrpc::Function
Storage for an actual function definition. Implements a `call` method so that it may be invoked with a simple hash of params.
Attributes
name[R]
rpc_desc[R]
service_name[R]
Public Class Methods
new(service_name, service_stub, rpc_desc)
click to toggle source
# File lib/graphql_grpc/function.rb, line 42 def initialize(service_name, service_stub, rpc_desc) @service_name = service_name @service_stub = service_stub @rpc_desc = rpc_desc @name = ::GRPC::GenericService.underscore(rpc_desc.name.to_s).to_sym end
Public Instance Methods
call(params = {}, metadata = {})
click to toggle source
# File lib/graphql_grpc/function.rb, line 88 def call(params = {}, metadata = {}) args = [name, arg(params || {}), metadata: metadata] arrayify_hashes normalize_result @service_stub.send(*args) end
function_args()
click to toggle source
# File lib/graphql_grpc/function.rb, line 53 def function_args result = TypeLibrary.descriptor_for(rpc_desc.input).types(InputTypeLibrary::PREFIX) result.any? ? "(#{result.join(', ')})" : '' end
input_type()
click to toggle source
# File lib/graphql_grpc/function.rb, line 58 def input_type rpc_desc.input.to_s.split(':').last end
output_type()
click to toggle source
# File lib/graphql_grpc/function.rb, line 62 def output_type out_type = begin streaming? ? rpc_desc.output.type : rpc_desc.output end.to_s.split(':').last streaming? ? "[#{out_type}]" : out_type end
to_query_type()
click to toggle source
# File lib/graphql_grpc/function.rb, line 69 def to_query_type # Turns out the single *Request type should NOT be the single arg. # # If GrpcFunctionNameRequest has two fields: # # foo: <type> # bar: <type> # # then the schema needs to show: # # GrpcFunctionName(foo: <type>, bar: <type>): GrpcFunctionNameResponse # # instead of: # # GrpcFunctionName(input: GrpcFunctionNameRequest): GrpcFunctionNameResponse # "#{rpc_desc.name}#{function_args}: #{output_type}!" end
to_s()
click to toggle source
# File lib/graphql_grpc/function.rb, line 49 def to_s "<GrpcFunction #{service_name}##{name} >" end
Private Instance Methods
arg(params)
click to toggle source
Build arguments to a func
# File lib/graphql_grpc/function.rb, line 114 def arg(params) rpc_desc.input.decode_json(params.reject { |k, _v| k == :selections }.to_json) end
normalize_result(result)
click to toggle source
# File lib/graphql_grpc/function.rb, line 95 def normalize_result(result) if result.is_a?(Enumerator) [].tap { |arr| result.each { |e| arr << e } } else if result.respond_to?(:to_h) result.to_h elsif result.respond_to?(:to_hash) result.to_hash else raise NotImplementedError end end end
streaming?()
click to toggle source
# File lib/graphql_grpc/function.rb, line 109 def streaming? rpc_desc.output.is_a?(GRPC::RpcDesc::Stream) end