Class: Types::BaseField

Inherits:
GraphQL::Schema::Field
  • Object
show all
Includes:
Gitlab::Graphql::Deprecations
Defined in:
app/graphql/types/base_field.rb

Direct Known Subclasses

Ci::JobBaseField

Constant Summary collapse

DEFAULT_COMPLEXITY =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Graphql::Deprecations

#visible?

Constructor Details

#initialize(**kwargs, &block) ⇒ BaseField

Returns a new instance of BaseField.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/graphql/types/base_field.rb', line 13

def initialize(**kwargs, &block)
  init_gitlab_deprecation(kwargs)
  @calls_gitaly = !!kwargs.delete(:calls_gitaly)
  @doc_reference = kwargs.delete(:see)
  @constant_complexity = kwargs[:complexity].is_a?(Integer) && kwargs[:complexity] > 0
  @requires_argument = !!kwargs.delete(:requires_argument)
  @authorize = Array.wrap(kwargs.delete(:authorize))
  kwargs[:complexity] = field_complexity(kwargs[:resolver_class], kwargs[:complexity])
  after_connection_extensions = kwargs.delete(:late_extensions) || []

  super(**kwargs, &block)

  # We want to avoid the overhead of this in prod
  extension ::Gitlab::Graphql::CallsGitaly::FieldExtension if Gitlab.dev_or_test_env?
  extension ::Gitlab::Graphql::Present::FieldExtension
  extension ::Gitlab::Graphql::Authorize::ConnectionFilterExtension

  after_connection_extensions.each { extension _1 } if after_connection_extensions.any?
end

Instance Attribute Details

#doc_referenceObject (readonly)

Returns the value of attribute doc_reference.



11
12
13
# File 'app/graphql/types/base_field.rb', line 11

def doc_reference
  @doc_reference
end

Instance Method Details

#authorized?(object, args, ctx) ⇒ Boolean

By default fields authorize against the current object, but that is not how our resolvers work - they use declarative permissions to authorize fields manually (so we make them opt in). TODO: gitlab.com/gitlab-org/gitlab/-/issues/300922

(separate out authorize into permissions on the object, and on the
resolved values)

We do not support argument authorization in our schema. If/when we do, we should call ‘super` here, to apply argument authorization checks. See: gitlab.com/gitlab-org/gitlab/-/issues/324647

Returns:

  • (Boolean)


50
51
52
# File 'app/graphql/types/base_field.rb', line 50

def authorized?(object, args, ctx)
  field_authorized?(object, ctx) && resolver_authorized?(object, ctx)
end

#base_complexityObject



78
79
80
81
82
# File 'app/graphql/types/base_field.rb', line 78

def base_complexity
  complexity = DEFAULT_COMPLEXITY
  complexity += 1 if calls_gitaly?
  complexity
end

#calls_gitaly?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/graphql/types/base_field.rb', line 84

def calls_gitaly?
  @calls_gitaly
end

#complexity_for(child_complexity:, query:, lookahead:) ⇒ Object

This gets called from the gem’s ‘calculate_complexity` method, allowing us to ensure our complexity calculation is used even for connections. This code is actually a copy of the default case in `calculate_complexity` in `lib/graphql/schema/field.rb` (github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/schema/field.rb)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/graphql/types/base_field.rb', line 59

def complexity_for(child_complexity:, query:, lookahead:)
  defined_complexity = complexity

  case defined_complexity
  when Proc
    arguments = query.arguments_for(lookahead.ast_nodes.first, self)

    if arguments.respond_to?(:keyword_arguments)
      defined_complexity.call(query.context, arguments.keyword_arguments, child_complexity)
    else
      child_complexity
    end
  when Numeric
    defined_complexity + child_complexity
  else
    raise("Invalid complexity: #{defined_complexity.inspect} on #{path} (#{inspect})")
  end
end

#constant_complexity?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/graphql/types/base_field.rb', line 88

def constant_complexity?
  @constant_complexity
end

#may_call_gitaly?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/graphql/types/base_field.rb', line 33

def may_call_gitaly?
  @constant_complexity || @calls_gitaly
end

#requires_argument?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/graphql/types/base_field.rb', line 37

def requires_argument?
  @requires_argument || arguments.values.any? { |argument| argument.type.non_null? }
end