Class: GraphQL::Query::InputValidationResult

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/query/input_validation_result.rb

Constant Summary collapse

VALID =
self.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(valid: true, problems: nil) ⇒ InputValidationResult

Returns a new instance of InputValidationResult.



13
14
15
16
# File 'lib/graphql/query/input_validation_result.rb', line 13

def initialize(valid: true, problems: nil)
  @valid = valid
  @problems = problems
end

Instance Attribute Details

#problemsObject

Returns the value of attribute problems.



5
6
7
# File 'lib/graphql/query/input_validation_result.rb', line 5

def problems
  @problems
end

Class Method Details

.from_problem(explanation, path = nil, extensions: nil, message: nil) ⇒ Object



7
8
9
10
11
# File 'lib/graphql/query/input_validation_result.rb', line 7

def self.from_problem(explanation, path = nil, extensions: nil, message: nil)
  result = self.new
  result.add_problem(explanation, path, extensions: extensions, message: message)
  result
end

Instance Method Details

#add_problem(explanation, path = nil, extensions: nil, message: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql/query/input_validation_result.rb', line 22

def add_problem(explanation, path = nil, extensions: nil, message: nil)
  @problems ||= []
  @valid = false
  problem = { "path" => path || [], "explanation" => explanation }
  if extensions
    problem["extensions"] = extensions
  end
  if message
    problem["message"] = message
  end
  @problems.push(problem)
end

#merge_result!(path, inner_result) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphql/query/input_validation_result.rb', line 35

def merge_result!(path, inner_result)
  return if inner_result.nil? || inner_result.valid?

  if inner_result.problems
    inner_result.problems.each do |p|
      item_path = [path, *p["path"]]
      add_problem(p["explanation"], item_path, message: p["message"], extensions: p["extensions"])
    end
  end
  # It could have been explicitly set on inner_result (if it had no problems)
  @valid = false
end

#valid?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/graphql/query/input_validation_result.rb', line 18

def valid?
  @valid
end