gitlab_schema.rb 2.4 KB
Newer Older
1 2
# frozen_string_literal: true

3
class GitlabSchema < GraphQL::Schema
B
Brett Walker 已提交
4
  # Currently an IntrospectionQuery has a complexity of 179.
5
  # These values will evolve over time.
B
Brett Walker 已提交
6 7 8
  DEFAULT_MAX_COMPLEXITY   = 200
  AUTHENTICATED_COMPLEXITY = 250
  ADMIN_COMPLEXITY         = 300
9

P
Phil Hughes 已提交
10
  DEFAULT_MAX_DEPTH = 10
K
Ken Ding 已提交
11 12
  AUTHENTICATED_MAX_DEPTH = 15

13
  use BatchLoader::GraphQL
14 15
  use Gitlab::Graphql::Authorize
  use Gitlab::Graphql::Present
B
Bob Van Landuyt 已提交
16
  use Gitlab::Graphql::Connections
17
  use Gitlab::Graphql::GenericTracing
N
Nick Thomas 已提交
18

C
charlieablett 已提交
19
  query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new
20

N
Nick Thomas 已提交
21
  query(Types::QueryType)
B
Bob Van Landuyt 已提交
22 23

  default_max_page_size 100
24 25

  max_complexity DEFAULT_MAX_COMPLEXITY
P
Phil Hughes 已提交
26
  max_depth DEFAULT_MAX_DEPTH
27

28
  mutation(Types::MutationType)
29

K
Ken Ding 已提交
30
  class << self
P
Phil Hughes 已提交
31 32 33 34 35 36 37 38 39 40
    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

K
Ken Ding 已提交
41 42 43
    def execute(query_str = nil, **kwargs)
      kwargs[:max_complexity] ||= max_query_complexity(kwargs[:context])
      kwargs[:max_depth] ||= max_query_depth(kwargs[:context])
44

K
Ken Ding 已提交
45 46 47
      super(query_str, **kwargs)
    end

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    def id_from_object(object)
      unless object.respond_to?(:to_global_id)
        # This is an error in our schema and needs to be solved. So raise a
        # more meaningfull error message
        raise "#{object} does not implement `to_global_id`. "\
              "Include `GlobalID::Identification` into `#{object.class}"
      end

      object.to_global_id
    end

    def object_from_id(global_id)
      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
      else
        gid.find
      end
    end

K
Ken Ding 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
    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
86

K
Ken Ding 已提交
87 88
    def max_query_depth(ctx)
      current_user = ctx&.fetch(:current_user, nil)
89

K
Ken Ding 已提交
90 91 92
      if current_user
        AUTHENTICATED_MAX_DEPTH
      else
P
Phil Hughes 已提交
93
        DEFAULT_MAX_DEPTH
K
Ken Ding 已提交
94
      end
95 96
    end
  end
N
Nick Thomas 已提交
97
end