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

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

Constant Summary collapse

INLINE_FRAGMENT_NO_TYPE =
"..."

Instance Method Summary collapse

Instance Method Details

#argument_definitionGraphQL::Argument?

Returns The most-recently-entered GraphQL::Argument, if currently inside one.

Returns:

  • (GraphQL::Argument, nil)

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



204
205
206
207
# File 'lib/graphql/static_validation/base_visitor.rb', line 204

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

#directive_definitionGraphQL::Directive?

Returns The most-recently-entered GraphQL::Directive, if currently inside one.

Returns:

  • (GraphQL::Directive, nil)

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



199
200
201
# File 'lib/graphql/static_validation/base_visitor.rb', line 199

def directive_definition
  @current_directive_definition
end

#field_definitionGraphQL::Field?

Returns The most-recently-entered GraphQL::Field, if currently inside one.

Returns:

  • (GraphQL::Field, nil)

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



194
195
196
# File 'lib/graphql/static_validation/base_visitor.rb', line 194

def field_definition
  @current_field_definition
end

#on_argument(node, parent) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/graphql/static_validation/base_visitor.rb', line 137

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
  @current_argument_definition = argument_defn
  @path[@path_depth] = node.name
  @path_depth += 1
  super
  @current_argument_definition = @parent_argument_definition
  @parent_argument_definition = prev_parent
  @path_depth -= 1
end

#on_directive(node, parent) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/graphql/static_validation/base_visitor.rb', line 129

def on_directive(node, parent)
  directive_defn = @context.schema_directives[node.name]
  prev_directive_definition = @current_directive_definition
  @current_directive_definition = directive_defn
  super
  @current_directive_definition = prev_directive_definition
end

#on_field(node, parent) ⇒ Object



108
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 108

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
  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[@path_depth] = node.alias || node.name
  @path_depth += 1
  super
  @current_field_definition = prev_field_definition
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth -= 1
end

#on_fragment_definition(node, parent) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/graphql/static_validation/base_visitor.rb', line 70

def on_fragment_definition(node, parent)
  object_type = if node.type
    @types.type(node.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[@path_depth] = "fragment #{node.name}"
  @path_depth += 1
  super
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth -= 1
end

#on_fragment_spread(node, parent) ⇒ Object



164
165
166
167
168
169
# File 'lib/graphql/static_validation/base_visitor.rb', line 164

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

#on_inline_fragment(node, parent) ⇒ Object



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

def on_inline_fragment(node, parent)
  if 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 += 1
  else
    object_type = @current_object_type
    @path[@path_depth] = INLINE_FRAGMENT_NO_TYPE
    @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 -= 1
end

#on_input_object(node, parent) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/graphql/static_validation/base_visitor.rb', line 171

def on_input_object(node, parent)
  arg_defn = @current_argument_definition
  if arg_defn && arg_defn.type.list?
    @path[@path_depth] = parent.children.index(node)
    @path_depth += 1
    super
    @path_depth -= 1
  else
    super
  end
end

#on_operation_definition(node, parent) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/graphql/static_validation/base_visitor.rb', line 57

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
  @current_object_type = object_type
  @path[@path_depth] = "#{node.operation_type}#{node.name ? " #{node.name}" : ""}"
  @path_depth += 1
  super
  @current_object_type = @parent_object_type
  @parent_object_type = prev_parent_ot
  @path_depth -= 1
end

#parent_type_definitionGraphQL::BaseType

Returns The type which the current type came from.

Returns:

  • (GraphQL::BaseType)

    The type which the current type came from



189
190
191
# File 'lib/graphql/static_validation/base_visitor.rb', line 189

def parent_type_definition
  @parent_object_type
end

#type_definitionGraphQL::BaseType

Returns The current object type.

Returns:

  • (GraphQL::BaseType)

    The current object type



184
185
186
# File 'lib/graphql/static_validation/base_visitor.rb', line 184

def type_definition
  @current_object_type
end