diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-26 09:08:47 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-26 09:08:47 +0000 |
commit | 66d4203791a01fdedf668a78818a229ea2c07aad (patch) | |
tree | 374fc9f6c5e709cf6ab48e257e6bfe4a504d6bbb /app/graphql | |
parent | a496f41f60e12a0a5c31482b7594ad547e0ade42 (diff) | |
download | gitlab-ce-66d4203791a01fdedf668a78818a229ea2c07aad.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/graphql')
-rw-r--r-- | app/graphql/resolvers/base_resolver.rb | 4 | ||||
-rw-r--r-- | app/graphql/types/base_field.rb | 34 |
2 files changed, 26 insertions, 12 deletions
diff --git a/app/graphql/resolvers/base_resolver.rb b/app/graphql/resolvers/base_resolver.rb index 66cb224f157..cf0642930ad 100644 --- a/app/graphql/resolvers/base_resolver.rb +++ b/app/graphql/resolvers/base_resolver.rb @@ -28,6 +28,10 @@ module Resolvers end end + def self.complexity + 0 + end + def self.resolver_complexity(args, child_complexity:) complexity = 1 complexity += 1 if args[:sort] diff --git a/app/graphql/types/base_field.rb b/app/graphql/types/base_field.rb index 3ade1300c2d..8ff2b5ad532 100644 --- a/app/graphql/types/base_field.rb +++ b/app/graphql/types/base_field.rb @@ -9,7 +9,7 @@ module Types def initialize(*args, **kwargs, &block) @calls_gitaly = !!kwargs.delete(:calls_gitaly) @constant_complexity = !!kwargs[:complexity] - kwargs[:complexity] ||= field_complexity(kwargs[:resolver_class]) + kwargs[:complexity] = field_complexity(kwargs[:resolver_class], kwargs[:complexity]) @feature_flag = kwargs[:feature_flag] kwargs = check_feature_flag(kwargs) @@ -51,7 +51,9 @@ module Types args end - def field_complexity(resolver_class) + def field_complexity(resolver_class, current) + return current if current.present? && current > 0 + if resolver_class field_resolver_complexity else @@ -66,22 +68,30 @@ module Types # proc because we set complexity depending on arguments and number of # items which can be loaded. proc do |ctx, args, child_complexity| + next base_complexity unless resolver_complexity_enabled?(ctx) + # Resolvers may add extra complexity depending on used arguments complexity = child_complexity + self.resolver&.try(:resolver_complexity, args, child_complexity: child_complexity).to_i complexity += 1 if calls_gitaly? - - field_defn = to_graphql - - if field_defn.connection? - # Resolvers may add extra complexity depending on number of items being loaded. - page_size = field_defn.connection_max_page_size || ctx.schema.default_max_page_size - limit_value = [args[:first], args[:last], page_size].compact.min - multiplier = self.resolver&.try(:complexity_multiplier, args).to_f - complexity += complexity * limit_value * multiplier - end + complexity += complexity * connection_complexity_multiplier(ctx, args) complexity.to_i end end + + def resolver_complexity_enabled?(ctx) + ctx.fetch(:graphql_resolver_complexity_flag) { |key| ctx[key] = Feature.enabled?(:graphql_resolver_complexity) } + end + + def connection_complexity_multiplier(ctx, args) + # Resolvers may add extra complexity depending on number of items being loaded. + field_defn = to_graphql + return 0 unless field_defn.connection? + + page_size = field_defn.connection_max_page_size || ctx.schema.default_max_page_size + limit_value = [args[:first], args[:last], page_size].compact.min + multiplier = self.resolver&.try(:complexity_multiplier, args).to_f + limit_value * multiplier + end end end |