Sha256: c20f1990eaeee2c51ab6a175e865ec94639eb44846a8194d8d2936595a7464df

Contents?: true

Size: 1.06 KB

Versions: 20

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

20 entries across 20 versions & 1 rubygems

Version Path
graphql-0.19.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.19.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.19.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.19.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.15 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.14 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.11 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.10 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.9 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.8 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.7 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.6 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.5 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-0.18.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb