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-06-07 13:13:26 -04:00
|
|
|
DEFAULT_MAX_DEPTH = 15
|
|
|
|
AUTHENTICATED_MAX_DEPTH = 20
|
2019-05-06 10:00:03 -04:00
|
|
|
|
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
|
2019-06-21 10:20:00 -04:00
|
|
|
use Gitlab::Graphql::CallsGitaly
|
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-05-01 20:16:49 -04:00
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new
|
2019-08-22 10:17:38 -04:00
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::RecursionAnalyzer.new
|
2019-03-27 16:02:25 -04:00
|
|
|
|
|
|
|
max_complexity DEFAULT_MAX_COMPLEXITY
|
2019-05-09 05:27:07 -04:00
|
|
|
max_depth DEFAULT_MAX_DEPTH
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-08-22 10:17:38 -04:00
|
|
|
query Types::QueryType
|
|
|
|
mutation Types::MutationType
|
|
|
|
|
|
|
|
default_max_page_size 100
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
class << self
|
2019-05-09 05:27:07 -04:00
|
|
|
def multiplex(queries, **kwargs)
|
|
|
|
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context])
|
|
|
|
|
|
|
|
queries.each do |query|
|
|
|
|
query[:max_depth] = max_query_depth(kwargs[:context])
|
|
|
|
end
|
|
|
|
|
|
|
|
super(queries, **kwargs)
|
|
|
|
end
|
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
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
|
|
|
|
|
2019-11-04 04:06:21 -05:00
|
|
|
def id_from_object(object, _type = nil, _ctx = nil)
|
2019-06-03 13:38:16 -04:00
|
|
|
unless object.respond_to?(:to_global_id)
|
|
|
|
# This is an error in our schema and needs to be solved. So raise a
|
2019-08-26 04:48:06 -04:00
|
|
|
# more meaningful error message
|
2019-06-03 13:38:16 -04:00
|
|
|
raise "#{object} does not implement `to_global_id`. "\
|
|
|
|
"Include `GlobalID::Identification` into `#{object.class}"
|
|
|
|
end
|
|
|
|
|
|
|
|
object.to_global_id
|
|
|
|
end
|
|
|
|
|
2019-11-04 04:06:21 -05:00
|
|
|
def object_from_id(global_id, _ctx = nil)
|
2019-06-03 13:38:16 -04:00
|
|
|
gid = GlobalID.parse(global_id)
|
|
|
|
|
|
|
|
unless gid
|
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, "#{global_id} is not a valid GitLab id."
|
|
|
|
end
|
|
|
|
|
|
|
|
if gid.model_class < ApplicationRecord
|
|
|
|
Gitlab::Graphql::Loaders::BatchModelLoader.new(gid.model_class, gid.model_id).find
|
2019-07-03 23:33:14 -04:00
|
|
|
elsif gid.model_class.respond_to?(:lazy_find)
|
|
|
|
gid.model_class.lazy_find(gid.model_id)
|
2019-06-03 13:38:16 -04:00
|
|
|
else
|
|
|
|
gid.find
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
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
|
2019-05-09 05:27:07 -04:00
|
|
|
DEFAULT_MAX_DEPTH
|
2019-05-06 10:00:03 -04:00
|
|
|
end
|
2019-03-27 16:02:25 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|