2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
class GitlabSchema < GraphQL::Schema
|
2019-04-05 13:30:10 -04:00
|
|
|
# Currently an IntrospectionQuery has a complexity of 179.
|
2019-03-27 16:02:25 -04:00
|
|
|
# These values will evolve over time.
|
2019-04-05 13:30:10 -04:00
|
|
|
DEFAULT_MAX_COMPLEXITY = 200
|
|
|
|
AUTHENTICATED_COMPLEXITY = 250
|
|
|
|
ADMIN_COMPLEXITY = 300
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
ANONYMOUS_MAX_DEPTH = 10
|
|
|
|
AUTHENTICATED_MAX_DEPTH = 15
|
|
|
|
|
2018-02-23 10:36:40 -05:00
|
|
|
use BatchLoader::GraphQL
|
2018-05-23 03:55:14 -04:00
|
|
|
use Gitlab::Graphql::Authorize
|
|
|
|
use Gitlab::Graphql::Present
|
2018-06-26 12:31:05 -04:00
|
|
|
use Gitlab::Graphql::Connections
|
2019-05-02 03:01:14 -04:00
|
|
|
use Gitlab::Graphql::GenericTracing
|
2017-08-16 09:04:41 -04:00
|
|
|
|
2019-03-27 16:02:25 -04:00
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::LogQueryComplexity.analyzer
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
query(Types::QueryType)
|
2018-06-26 12:31:05 -04:00
|
|
|
|
|
|
|
default_max_page_size 100
|
2019-03-27 16:02:25 -04:00
|
|
|
|
|
|
|
max_complexity DEFAULT_MAX_COMPLEXITY
|
|
|
|
|
2018-07-10 10:19:45 -04:00
|
|
|
mutation(Types::MutationType)
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
class << self
|
|
|
|
def execute(query_str = nil, **kwargs)
|
|
|
|
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context])
|
|
|
|
kwargs[:max_depth] ||= max_query_depth(kwargs[:context])
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
super(query_str, **kwargs)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def max_query_complexity(ctx)
|
|
|
|
current_user = ctx&.fetch(:current_user, nil)
|
|
|
|
|
|
|
|
if current_user&.admin
|
|
|
|
ADMIN_COMPLEXITY
|
|
|
|
elsif current_user
|
|
|
|
AUTHENTICATED_COMPLEXITY
|
|
|
|
else
|
|
|
|
DEFAULT_MAX_COMPLEXITY
|
|
|
|
end
|
|
|
|
end
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
def max_query_depth(ctx)
|
|
|
|
current_user = ctx&.fetch(:current_user, nil)
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
if current_user
|
|
|
|
AUTHENTICATED_MAX_DEPTH
|
|
|
|
else
|
|
|
|
ANONYMOUS_MAX_DEPTH
|
|
|
|
end
|
2019-03-27 16:02:25 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|