Sha256: 91545e35d9b3fc66bf244d4bc265868fa5201c8b498b5841d87ad84438bfc80d

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
module GraphQL
  module StaticValidation
    module VariableDefaultValuesAreCorrectlyTyped
      def on_variable_definition(node, parent)
        if !node.default_value.nil?
          value = node.default_value
          type = context.schema.type_from_ast(node.type, context: context)
          if type.nil?
            # This is handled by another validator
          else
            validation_result = context.validate_literal(value, type)

            if !validation_result.valid?
              problems = validation_result.problems
              first_problem = problems && problems.first
              if first_problem
                error_message = first_problem["explanation"]
              end

              error_message ||= "Default value for $#{node.name} doesn't match type #{type.to_type_signature}"
              add_error(GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTypedError.new(
                error_message,
                nodes: node,
                name: node.name,
                type: type.to_type_signature,
                error_type: VariableDefaultValuesAreCorrectlyTypedError::VIOLATIONS[:INVALID_TYPE],
              ))
            end
          end
        end

        super
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphql-2.4.4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.4.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.4.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.4.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.4.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.20 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.19 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.18 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.17 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.16 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.15 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.14 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.11 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.10 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb