diff options
author | charlieablett <cablett@gitlab.com> | 2019-06-16 22:12:56 +1200 |
---|---|---|
committer | charlieablett <cablett@gitlab.com> | 2019-07-04 15:44:24 +1200 |
commit | e43fb1f6a3f240f0833254157ce3ac4c2db0c846 (patch) | |
tree | 48c5176119d9e3ad10105b1e2e0bc75018af4ade /app/graphql | |
parent | 63a9c26aa234f89a7e69fcd32bdec9ad39c53ffc (diff) | |
download | gitlab-ce-e43fb1f6a3f240f0833254157ce3ac4c2db0c846.tar.gz |
Enumerate fields with Gitaly calls
- Add a complexity of 1 if Gitaly is called at least once
- Add an error notification if `calls_gitaly` isn't right for a
particular field
Diffstat (limited to 'app/graphql')
-rw-r--r-- | app/graphql/types/base_field.rb | 23 | ||||
-rw-r--r-- | app/graphql/types/project_type.rb | 2 | ||||
-rw-r--r-- | app/graphql/types/tree/tree_type.rb | 4 |
3 files changed, 25 insertions, 4 deletions
diff --git a/app/graphql/types/base_field.rb b/app/graphql/types/base_field.rb index dd0d9105df6..ee23146f711 100644 --- a/app/graphql/types/base_field.rb +++ b/app/graphql/types/base_field.rb @@ -4,21 +4,30 @@ module Types class BaseField < GraphQL::Schema::Field prepend Gitlab::Graphql::Authorize + attr_reader :calls_gitaly + DEFAULT_COMPLEXITY = 1 def initialize(*args, **kwargs, &block) + @calls_gitaly = !!kwargs.delete(:calls_gitaly) kwargs[:complexity] ||= field_complexity(kwargs[:resolver_class]) super(*args, **kwargs, &block) end + def base_complexity + complexity = DEFAULT_COMPLEXITY + complexity += 1 if @calls_gitaly + complexity + end + private def field_complexity(resolver_class) if resolver_class field_resolver_complexity else - DEFAULT_COMPLEXITY + base_complexity end end @@ -45,5 +54,17 @@ module Types complexity.to_i end end + + def calls_gitaly_check + # Will inform you if :calls_gitaly should be true or false based on the number of Gitaly calls + # involved with the request. + if @calls_gitaly && Gitlab::GitalyClient.get_request_count == 0 + raise "Gitaly is called for field '#{name}' - please add `calls_gitaly: true` to the field declaration" + elsif !@calls_gitaly && Gitlab::GitalyClient.get_request_count > 0 + raise "Gitaly not called for field '#{name}' - please remove `calls_gitaly: true` from the field declaration" + end + rescue => e + Gitlab::Sentry.track_exception(e) + end end end diff --git a/app/graphql/types/project_type.rb b/app/graphql/types/project_type.rb index c25688ab043..87d5351f80f 100644 --- a/app/graphql/types/project_type.rb +++ b/app/graphql/types/project_type.rb @@ -26,7 +26,7 @@ module Types field :web_url, GraphQL::STRING_TYPE, null: true field :star_count, GraphQL::INT_TYPE, null: false - field :forks_count, GraphQL::INT_TYPE, null: false + field :forks_count, GraphQL::INT_TYPE, null: false, calls_gitaly: true # 4 times field :created_at, Types::TimeType, null: true field :last_activity_at, Types::TimeType, null: true diff --git a/app/graphql/types/tree/tree_type.rb b/app/graphql/types/tree/tree_type.rb index b947713074e..2023abc13f9 100644 --- a/app/graphql/types/tree/tree_type.rb +++ b/app/graphql/types/tree/tree_type.rb @@ -15,9 +15,9 @@ module Types Gitlab::Graphql::Representation::TreeEntry.decorate(obj.trees, obj.repository) end - field :submodules, Types::Tree::SubmoduleType.connection_type, null: false + field :submodules, Types::Tree::SubmoduleType.connection_type, null: false, calls_gitaly: true - field :blobs, Types::Tree::BlobType.connection_type, null: false, resolve: -> (obj, args, ctx) do + field :blobs, Types::Tree::BlobType.connection_type, null: false, calls_gitaly: true, resolve: -> (obj, args, ctx) do Gitlab::Graphql::Representation::TreeEntry.decorate(obj.blobs, obj.repository) end # rubocop: enable Graphql/AuthorizeTypes |