Welcome to mirror list, hosted at ThFree Co, Russian Federation.

application_context_tracer.rb « tracers « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4193c46e3215e200b7d989188be1bf784a5f5e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module Gitlab
  module Graphql
    module Tracers
      # This graphql-ruby tracer sets up `ApplicationContext` for certain operations.
      class ApplicationContextTracer
        def self.use(schema)
          schema.tracer(self.new)
        end

        # See docs on expected interface for trace
        # https://graphql-ruby.org/api-doc/1.12.17/GraphQL/Tracing
        def trace(key, data)
          case key
          when "execute_query"
            operation = known_operation(data)

            ::Gitlab::ApplicationContext.with_context(caller_id: operation.to_caller_id) do
              yield
            end
          else
            yield
          end
        end

        private

        def known_operation(data)
          # The library guarantees that we should have :query for execute_query, but we're being defensive here
          query = data.fetch(:query, nil)

          return ::Gitlab::Graphql::KnownOperations.UNKNOWN unless query

          ::Gitlab::Graphql::KnownOperations.default.from_query(query)
        end
      end
    end
  end
end