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-06-07 17:13:26 +00:00
|
|
|
DEFAULT_MAX_DEPTH = 15
|
|
|
|
AUTHENTICATED_MAX_DEPTH = 20
|
2019-05-06 14:00:03 +00:00
|
|
|
|
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
|
2019-06-21 14:20:00 +00:00
|
|
|
use Gitlab::Graphql::CallsGitaly
|
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-05-02 00:16:49 +00:00
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new
|
2019-08-22 14:17:38 +00:00
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::RecursionAnalyzer.new
|
2019-03-27 20:02:25 +00:00
|
|
|
|
|
|
|
max_complexity DEFAULT_MAX_COMPLEXITY
|
2019-05-09 09:27:07 +00:00
|
|
|
max_depth DEFAULT_MAX_DEPTH
|
2019-03-27 20:02:25 +00:00
|
|
|
|
2019-08-22 14:17:38 +00:00
|
|
|
query Types::QueryType
|
|
|
|
mutation Types::MutationType
|
|
|
|
|
|
|
|
default_max_page_size 100
|
2019-03-27 20:02:25 +00:00
|
|
|
|
2019-05-06 14:00:03 +00:00
|
|
|
class << self
|
2019-05-09 09:27:07 +00: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 14:00:03 +00: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 20:02:25 +00:00
|
|
|
|
2019-05-06 14:00:03 +00:00
|
|
|
super(query_str, **kwargs)
|
|
|
|
end
|
|
|
|
|
2019-11-04 09:06:21 +00:00
|
|
|
def id_from_object(object, _type = nil, _ctx = nil)
|
2019-06-03 17:38:16 +00: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 08:48:06 +00:00
|
|
|
# more meaningful error message
|
2019-06-03 17:38:16 +00:00
|
|
|
raise "#{object} does not implement `to_global_id`. "\
|
|
|
|
"Include `GlobalID::Identification` into `#{object.class}"
|
|
|
|
end
|
|
|
|
|
|
|
|
object.to_global_id
|
|
|
|
end
|
|
|
|
|
2019-11-04 09:06:21 +00:00
|
|
|
def object_from_id(global_id, _ctx = nil)
|
2019-06-03 17:38:16 +00: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-04 03:33:14 +00:00
|
|
|
elsif gid.model_class.respond_to?(:lazy_find)
|
|
|
|
gid.model_class.lazy_find(gid.model_id)
|
2019-06-03 17:38:16 +00:00
|
|
|
else
|
|
|
|
gid.find
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-06 14:00:03 +00: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 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
|
2019-05-09 09:27:07 +00:00
|
|
|
DEFAULT_MAX_DEPTH
|
2019-05-06 14:00:03 +00:00
|
|
|
end
|
2019-03-27 20:02:25 +00:00
|
|
|
end
|
|
|
|
end
|
2017-08-16 13:04:41 +00:00
|
|
|
end
|