File: variable_validation_error.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 1,365 bytes parent folder | download
1
2
3
4
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
# frozen_string_literal: true
module GraphQL
  class Query
    class VariableValidationError < GraphQL::ExecutionError
      attr_accessor :value, :validation_result

      def initialize(variable_ast, type, value, validation_result, msg: nil)
        @value = value
        @validation_result = validation_result

        msg ||= "Variable $#{variable_ast.name} of type #{type.to_type_signature} was provided invalid value"

        if problem_fields.any?
          msg += " for #{problem_fields.join(", ")}"
        end

        super(msg)
        self.ast_node = variable_ast
      end

      def to_h
        # It is possible there are other extension items in this error, so handle
        # a one level deep merge explicitly. However beyond that only show the
        # latest value and problems.
        super.merge({ "extensions" => { "value" => value, "problems" => validation_result.problems }}) do |key, oldValue, newValue|
          if oldValue.respond_to?(:merge)
            oldValue.merge(newValue)
          else
            newValue
          end
        end
      end

      private

      def problem_fields
        @problem_fields ||= @validation_result
          .problems
          .reject { |problem| problem["path"].empty? }
          .map { |problem| "#{problem['path'].join('.')} (#{problem['explanation']})" }
      end
    end
  end
end