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