Sha256: 8ef025f71904ec8e349509854afc97e7b12a834beec6655f570acbb9e18a34e4

Contents?: true

Size: 967 Bytes

Versions: 8

Compression:

Stored size: 967 Bytes

Contents

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

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

      def validate_default_value(node, 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 !context.valid_literal?(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

8 entries across 8 versions & 1 rubygems

Version Path
graphql-1.2.6 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.2.5 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.2.4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.2.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.2.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.2.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.2.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.1.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb