Sha256: bd7cf67020aa4631675953b515f7faec3fa88b39b634175fe0866b26c11bbb9e

Contents?: true

Size: 1.81 KB

Versions: 7

Compression:

Stored size: 1.81 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
              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.to_type_signature}"
                VariableDefaultValuesAreCorrectlyTypedError
                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

7 entries across 7 versions & 1 rubygems

Version Path
graphql-1.10.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.10.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.10.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.10.0.pre4 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.10.0.pre3 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.10.0.pre2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-1.10.0.pre1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb