Sha256: 746597d6669cde12dfa311add6f2da72d362a7624c6cbe465d7bea56b250d344

Contents?: true

Size: 1.76 KB

Versions: 25

Compression:

Stored size: 1.76 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)
            if type.nil?
              # This is handled by another validator
            else
              begin
                valid = context.valid_literal?(value, type)
              rescue GraphQL::CoercionError => err
                error_message = err.message
              rescue GraphQL::LiteralValidationError
                # noop, we just want to stop any LiteralValidationError from propagating
              end

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

        super
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
graphql-1.9.21 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.20 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.19 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.18 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.17 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.16 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.15 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.14 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.11 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.10 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.9 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.8 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.7 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.6 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.5 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.9.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb