Sha256: cdd8da2b16a491ba5cdefd92e9e1b9ab1a7d47d03b3ea65aa1ce4dfd60cc17a8

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

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

      def validate(context)
        literal_validator = GraphQL::StaticValidation::LiteralValidator.new
        context.visitor[GraphQL::Language::Nodes::VariableDefinition] << ->(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, context: context)
        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 #{type}", node, context: context)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-1.0.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.19.4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb