Module: GraphQL::StaticValidation::VariablesAreUsedAndDefined
- Defined in:
- lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
Overview
The problem is
- Variable $usage must be determined at the OperationDefinition level
- You can't tell how fragments use variables until you visit FragmentDefinitions (which may be at the end of the document)
So, this validator includes some crazy logic to follow fragment spreads recursively, while avoiding infinite loops.
graphql-js solves this problem by:
- re-visiting the AST for each validator
- allowing validators to say `followSpreads: true`
-- @requires_ancestor: GraphQL::StaticValidation::BaseVisitor
Defined Under Namespace
Classes: VariableUsage
Instance Method Summary collapse
- #initialize ⇒ Object
- #on_document(node, parent) ⇒ Object
- #on_fragment_definition(node, parent) ⇒ Object
-
#on_fragment_spread(node, parent) ⇒ Object
For FragmentSpreads: - find the context on the stack - mark the context as containing this spread.
- #on_operation_definition(node, parent) ⇒ Object
-
#on_variable_identifier(node, parent) ⇒ Object
For VariableIdentifiers: - mark the variable as used - assign its AST node.
Instance Method Details
#initialize ⇒ Object
30 31 32 33 34 35 |
# File 'lib/graphql/static_validation/rules/variables_are_used_and_defined.rb', line 30 def initialize(*) super @variable_usages_for_context = Hash.new {|hash, key| hash[key] = Hash.new {|h, k| h[k] = VariableUsage.new } } @spreads_for_context = Hash.new {|hash, key| hash[key] = [] } @variable_context_stack = [] end |
#on_document(node, parent) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/graphql/static_validation/rules/variables_are_used_and_defined.rb', line 82 def on_document(node, parent) super fragment_definitions = @variable_usages_for_context.select { |key, value| key.is_a?(GraphQL::Language::Nodes::FragmentDefinition) } operation_definitions = @variable_usages_for_context.select { |key, value| key.is_a?(GraphQL::Language::Nodes::OperationDefinition) } operation_definitions.each do |node, node_variables| follow_spreads(node, node_variables, @spreads_for_context, fragment_definitions, []) create_errors(node_variables) end end |
#on_fragment_definition(node, parent) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/graphql/static_validation/rules/variables_are_used_and_defined.rb', line 52 def on_fragment_definition(node, parent) # initialize the hash of vars for this context: @variable_usages_for_context[node] @variable_context_stack.push(node) super @variable_context_stack.pop end |
#on_fragment_spread(node, parent) ⇒ Object
For FragmentSpreads:
- find the context on the stack
- mark the context as containing this spread
63 64 65 66 67 |
# File 'lib/graphql/static_validation/rules/variables_are_used_and_defined.rb', line 63 def on_fragment_spread(node, parent) variable_context = @variable_context_stack.last @spreads_for_context[variable_context] << node.name super end |
#on_operation_definition(node, parent) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/static_validation/rules/variables_are_used_and_defined.rb', line 37 def on_operation_definition(node, parent) # initialize the hash of vars for this context: @variable_usages_for_context[node] @variable_context_stack.push(node) # mark variables as defined: var_hash = @variable_usages_for_context[node] node.variables.each { |var| var_usage = var_hash[var.name] var_usage.declared_by = node var_usage.path = context.path } super @variable_context_stack.pop end |
#on_variable_identifier(node, parent) ⇒ Object
For VariableIdentifiers:
- mark the variable as used
- assign its AST node
72 73 74 75 76 77 78 79 80 |
# File 'lib/graphql/static_validation/rules/variables_are_used_and_defined.rb', line 72 def on_variable_identifier(node, parent) usage_context = @variable_context_stack.last declared_variables = @variable_usages_for_context[usage_context] usage = declared_variables[node.name] usage.used_by = usage_context usage.ast_node = node usage.path = context.path super end |