Sha256: 4a4308e55b6f79c8ac68c66ce9127edb29b0fbd4fca716faa52b3d2625d14ee5

Contents?: true

Size: 1.73 KB

Versions: 88

Compression:

Stored size: 1.73 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
          if node.type.is_a?(GraphQL::Language::Nodes::NonNullType)
            add_error(GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTypedError.new(
              "Non-null variable $#{node.name} can't have a default value",
              nodes: node,
              name: node.name,
              error_type: VariableDefaultValuesAreCorrectlyTypedError::VIOLATIONS[:INVALID_ON_NON_NULL]
            ))
          else
            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["message"]
                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
        end

        super
      end
    end
  end
end

Version data entries

88 entries across 88 versions & 2 rubygems

Version Path
graphql-1.11.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.11.11 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.12.25 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.24 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.23 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.22 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.21 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.20 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.19 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.18 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.17 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.16 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.15 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.11 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.14 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.13.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.9 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.8 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb