Sha256: 4b39bcccfdecd7f196d82c9213362dac86e6eedd5cf314d9303b0a576d7fa52e

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

class GraphQL::StaticValidation::VariablesAreUsedAndDefined
  include GraphQL::StaticValidation::Message::MessageHelper

  def validate(context)
    # holds { name => used? } pairs
    declared_variables = {}

    context.visitor[GraphQL::Nodes::OperationDefinition] << -> (node, parent) {
      declared_variables = node.variables.each_with_object({}) { |var, memo| memo[var.name] = false }
    }

    context.visitor[GraphQL::Nodes::VariableIdentifier] << -> (node, parent) {
      if declared_variables.key?(node.name)
        declared_variables[node.name] = true
      else
        context.errors << message("Variable $#{node.name} is used but not declared", node)
        GraphQL::Visitor::SKIP
      end
    }

    context.visitor[GraphQL::Nodes::OperationDefinition].leave << -> (node, parent) {
      unused_variables = declared_variables
        .select { |name, used| !used }
        .keys

      unused_variables.each do |var_name|
        context.errors << message("Variable $#{var_name} is declared but not used", node)
      end
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-0.5.0 lib/graph_ql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.4.0 lib/graph_ql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.3.0 lib/graph_ql/static_validation/rules/variables_are_used_and_defined.rb