Sha256: 2bbd545286146774eb264d27cd40a6066d9c71010e9b46a837c9b17b157fc42b

Contents?: true

Size: 1.74 KB

Versions: 57

Compression:

Stored size: 1.74 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["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
        end

        super
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
graphql-2.2.16 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.1.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.9 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.8 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.7 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.6 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.5 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.31 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.2 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.1 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.3.0 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.14 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.13 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.0.29 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.1.12 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.11 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.10 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.9 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb
graphql-2.2.8 lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb