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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-09-14 03:10:29 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2022-09-14 03:10:29 +0300
commit3521fa595b022a402f3ed1e8c423021e6ad8ae49 (patch)
tree6cfbb28fcf84409f42212753abd05740ceeae3e4 /lib/gitlab/graphql
parent21eec1097daf6817af98894cf53fa436a25c5ce1 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/graphql')
-rw-r--r--lib/gitlab/graphql/errors.rb1
-rw-r--r--lib/gitlab/graphql/limit/field_call_count.rb32
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/gitlab/graphql/errors.rb b/lib/gitlab/graphql/errors.rb
index 40b90310e8b..657364abfdf 100644
--- a/lib/gitlab/graphql/errors.rb
+++ b/lib/gitlab/graphql/errors.rb
@@ -7,6 +7,7 @@ module Gitlab
ArgumentError = Class.new(BaseError)
ResourceNotAvailable = Class.new(BaseError)
MutationError = Class.new(BaseError)
+ LimitError = Class.new(BaseError)
end
end
end
diff --git a/lib/gitlab/graphql/limit/field_call_count.rb b/lib/gitlab/graphql/limit/field_call_count.rb
new file mode 100644
index 00000000000..4165970a2a6
--- /dev/null
+++ b/lib/gitlab/graphql/limit/field_call_count.rb
@@ -0,0 +1,32 @@
+# 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)
+ context[:call_count] ||= {}
+ context[:call_count][field] ||= 0
+ context[:call_count][field] += 1
+ 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