Sha256: 48a16dd6871c153ae7abd60d1e56c7c20fb788537d4ff963ed2ca9ab72611016

Contents?: true

Size: 1.03 KB

Versions: 13

Compression:

Stored size: 1.03 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)
        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)
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
graphql-0.17.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.17.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.17.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.16.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.16.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.15.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.15.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.14.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.15.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.15.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.14.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.14.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.13.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb