Class: GraphqlGrpc::Function

Inherits:
Object
  • Object
show all
Includes:
Arrayify
Defined in:
lib/graphql_grpc/function.rb

Overview

Storage for an actual function definition. Implements a ‘call` method so that it may be invoked with a simple hash of params.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arrayify

#arrayify_hashes, #numeric_klass

Constructor Details

#initialize(service_name, service_stub, rpc_desc) ⇒ Function

Returns a new instance of Function.



42
43
44
45
46
47
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/graphql_grpc/function.rb', line 40

def name
  @name
end

#rpc_descObject (readonly)

Returns the value of attribute rpc_desc.



40
41
42
# File 'lib/graphql_grpc/function.rb', line 40

def rpc_desc
  @rpc_desc
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



40
41
42
# File 'lib/graphql_grpc/function.rb', line 40

def service_name
  @service_name
end

Instance Method Details

#call(params = {}, metadata = {}) ⇒ Object



88
89
90
91
# File 'lib/graphql_grpc/function.rb', line 88

def call(params = {},  = {})
  args = [name, arg(params || {}), metadata: ]
  arrayify_hashes normalize_result @service_stub.send(*args)
end

#function_argsObject



53
54
55
56
# 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_typeObject



58
59
60
# File 'lib/graphql_grpc/function.rb', line 58

def input_type
  rpc_desc.input.to_s.split(':').last
end

#output_typeObject



62
63
64
65
66
67
# 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_typeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_sObject



49
50
51
# File 'lib/graphql_grpc/function.rb', line 49

def to_s
  "<GrpcFunction #{service_name}##{name} >"
end