Module: GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTyped

Defined in:
lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb

Instance Method Summary collapse

Instance Method Details

#on_variable_definition(node, parent) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb', line 5

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