Sha256: 30080d7b9d713a417137392bf469ca79092ecbbec5c862f5af203955c48a9f69

Contents?: true

Size: 938 Bytes

Versions: 1

Compression:

Stored size: 938 Bytes

Contents

class GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTyped
  include GraphQL::StaticValidation::Message::MessageHelper

  def validate(context)
    literal_validator = GraphQL::StaticValidation::LiteralValidator.new
    context.visitor[GraphQL::Language::Nodes::Variable] << -> (node, parent) {
      if !node.default_value.nil?
        validate_default_value(node, literal_validator, context)
      end
    }
  end

  def validate_default_value(node, literal_validator, context)
    value = node.default_value
    if node.type.is_a?(GraphQL::Language::Nodes::NonNullType)
      context.errors << message("Non-null variable $#{node.name} can't have a default value", node)
    else
      type = context.schema.type_from_ast(node.type)
      if !literal_validator.validate(value, type)
        context.errors << message("Default value for $#{node.name} doesn't match type #{node.type.name}", node)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-0.10.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb