summaryrefslogtreecommitdiff
path: root/lib/gitlab/etag_caching/router/graphql.rb
blob: 7a0fb2ac26914cb0ee1228333ac3c6b3aef084c6 (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
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module Gitlab
  module EtagCaching
    module Router
      class Graphql
        extend EtagCaching::Router::Helpers
        GRAPHQL_ETAG_RESOURCE_HEADER = 'X-GITLAB-GRAPHQL-RESOURCE-ETAG'

        ROUTES = [
          [
            %r(\Apipelines/id/\d+\z),
            'pipelines_graph',
            'continuous_integration'
          ],
          [
            %r(\Apipelines/sha/\w{7,40}\z),
            'ci_editor',
            'pipeline_authoring'
          ],
          [
            %r(\Aon_demand_scan/counts/),
            'on_demand_scans',
            'dynamic_application_security_testing'
          ]
        ].map { |attrs| build_graphql_route(*attrs) }.freeze

        def self.match(request)
          return unless request.path_info == graphql_api_path

          graphql_resource = request.headers[GRAPHQL_ETAG_RESOURCE_HEADER]
          return unless graphql_resource

          ROUTES.find { |route| route.match(graphql_resource) }
        end

        def self.cache_key(request)
          [
            request.path,
            request.headers[GRAPHQL_ETAG_RESOURCE_HEADER]
          ].compact.join(':')
        end

        def self.graphql_api_path
          @graphql_api_path ||= Gitlab::Routing.url_helpers.api_graphql_path
        end
        private_class_method :graphql_api_path
      end
    end
  end
end