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.
|
2021-08-08 23:10:10 -04:00
|
|
|
DEFAULT_MAX_COMPLEXITY = 200
|
|
|
|
AUTHENTICATED_MAX_COMPLEXITY = 250
|
|
|
|
ADMIN_MAX_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
|
|
|
|
2021-10-29 11:11:56 -04:00
|
|
|
# Tracers (order is important)
|
2021-11-09 07:12:15 -05:00
|
|
|
use Gitlab::Graphql::Tracers::ApplicationContextTracer
|
2021-11-09 13:13:13 -05:00
|
|
|
use Gitlab::Graphql::Tracers::MetricsTracer
|
2021-10-29 11:11:56 -04:00
|
|
|
use Gitlab::Graphql::Tracers::LoggerTracer
|
2022-06-01 14:09:44 -04:00
|
|
|
|
|
|
|
# TODO: Old tracer which will be removed eventually
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/345396
|
|
|
|
use Gitlab::Graphql::GenericTracing
|
2021-10-29 11:11:56 -04:00
|
|
|
use Gitlab::Graphql::Tracers::TimerTracer
|
|
|
|
|
2021-04-23 05:10:03 -04:00
|
|
|
use GraphQL::Subscriptions::ActionCableSubscriptions
|
2018-02-23 10:36:40 -05:00
|
|
|
use BatchLoader::GraphQL
|
2020-04-07 23:09:31 -04:00
|
|
|
use Gitlab::Graphql::Pagination::Connections
|
2020-03-10 20:09:09 -04:00
|
|
|
use Gitlab::Graphql::Timeout, max_seconds: Gitlab.config.gitlab.graphql_timeout
|
2017-08-16 09:04:41 -04:00
|
|
|
|
2022-06-01 14:09:44 -04:00
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::AST::LoggerAnalyzer
|
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::AST::RecursionAnalyzer
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2019-08-22 10:17:38 -04:00
|
|
|
query Types::QueryType
|
|
|
|
mutation Types::MutationType
|
2021-04-23 05:10:03 -04:00
|
|
|
subscription Types::SubscriptionType
|
2019-08-22 10:17:38 -04:00
|
|
|
|
|
|
|
default_max_page_size 100
|
2019-03-27 16:02:25 -04:00
|
|
|
|
2021-12-06 22:12:22 -05:00
|
|
|
validate_max_errors 5
|
|
|
|
validate_timeout 0.2.seconds
|
|
|
|
|
2020-10-30 11:08:59 -04:00
|
|
|
lazy_resolve ::Gitlab::Graphql::Lazy, :force
|
|
|
|
|
2019-05-06 10:00:03 -04:00
|
|
|
class << self
|
2019-05-09 05:27:07 -04:00
|
|
|
def multiplex(queries, **kwargs)
|
2021-04-06 11:09:23 -04:00
|
|
|
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context]) unless kwargs.key?(:max_complexity)
|
2019-05-09 05:27:07 -04:00
|
|
|
|
|
|
|
queries.each do |query|
|
2021-08-08 23:10:10 -04:00
|
|
|
query[:max_complexity] ||= max_query_complexity(query[:context]) unless query.key?(:max_complexity)
|
|
|
|
query[:max_depth] = max_query_depth(query[:context]) unless query.key?(:max_depth)
|
2019-05-09 05:27:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
super(queries, **kwargs)
|
|
|
|
end
|
|
|
|
|
2022-06-01 14:09:44 -04:00
|
|
|
def get_type(type_name, context = GraphQL::Query::NullContext)
|
2021-06-15 17:10:04 -04:00
|
|
|
type_name = Gitlab::GlobalId::Deprecations.apply_to_graphql_name(type_name)
|
2022-07-28 14:09:03 -04:00
|
|
|
type_name = Gitlab::Graphql::TypeNameDeprecations.apply_to_graphql_name(type_name)
|
2021-06-15 17:10:04 -04:00
|
|
|
|
2022-06-01 14:09:44 -04:00
|
|
|
super(type_name, context)
|
2020-08-13 23:10:00 -04:00
|
|
|
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
|
|
|
|
|
2020-01-08 16:08:08 -05:00
|
|
|
# Find an object by looking it up from its global ID, passed as a string.
|
|
|
|
#
|
|
|
|
# This is the composition of 'parse_gid' and 'find_by_gid', see these
|
|
|
|
# methods for further documentation.
|
2019-12-23 07:08:18 -05:00
|
|
|
def object_from_id(global_id, ctx = {})
|
2020-01-08 16:08:08 -05:00
|
|
|
gid = parse_gid(global_id, ctx)
|
|
|
|
|
|
|
|
find_by_gid(gid)
|
|
|
|
end
|
|
|
|
|
2020-11-06 19:08:58 -05:00
|
|
|
def resolve_type(type, object, ctx = :__undefined__)
|
2022-06-01 14:09:44 -04:00
|
|
|
return if type.respond_to?(:assignable?) && !type.assignable?(object)
|
2020-11-06 19:08:58 -05:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-01-08 16:08:08 -05:00
|
|
|
# Find an object by looking it up from its 'GlobalID'.
|
|
|
|
#
|
|
|
|
# * For `ApplicationRecord`s, this is equivalent to
|
|
|
|
# `global_id.model_class.find(gid.model_id)`, but more efficient.
|
|
|
|
# * For classes that implement `.lazy_find(global_id)`, this class method
|
|
|
|
# will be called.
|
|
|
|
# * All other classes will use `GlobalID#find`
|
|
|
|
def find_by_gid(gid)
|
2020-08-07 17:10:07 -04:00
|
|
|
return unless gid
|
|
|
|
|
2020-01-08 16:08:08 -05:00
|
|
|
if gid.model_class < ApplicationRecord
|
|
|
|
Gitlab::Graphql::Loaders::BatchModelLoader.new(gid.model_class, gid.model_id).find
|
|
|
|
elsif gid.model_class.respond_to?(:lazy_find)
|
|
|
|
gid.model_class.lazy_find(gid.model_id)
|
|
|
|
else
|
|
|
|
gid.find
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parse a string to a GlobalID, raising ArgumentError if there are problems
|
|
|
|
# with it.
|
|
|
|
#
|
|
|
|
# Problems that may occur:
|
|
|
|
# * it may not be syntactically valid
|
|
|
|
# * it may not match the expected type (see below)
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
# * :expected_type [Class] - the type of object this GlobalID should refer to.
|
2021-04-19 17:09:27 -04:00
|
|
|
# * :expected_type [[Class]] - array of the types of object this GlobalID should refer to.
|
2020-01-08 16:08:08 -05:00
|
|
|
#
|
|
|
|
# e.g.
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# gid = GitlabSchema.parse_gid(my_string, expected_type: ::Project)
|
|
|
|
# project_id = gid.model_id
|
|
|
|
# gid.model_class == ::Project
|
|
|
|
# ```
|
|
|
|
def parse_gid(global_id, ctx = {})
|
2021-04-19 17:09:27 -04:00
|
|
|
expected_types = Array(ctx[:expected_type])
|
2019-06-03 13:38:16 -04:00
|
|
|
gid = GlobalID.parse(global_id)
|
|
|
|
|
2020-08-20 20:10:44 -04:00
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, "#{global_id} is not a valid GitLab ID." unless gid
|
2019-06-03 13:38:16 -04:00
|
|
|
|
2021-04-19 17:09:27 -04:00
|
|
|
if expected_types.any? && expected_types.none? { |type| gid.model_class.ancestors.include?(type) }
|
|
|
|
vars = { global_id: global_id, expected_types: expected_types.join(', ') }
|
|
|
|
msg = _('%{global_id} is not a valid ID for %{expected_types}.') % vars
|
2019-12-23 07:08:18 -05:00
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, msg
|
|
|
|
end
|
|
|
|
|
2020-01-08 16:08:08 -05:00
|
|
|
gid
|
2019-06-03 13:38:16 -04:00
|
|
|
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
|
2021-08-08 23:10:10 -04:00
|
|
|
ADMIN_MAX_COMPLEXITY
|
2019-05-06 10:00:03 -04:00
|
|
|
elsif current_user
|
2021-08-08 23:10:10 -04:00
|
|
|
AUTHENTICATED_MAX_COMPLEXITY
|
2019-05-06 10:00:03 -04:00
|
|
|
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
|
2020-08-13 23:10:00 -04:00
|
|
|
|
|
|
|
def get_type(type_name)
|
2021-06-15 17:10:04 -04:00
|
|
|
type_name = Gitlab::GlobalId::Deprecations.apply_to_graphql_name(type_name)
|
2022-07-28 14:09:03 -04:00
|
|
|
type_name = Gitlab::Graphql::TypeNameDeprecations.apply_to_graphql_name(type_name)
|
2021-06-15 17:10:04 -04:00
|
|
|
|
2020-08-13 23:10:00 -04:00
|
|
|
super(type_name)
|
|
|
|
end
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
2020-03-10 11:08:08 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
GitlabSchema.prepend_mod_with('GitlabSchema') # rubocop: disable Cop/InjectEnterpriseEditionModule
|