Module: GraphQL::StaticValidation::BaseVisitor::ContextMethods

Defined in:
lib/graphql/static_validation/base_visitor.rb

Overview

-- @requires_ancestor: GraphQL::StaticValidation::BaseVisitor

Constant Summary collapse

INLINE_FRAGMENT_NO_TYPE =

: String

"..."

Instance Method Summary collapse

Instance Method Details

#argument_definition ⇒ Object

Returns

  • GraphQL::Argument, nil — The most-recently-entered GraphQL::Argument, if currently inside one

:call-seq:

argument_definition() -> GraphQL::Argument | nil

: () -> GraphQL::Schema::Argument?



297
298
299
300
# File 'lib/graphql/static_validation/base_visitor.rb', line 297

def argument_definition
  # Return the parent argument definition (not the current one).
  @parent_argument_definition
end

#child_positions(parent) ⇒ Object

: (GraphQL::Language::Nodes::AbstractNode parent) -> Hash[GraphQL::Language::Nodes::AbstractNode, Integer]



238
239
240
241
242
243
244
# File 'lib/graphql/static_validation/base_visitor.rb', line 238

def child_positions(parent)
  @child_positions[parent] ||= begin
    positions = {}.compare_by_identity #: Hash[GraphQL::Language::Nodes::AbstractNode, Integer]
    parent.children.each_with_index { |child, index| positions[child] ||= index }
    positions
  end
end

#directive_definition ⇒ Object

Returns

  • GraphQL::Directive, nil — The most-recently-entered GraphQL::Directive, if currently inside one

:call-seq:

directive_definition() -> GraphQL::Directive | nil

: () -> singleton(GraphQL::Schema::Directive)?



286
287
288
# File 'lib/graphql/static_validation/base_visitor.rb', line 286

def directive_definition
  @current_directive_definition
end

#field_definition ⇒ Object

Returns

  • GraphQL::Field, nil — The most-recently-entered GraphQL::Field, if currently inside one

:call-seq:

field_definition() -> GraphQL::Field | nil

: () -> GraphQL::Schema::Field?



275
276
277
# File 'lib/graphql/static_validation/base_visitor.rb', line 275

def field_definition
  @current_field_definition
end

#initialize(document, context) ⇒ Object

BaseVisitor#initialize sets these. They're re-assigned here so Sorbet knows their types in this module, because the hooks below use them directly instead of calling the readers on every node. : (GraphQL::Language::Nodes::Document document, GraphQL::StaticValidation::ValidationContext context) -> void



83
84
85
86
87
88
89
90
91
# File 'lib/graphql/static_validation/base_visitor.rb', line 83

def initialize(document, context)
  super
  @path = path_parts #: Array[String | Integer]
  @path_depth = path_depth #: Integer
  @types = types #: GraphQL::Schema::Visibility::Profile | GraphQL::Schema::Warden::VisibilityProfile
  @inline_fragment_paths = inline_fragment_paths #: Hash[String, String]
  @field_unwrapped_types = field_unwrapped_types #: Hash[GraphQL::Schema::Field, GraphQL::Schema::Member::type_definition]
  @child_positions = child_positions_by_parent #: Hash[GraphQL::Language::Nodes::AbstractNode, Hash[GraphQL::Language::Nodes::AbstractNode, Integer]]
end

#on_argument(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::Argument node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/graphql/static_validation/base_visitor.rb', line 185

def on_argument(node, parent)
  argument_defn = if (arg = @current_argument_definition)
    arg_type = arg.type.unwrap
    if arg_type.kind.input_object?
      @types.argument(arg_type, node.name)
    else
      nil
    end
  elsif (directive_defn = @current_directive_definition)
    @types.argument(directive_defn, node.name)
  elsif (field_defn = @current_field_definition)
    @types.argument(field_defn, node.name)
  else
    nil
  end

  prev_parent = @parent_argument_definition
  @parent_argument_definition = @current_argument_definition #: GraphQL::Schema::Argument?
  @current_argument_definition = argument_defn #: GraphQL::Schema::Argument?
  path_depth = @path_depth
  @path[path_depth] = node.name
  @path_depth = path_depth + 1
  super
  @current_argument_definition = @parent_argument_definition
  @parent_argument_definition = prev_parent
  @path_depth = path_depth
  @child_positions.delete(node)
end

#on_directive(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::Directive node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



176
177
178
179
180
181
182
# File 'lib/graphql/static_validation/base_visitor.rb', line 176

def on_directive(node, parent)
  directive_defn = context.schema_directives[node.name]
  prev_directive_definition = @current_directive_definition
  @current_directive_definition = directive_defn #: singleton(GraphQL::Schema::Directive)?
  super
  @current_directive_definition = prev_directive_definition
end

#on_field(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::Field node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/graphql/static_validation/base_visitor.rb', line 153

def on_field(node, parent)
  parent_type = @current_object_type
  field_definition = @types.field(parent_type, node.name)
  prev_field_definition = @current_field_definition
  @current_field_definition = field_definition #: GraphQL::Schema::Field?
  prev_parent_ot = @parent_object_type
  @parent_object_type = @current_object_type
  if field_definition
    @current_object_type = @field_unwrapped_types[field_definition] ||= field_definition.type.unwrap
  else
    @current_object_type = nil
  end
  path_depth = @path_depth
  @path[path_depth] = node.alias || node.name
  @path_depth = path_depth + 1
  super
  @current_field_definition = prev_field_definition
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth = path_depth
end

#on_fragment_definition(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::FragmentDefinition node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/graphql/static_validation/base_visitor.rb', line 109

def on_fragment_definition(node, parent)
  # The type is always present when parsed, but may be missing on a hand-built node:
  fragment_type = node.type #: GraphQL::Language::Nodes::TypeName?
  object_type = if fragment_type
    @types.type(fragment_type.name)
  else
    @current_object_type
  end
  prev_parent_ot = @parent_object_type
  @parent_object_type = @current_object_type
  @current_object_type = object_type
  path_depth = @path_depth
  @path[path_depth] = "fragment #{node.name}"
  @path_depth = path_depth + 1
  super
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth = path_depth
end

#on_fragment_spread(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::FragmentSpread node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



215
216
217
218
219
220
221
# File 'lib/graphql/static_validation/base_visitor.rb', line 215

def on_fragment_spread(node, parent)
  path_depth = @path_depth
  @path[path_depth] = "... #{node.name}"
  @path_depth = path_depth + 1
  super
  @path_depth = path_depth
end

#on_inline_fragment(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::InlineFragment node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/graphql/static_validation/base_visitor.rb', line 132

def on_inline_fragment(node, parent)
  path_depth = @path_depth
  if (node_type = node.type)
    object_type = @types.type(node_type.name)
    @path[path_depth] = @inline_fragment_paths[node_type.name] ||= -"... on #{node_type.to_query_string}"
    @path_depth = path_depth + 1
  else
    object_type = @current_object_type
    @path[path_depth] = INLINE_FRAGMENT_NO_TYPE
    @path_depth = path_depth + 1
  end
  prev_parent_ot = @parent_object_type
  @parent_object_type = @current_object_type
  @current_object_type = object_type
  super
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth = path_depth
end

#on_input_object(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::InputObject node, GraphQL::Language::Nodes::AbstractNode parent) -> void



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/graphql/static_validation/base_visitor.rb', line 224

def on_input_object(node, parent)
  arg_defn = @current_argument_definition
  if arg_defn && arg_defn.type.list?
    path_depth = @path_depth
    @path[path_depth] = child_positions(parent)[node] #: as !nil
    @path_depth = path_depth + 1
    super
    @path_depth = path_depth
  else
    super
  end
end

#on_operation_definition(node, parent) ⇒ Object

: (GraphQL::Language::Nodes::OperationDefinition node, GraphQL::Language::Nodes::AbstractNode? parent) -> void



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/graphql/static_validation/base_visitor.rb', line 94

def on_operation_definition(node, parent)
  object_type = schema.root_type_for_operation(node.operation_type)
  prev_parent_ot = @parent_object_type
  @parent_object_type = @current_object_type #: GraphQL::Schema::Member::type_definition?
  @current_object_type = object_type #: GraphQL::Schema::Member::type_definition?
  path_depth = @path_depth
  @path[path_depth] = "#{node.operation_type}#{node.name ? " #{node.name}" : ""}"
  @path_depth = path_depth + 1
  super
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth = path_depth
end

#parent_type_definition ⇒ Object

Returns

  • GraphQL::BaseType — The type which the current type came from

:call-seq:

parent_type_definition() -> GraphQL::BaseType

: () -> GraphQL::Schema::Member::type_definition?



264
265
266
# File 'lib/graphql/static_validation/base_visitor.rb', line 264

def parent_type_definition
  @parent_object_type
end

#type_definition ⇒ Object

Returns

  • GraphQL::BaseType — The current object type

:call-seq:

type_definition() -> GraphQL::BaseType

: () -> GraphQL::Schema::Member::type_definition?



253
254
255
# File 'lib/graphql/static_validation/base_visitor.rb', line 253

def type_definition
  @current_object_type
end