Class: Types::BaseField

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

Constant Summary collapse

DEFAULT_COMPLEXITY =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

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
32
33
# File 'app/graphql/types/base_field.rb', line 13

def initialize(**kwargs, &block)
  @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])
  @feature_flag = kwargs[:feature_flag]
  kwargs = check_feature_flag(kwargs)
  @deprecation = gitlab_deprecation(kwargs)
  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

#deprecationObject (readonly)

Returns the value of attribute deprecation.


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

def deprecation
  @deprecation
end

#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)

52
53
54
# File 'app/graphql/types/base_field.rb', line 52

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

#base_complexityObject


56
57
58
59
60
# File 'app/graphql/types/base_field.rb', line 56

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

#calls_gitaly?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'app/graphql/types/base_field.rb', line 62

def calls_gitaly?
  @calls_gitaly
end

#constant_complexity?Boolean

Returns:

  • (Boolean)

66
67
68
# File 'app/graphql/types/base_field.rb', line 66

def constant_complexity?
  @constant_complexity
end

#may_call_gitaly?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'app/graphql/types/base_field.rb', line 35

def may_call_gitaly?
  @constant_complexity || @calls_gitaly
end

#requires_argument?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'app/graphql/types/base_field.rb', line 39

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

#visible?(context) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
73
74
# File 'app/graphql/types/base_field.rb', line 70

def visible?(context)
  return false if feature_flag.present? && !Feature.enabled?(feature_flag)

  super
end