class GraphqlGrpc::Proxy

Attributes

services[R]

Public Class Methods

new(stub_services = {}, &block) click to toggle source

@param stub_services [Hash] mapping of a service_name to an instance of a stub service. @param error_presenter [Proc] a method that turns exceptions into a hash.

# 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

Public Instance Methods

function(function_name, noisy = true) click to toggle source
# 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
healthcheck() click to toggle source

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

# 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) click to toggle source
# File lib/graphql_grpc/proxy.rb, line 62
def invoke(field, args, ctx)
  rpc(field.name, args.to_h, {})
end
method_missing(method, *args, &block) click to toggle source

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

Calls superclass method
# 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
respond_to_missing?(method, _include_private = false) click to toggle source
# 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 = {}) click to toggle source

Execute a function with given params.

# File lib/graphql_grpc/proxy.rb, line 67
def rpc(function_name, params = {}, metadata = {})
  function(function_name).call(params, metadata || {})
end

Private Instance Methods

map_functions(stub_services) click to toggle source

Add to the function_map by inspecting each service for the RPCs it provides.

# File lib/graphql_grpc/proxy.rb, line 85
def map_functions(stub_services)
  return @function_map unless @function_map.empty?

  stub_services.keys.each do |service_name|
    stub = @services[service_name] = stub_services[service_name]
    stub.class.to_s.gsub('::Stub', '::Service').constantize.rpc_descs.values.each do |d|
      next if d.name.to_sym == :Healthcheck

      grpc_func = ::GraphqlGrpc::Function.new(service_name, stub, d)
      if @function_map.key?(grpc_func.name)
        sn = @function_map[grpc_func.name].service_name
        STDERR.puts "Skipping method #{grpc_func.name}; it was already defined on #{sn}"
        # raise ConfigurationError, "#{grpc_func.name} was already defined on #{sn}."
      end
      @function_map[grpc_func.name] = grpc_func
    end
  end
  @function_map
end