Class: GraphqlGrpc::Proxy

Inherits:
Object
  • Object
show all
Includes:
Schema
Defined in:
lib/graphql_grpc/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Schema

#gql_mutations, #gql_queries, #query?, #streaming_response?, #to_function_types, #to_gql_schema, #to_schema_mutations, #to_schema_query, #to_schema_types

Constructor Details

#initialize(stub_services = {}, &block) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • stub_services (Hash) (defaults to: {})

    mapping of a service_name to an instance of a stub service.

  • error_presenter (Proc)

    a method that turns exceptions into a hash.



36
37
38
39
40
41
# File 'lib/graphql_grpc/proxy.rb', line 36

def initialize(stub_services = {}, &block)
  @function_map = {} # func name => hash containing details
  @services = {}
  @error_presenter = block
  map_functions(stub_services)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Proxy methods through to the services, instead of calling rpc()



76
77
78
79
80
# File 'lib/graphql_grpc/proxy.rb', line 76

def method_missing(method, *args, &block)
  return rpc(method, args.first, args[1]) if function(method)

  super
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



32
33
34
# File 'lib/graphql_grpc/proxy.rb', line 32

def services
  @services
end

Instance Method Details

#function(function_name, noisy = true) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
# File 'lib/graphql_grpc/proxy.rb', line 53

def function(function_name, noisy = true)
  # function_name is a symbol; calling #to_s and #underscore calls #gsub! on it
  # and it is frozen; so #dup first.
  func = @function_map[::GRPC::GenericService.underscore(function_name.to_s.dup).to_sym]
  raise RpcNotFoundError, "#{function_name} does not exist." if noisy && !func

  func
end

#healthcheckObject

Return a hash of all the healthchecks from all the services.



44
45
46
47
48
49
50
51
# File 'lib/graphql_grpc/proxy.rb', line 44

def healthcheck
  Hash[@services.map do |service_name, stub|
    hc = stub.send(:healthcheck, ::Google::Protobuf::Empty.new)
    raise HealthError, "#{service_name} is not healthy." unless hc && hc.processID > 0

    [service_name, hc]
  end]
end

#invoke(field, args, ctx) ⇒ Object



62
63
64
# File 'lib/graphql_grpc/proxy.rb', line 62

def invoke(field, args, ctx)
  rpc(field.name, args.to_h, {})
end

#respond_to_missing?(method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/graphql_grpc/proxy.rb', line 71

def respond_to_missing?(method, _include_private = false)
  !!function(method, false)
end

#rpc(function_name, params = {}, metadata = {}) ⇒ Object

Execute a function with given params.



67
68
69
# File 'lib/graphql_grpc/proxy.rb', line 67

def rpc(function_name, params = {},  = {})
  function(function_name).call(params,  || {})
end