Module: GraphQL::StaticValidation::VariableUsagesAreAllowed

Defined in:
lib/graphql/static_validation/rules/variable_usages_are_allowed.rb

Instance Method Summary collapse

Instance Method Details

#initializeObject

[View source]

5
6
7
8
9
# File 'lib/graphql/static_validation/rules/variable_usages_are_allowed.rb', line 5

def initialize(*)
  super
  # holds { name => ast_node } pairs
  @declared_variables = {}
end

#on_argument(node, parent) ⇒ Object

[View source]

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphql/static_validation/rules/variable_usages_are_allowed.rb', line 16

def on_argument(node, parent)
  node_values = if node.value.is_a?(Array)
    node.value
  else
    [node.value]
  end
  node_values = node_values.select { |value| value.is_a? GraphQL::Language::Nodes::VariableIdentifier }

  if !node_values.empty?
    argument_owner = case parent
    when GraphQL::Language::Nodes::Field
      context.field_definition
    when GraphQL::Language::Nodes::Directive
      context.directive_definition
    when GraphQL::Language::Nodes::InputObject
      arg_type = context.argument_definition.type.unwrap
      if arg_type.kind.input_object?
        arg_type
      else
        # This is some kind of error
        nil
      end
    else
      raise("Unexpected argument parent: #{parent}")
    end

    node_values.each do |node_value|
      var_defn_ast = @declared_variables[node_value.name]
      # Might be undefined :(
      # VariablesAreUsedAndDefined can't finalize its search until the end of the document.
      var_defn_ast && argument_owner && validate_usage(argument_owner, node, var_defn_ast)
    end
  end
  super
end

#on_operation_definition(node, parent) ⇒ Object

[View source]

11
12
13
14
# File 'lib/graphql/static_validation/rules/variable_usages_are_allowed.rb', line 11

def on_operation_definition(node, parent)
  @declared_variables = node.variables.each_with_object({}) { |var, memo| memo[var.name] = var }
  super
end