debian-mirror-gitlab/app/graphql/gitlab_schema.rb

101 lines
2.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
class GitlabSchema < GraphQL::Schema
2019-07-07 11:18:12 +05:30
# Currently an IntrospectionQuery has a complexity of 179.
# These values will evolve over time.
DEFAULT_MAX_COMPLEXITY = 200
AUTHENTICATED_COMPLEXITY = 250
ADMIN_COMPLEXITY = 300
2019-09-04 21:01:54 +05:30
DEFAULT_MAX_DEPTH = 15
AUTHENTICATED_MAX_DEPTH = 20
2018-11-08 19:23:39 +05:30
use BatchLoader::GraphQL
use Gitlab::Graphql::Authorize
use Gitlab::Graphql::Present
2019-09-30 21:07:59 +05:30
use Gitlab::Graphql::CallsGitaly
2018-11-08 19:23:39 +05:30
use Gitlab::Graphql::Connections
2019-07-31 22:56:46 +05:30
use Gitlab::Graphql::GenericTracing
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new
2019-10-31 01:37:42 +05:30
query_analyzer Gitlab::Graphql::QueryAnalyzers::RecursionAnalyzer.new
2019-07-07 11:18:12 +05:30
max_complexity DEFAULT_MAX_COMPLEXITY
2019-09-04 21:01:54 +05:30
max_depth DEFAULT_MAX_DEPTH
2019-07-07 11:18:12 +05:30
2019-10-31 01:37:42 +05:30
query Types::QueryType
mutation Types::MutationType
default_max_page_size 100
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
class << self
def multiplex(queries, **kwargs)
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context])
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
queries.each do |query|
query[:max_depth] = max_query_depth(kwargs[:context])
end
super(queries, **kwargs)
end
def execute(query_str = nil, **kwargs)
kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context])
kwargs[:max_depth] ||= max_query_depth(kwargs[:context])
super(query_str, **kwargs)
end
2019-12-26 22:10:19 +05:30
def id_from_object(object, _type = nil, _ctx = nil)
2019-09-04 21:01:54 +05:30
unless object.respond_to?(:to_global_id)
# This is an error in our schema and needs to be solved. So raise a
2019-12-04 20:38:33 +05:30
# more meaningful error message
2019-09-04 21:01:54 +05:30
raise "#{object} does not implement `to_global_id`. "\
"Include `GlobalID::Identification` into `#{object.class}"
end
object.to_global_id
end
2019-12-26 22:10:19 +05:30
def object_from_id(global_id, _ctx = nil)
2019-09-04 21:01:54 +05:30
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-09-30 21:07:59 +05:30
elsif gid.model_class.respond_to?(:lazy_find)
gid.model_class.lazy_find(gid.model_id)
2019-09-04 21:01:54 +05:30
else
gid.find
end
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-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
def max_query_depth(ctx)
current_user = ctx&.fetch(:current_user, nil)
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
if current_user
AUTHENTICATED_MAX_DEPTH
else
DEFAULT_MAX_DEPTH
end
2019-07-07 11:18:12 +05:30
end
end
2018-11-08 19:23:39 +05:30
end