Sha256: b49b9d6eb6f7ffa854b2b88979866c277c664ff896215090241e0856cd263ae7

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

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

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

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

    context.visitor[GraphQL::Language::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::Language::Visitor::SKIP
      end
    }

    context.visitor[GraphQL::Language::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

8 entries across 8 versions & 1 rubygems

Version Path
graphql-0.9.2 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.8.1 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.8.0 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.7.1 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.7.0 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.6.2 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.6.1 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb
graphql-0.6.0 lib/graphql/static_validation/rules/variables_are_used_and_defined.rb