Class: Artemis::GraphQLEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/artemis/graphql_endpoint.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url: nil, adapter: :net_http, timeout: 10, schema_path: nil, pool_size: 25, adapter_options: {}) ⇒ GraphQLEndpoint

Returns a new instance of GraphQLEndpoint.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/artemis/graphql_endpoint.rb', line 49

def initialize(name, url: nil, adapter: :net_http, timeout: 10, schema_path: nil, pool_size: 25, adapter_options: {})
  @name            = name.to_s
  @url             = url
  @adapter         = adapter
  @timeout         = timeout
  @schema_path     = schema_path
  @pool_size       = pool_size
  @adapter_options = adapter_options

  @mutex_for_schema     = Mutex.new
  @mutex_for_connection = Mutex.new
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def adapter
  @adapter
end

#adapter_optionsObject (readonly)

Returns the value of attribute adapter_options.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def adapter_options
  @adapter_options
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def name
  @name
end

#pool_sizeObject (readonly)

Returns the value of attribute pool_size.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def pool_size
  @pool_size
end

#schema_pathObject (readonly)

Returns the value of attribute schema_path.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def schema_path
  @schema_path
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



47
48
49
# File 'lib/artemis/graphql_endpoint.rb', line 47

def url
  @url
end

Class Method Details

.lookup(service_name) ⇒ Object

Provides an endpoint instance specified in the configuration. If the endpoint is not found in ENDPOINT_INSTANCES, it’ll raise an exception.



31
32
33
# File 'lib/artemis/graphql_endpoint.rb', line 31

def lookup(service_name)
  ENDPOINT_INSTANCES[service_name.to_s.underscore] || raise(Artemis::EndpointNotFound, "Service `#{service_name}' not registered.")
end

.register!(service_name, configurations) ⇒ Object



35
36
37
# File 'lib/artemis/graphql_endpoint.rb', line 35

def register!(service_name, configurations)
  ENDPOINT_INSTANCES[service_name.to_s.underscore] = new(service_name.to_s, **configurations.symbolize_keys)
end

.registered_servicesObject

Returns the registered services as an array.



42
43
44
# File 'lib/artemis/graphql_endpoint.rb', line 42

def registered_services
  ENDPOINT_INSTANCES.keys
end

Instance Method Details

#connectionObject



73
74
75
76
77
# File 'lib/artemis/graphql_endpoint.rb', line 73

def connection
  @connection || @mutex_for_connection.synchronize do
    @connection ||= ::Artemis::Adapters.lookup(adapter).new(url, service_name: name, timeout: timeout, pool_size: pool_size, adapter_options: adapter_options)
  end
end

#schemaObject Also known as: load_schema!



62
63
64
65
66
67
68
69
70
# File 'lib/artemis/graphql_endpoint.rb', line 62

def schema
  org, $stderr = $stderr, File.new("/dev/null", "w") if self.class.suppress_warnings_on_schema_load

  @schema || @mutex_for_schema.synchronize do
    @schema ||= ::GraphQL::Client.load_schema(schema_path.presence || connection)
  end
ensure
  $stderr = org if self.class.suppress_warnings_on_schema_load
end