Module: ElasticGraph::Apollo::GraphQL::HTTPEndpointExtension

Defined in:
lib/elastic_graph/apollo/graphql/http_endpoint_extension.rb

Overview

This extension is designed to hook ‘ElasticGraph::GraphQL::HTTPEndpoint` in order to provide Apollo Federated Tracing:

www.apollographql.com/docs/federation/metrics/

Luckily, the apollo-federation gem supports this–we just need to:

  1. Use the ‘ApolloFederation::Tracing` plugin (implemented via `EngineExtension#graphql_gem_plugins`).

  2. Conditionally pass ‘tracing_enabled: true` into in `context`.

This extension handles the latter requirement. For more info, see: github.com/Gusto/apollo-federation-ruby#tracing

Instance Method Summary collapse

Instance Method Details

#with_context(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elastic_graph/apollo/graphql/http_endpoint_extension.rb', line 27

def with_context(request)
  # Steep has an error here for some reason:
  # UnexpectedError: undefined method `selector' for #<Parser::Source::Map::Keyword:0x0000000131979b18>
  __skip__ = super(request) do |context|
    # `ApolloFederation::Tracing.should_add_traces` expects the header to be in SCREAMING_SNAKE_CASE with an HTTP_ prefix:
    # https://github.com/Gusto/apollo-federation-ruby/blob/v3.8.4/lib/apollo-federation/tracing.rb#L5
    normalized_headers = request.headers.transform_keys { |key| "HTTP_#{key.upcase.tr("-", "_")}" }

    if ApolloFederation::Tracing.should_add_traces(normalized_headers)
      context = context.merge(tracing_enabled: true)
    end

    yield context
  end
end