Commit 163a3575 authored by James Fargher's avatar James Fargher

Merge branch 'ld-simplify-gitlab_schema-execute' into 'master'

Remove `GitlabSchema#execute`, and small tidy ups related to max limits

See merge request gitlab-org/gitlab!67590
parents 85e38e82 aea37f50
...@@ -4,8 +4,8 @@ class GitlabSchema < GraphQL::Schema ...@@ -4,8 +4,8 @@ class GitlabSchema < GraphQL::Schema
# Currently an IntrospectionQuery has a complexity of 179. # Currently an IntrospectionQuery has a complexity of 179.
# These values will evolve over time. # These values will evolve over time.
DEFAULT_MAX_COMPLEXITY = 200 DEFAULT_MAX_COMPLEXITY = 200
AUTHENTICATED_COMPLEXITY = 250 AUTHENTICATED_MAX_COMPLEXITY = 250
ADMIN_COMPLEXITY = 300 ADMIN_MAX_COMPLEXITY = 300
DEFAULT_MAX_DEPTH = 15 DEFAULT_MAX_DEPTH = 15
AUTHENTICATED_MAX_DEPTH = 20 AUTHENTICATED_MAX_DEPTH = 20
...@@ -20,9 +20,6 @@ class GitlabSchema < GraphQL::Schema ...@@ -20,9 +20,6 @@ class GitlabSchema < GraphQL::Schema
query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new
query_analyzer Gitlab::Graphql::QueryAnalyzers::RecursionAnalyzer.new query_analyzer Gitlab::Graphql::QueryAnalyzers::RecursionAnalyzer.new
max_complexity DEFAULT_MAX_COMPLEXITY
max_depth DEFAULT_MAX_DEPTH
query Types::QueryType query Types::QueryType
mutation Types::MutationType mutation Types::MutationType
subscription Types::SubscriptionType subscription Types::SubscriptionType
...@@ -36,20 +33,13 @@ class GitlabSchema < GraphQL::Schema ...@@ -36,20 +33,13 @@ class GitlabSchema < GraphQL::Schema
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context]) unless kwargs.key?(:max_complexity) kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context]) unless kwargs.key?(:max_complexity)
queries.each do |query| queries.each do |query|
query[:max_complexity] ||= max_query_complexity(kwargs[:context]) unless query.key?(:max_complexity) query[:max_complexity] ||= max_query_complexity(query[:context]) unless query.key?(:max_complexity)
query[:max_depth] = max_query_depth(kwargs[:context]) query[:max_depth] = max_query_depth(query[:context]) unless query.key?(:max_depth)
end end
super(queries, **kwargs) super(queries, **kwargs)
end end
def execute(query_str = nil, **kwargs)
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context])
kwargs[:max_depth] ||= max_query_depth(kwargs[:context])
super(query_str, **kwargs)
end
def get_type(type_name) def get_type(type_name)
type_name = Gitlab::GlobalId::Deprecations.apply_to_graphql_name(type_name) type_name = Gitlab::GlobalId::Deprecations.apply_to_graphql_name(type_name)
...@@ -142,9 +132,9 @@ class GitlabSchema < GraphQL::Schema ...@@ -142,9 +132,9 @@ class GitlabSchema < GraphQL::Schema
current_user = ctx&.fetch(:current_user, nil) current_user = ctx&.fetch(:current_user, nil)
if current_user&.admin if current_user&.admin
ADMIN_COMPLEXITY ADMIN_MAX_COMPLEXITY
elsif current_user elsif current_user
AUTHENTICATED_COMPLEXITY AUTHENTICATED_MAX_COMPLEXITY
else else
DEFAULT_MAX_COMPLEXITY DEFAULT_MAX_COMPLEXITY
end end
......
...@@ -129,7 +129,7 @@ module Types ...@@ -129,7 +129,7 @@ module Types
description: "Find runners visible to the current user.", description: "Find runners visible to the current user.",
feature_flag: :runner_graphql_query feature_flag: :runner_graphql_query
field :ci_config, resolver: Resolvers::Ci::ConfigResolver, complexity: 126 # AUTHENTICATED_COMPLEXITY / 2 + 1 field :ci_config, resolver: Resolvers::Ci::ConfigResolver, complexity: 126 # AUTHENTICATED_MAX_COMPLEXITY / 2 + 1
def design_management def design_management
DesignManagementObject.new(nil) DesignManagementObject.new(nil)
......
...@@ -56,9 +56,9 @@ namespace :gitlab do ...@@ -56,9 +56,9 @@ namespace :gitlab do
color = case complexity color = case complexity
when 0..GitlabSchema::DEFAULT_MAX_COMPLEXITY when 0..GitlabSchema::DEFAULT_MAX_COMPLEXITY
:green :green
when GitlabSchema::DEFAULT_MAX_COMPLEXITY..GitlabSchema::AUTHENTICATED_COMPLEXITY when GitlabSchema::DEFAULT_MAX_COMPLEXITY..GitlabSchema::AUTHENTICATED_MAX_COMPLEXITY
:yellow :yellow
when GitlabSchema::AUTHENTICATED_COMPLEXITY..GitlabSchema::ADMIN_COMPLEXITY when GitlabSchema::AUTHENTICATED_MAX_COMPLEXITY..GitlabSchema::ADMIN_MAX_COMPLEXITY
:orange :orange
else else
:red :red
......
...@@ -56,6 +56,7 @@ module DeprecationToolkitEnv ...@@ -56,6 +56,7 @@ module DeprecationToolkitEnv
def self.allowed_kwarg_warning_paths def self.allowed_kwarg_warning_paths
%w[ %w[
actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb actionpack-6.1.3.2/lib/action_dispatch/routing/route_set.rb
graphql-1.11.8/lib/graphql/schema.rb
] ]
end end
......
...@@ -36,75 +36,66 @@ RSpec.describe GitlabSchema do ...@@ -36,75 +36,66 @@ RSpec.describe GitlabSchema do
end end
describe '.execute' do describe '.execute' do
context 'with different types of users' do describe 'setting query `max_complexity` and `max_depth`' do
context 'when no context' do subject(:result) { described_class.execute('query', kwargs).query }
it 'returns DEFAULT_MAX_COMPLEXITY' do
expect(GraphQL::Schema)
.to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::DEFAULT_MAX_COMPLEXITY))
described_class.execute('query') shared_examples 'sets default limits' do
specify do
expect(result).to have_attributes(
max_complexity: GitlabSchema::DEFAULT_MAX_COMPLEXITY,
max_depth: GitlabSchema::DEFAULT_MAX_DEPTH
)
end end
end end
context 'when no user' do context 'with no context' do
it 'returns DEFAULT_MAX_COMPLEXITY' do let(:kwargs) { {} }
expect(GraphQL::Schema)
.to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::DEFAULT_MAX_COMPLEXITY))
described_class.execute('query', context: {}) include_examples 'sets default limits'
end end
it 'returns DEFAULT_MAX_DEPTH' do context 'with no :current_user' do
expect(GraphQL::Schema) let(:kwargs) { { context: {} } }
.to receive(:execute)
.with('query', hash_including(max_depth: GitlabSchema::DEFAULT_MAX_DEPTH))
described_class.execute('query', context: {}) include_examples 'sets default limits'
end
end
context 'when a logged in user' do
it 'returns AUTHENTICATED_COMPLEXITY' do
expect(GraphQL::Schema).to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::AUTHENTICATED_COMPLEXITY))
described_class.execute('query', context: { current_user: user })
end end
it 'returns AUTHENTICATED_MAX_DEPTH' do context 'with anonymous user' do
expect(GraphQL::Schema).to receive(:execute) let(:kwargs) { { context: { current_user: nil } } }
.with('query', hash_including(max_depth: GitlabSchema::AUTHENTICATED_MAX_DEPTH))
described_class.execute('query', context: { current_user: user }) include_examples 'sets default limits'
end
end end
context 'when an admin user' do context 'with a logged in user' do
it 'returns ADMIN_COMPLEXITY' do let(:kwargs) { { context: { current_user: user } } }
user = build :user, :admin
expect(GraphQL::Schema).to receive(:execute)
.with('query', hash_including(max_complexity: GitlabSchema::ADMIN_COMPLEXITY))
described_class.execute('query', context: { current_user: user }) it 'sets authenticated user limits' do
expect(result).to have_attributes(
max_complexity: GitlabSchema::AUTHENTICATED_MAX_COMPLEXITY,
max_depth: GitlabSchema::AUTHENTICATED_MAX_DEPTH
)
end end
end end
context 'when max_complexity passed on the query' do context 'with an admin user' do
it 'returns what was passed on the query' do let(:kwargs) { { context: { current_user: build(:user, :admin) } } }
expect(GraphQL::Schema).to receive(:execute).with('query', hash_including(max_complexity: 1234))
described_class.execute('query', max_complexity: 1234) it 'sets admin/authenticated user limits' do
expect(result).to have_attributes(
max_complexity: GitlabSchema::ADMIN_MAX_COMPLEXITY,
max_depth: GitlabSchema::AUTHENTICATED_MAX_DEPTH
)
end end
end end
context 'when max_depth passed on the query' do context 'when limits passed as kwargs' do
it 'returns what was passed on the query' do let(:kwargs) { { max_complexity: 1234, max_depth: 4321 } }
expect(GraphQL::Schema).to receive(:execute).with('query', hash_including(max_depth: 1234))
described_class.execute('query', max_depth: 1234) it 'sets limits from the kwargs' do
expect(result).to have_attributes(
max_complexity: 1234,
max_depth: 4321
)
end end
end end
end end
......
...@@ -385,7 +385,7 @@ RSpec.describe 'GraphQL' do ...@@ -385,7 +385,7 @@ RSpec.describe 'GraphQL' do
context 'authenticated user' do context 'authenticated user' do
subject { post_graphql(query, current_user: user) } subject { post_graphql(query, current_user: user) }
it 'does not raise an error as it uses the `AUTHENTICATED_COMPLEXITY`' do it 'does not raise an error as it uses the `AUTHENTICATED_MAX_COMPLEXITY`' do
subject subject
expect(graphql_errors).to be_nil expect(graphql_errors).to be_nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment