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

field_call_count.rb « limit « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a02e8abbb581b4295f3464885d24e7c1822ebe2 (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
# frozen_string_literal: true

module Gitlab
  module Graphql
    module Limit
      class FieldCallCount < ::GraphQL::Schema::FieldExtension
        def resolve(object:, arguments:, context:)
          raise Gitlab::Graphql::Errors::ArgumentError, 'Limit must be specified.' unless limit
          raise Gitlab::Graphql::Errors::LimitError, error_message if increment_call_count(context) > limit

          yield(object, arguments)
        end

        private

        def increment_call_count(context)
          query_id = fetch_query_id(context)

          context[:call_count] ||= {}
          context[:call_count][query_id] ||= {}
          context[:call_count][query_id][field] ||= 0
          context[:call_count][query_id][field] += 1
        end

        def fetch_query_id(context)
          context.query.operation_fingerprint
        rescue TypeError
          ''
        end

        def limit
          options[:limit]
        end

        def error_message
          "\"#{field.graphql_name}\" field can be requested only for #{limit} #{field.owner.graphql_name}(s) at a time."
        end
      end
    end
  end
end