Sha256: f4b433e33ea0fccf60fefcda52c672bff54c0d5f6987ad3185145b801ff3895b

Contents?: true

Size: 997 Bytes

Versions: 8

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true
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.5.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.4.5 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.4.4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.4.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.4.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.4.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.4.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.3.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb