forked from rmosolgo/graphql-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.rb
More file actions
95 lines (79 loc) · 2.44 KB
/
list.rb
File metadata and controls
95 lines (79 loc) · 2.44 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true
module GraphQL
class Schema
# Represents a list type in the schema.
# Wraps a {Schema::Member} as a list type.
# @see {Schema::Member::TypeSystemHelpers#to_list_type}
class List < GraphQL::Schema::Wrapper
include Schema::Member::ValidatesInput
def initialize(of_type, skip_nodes_on_raise: false)
super(of_type)
@skip_nodes_on_raise = skip_nodes_on_raise
end
# @return [GraphQL::TypeKinds::LIST]
def kind
GraphQL::TypeKinds::LIST
end
# @return [true]
def list?
true
end
def skip_nodes_on_raise?
@skip_nodes_on_raise
end
def to_type_signature
"[#{@of_type.to_type_signature}]"
end
# This is for introspection, where it's expected the name will be `null`
def graphql_name
nil
end
# Also for implementing introspection
def description
nil
end
def coerce_result(value, ctx)
value.map { |i| i.nil? ? nil : of_type.coerce_result(i, ctx) }
end
def coerce_input(value, ctx)
if value.nil?
nil
else
coerced = ensure_array(value).map { |item| item.nil? ? item : of_type.coerce_input(item, ctx) }
ctx.schema.after_any_lazies(coerced, &:itself)
end
end
def validate_non_null_input(value, ctx, max_errors: nil)
result = GraphQL::Query::InputValidationResult.new
ensure_array(value).each_with_index do |item, index|
item_result = of_type.validate_input(item, ctx)
unless item_result.valid?
if max_errors
if max_errors == 0
add_max_errros_reached_message(result)
break
end
max_errors -= 1
end
result.merge_result!(index, item_result)
end
end
result.valid? ? nil : result
end
private
def ensure_array(value)
# `Array({ a: 1 })` makes `[[:a, 1]]`, so do it manually
if value.is_a?(Array)
value
else
[value]
end
end
def add_max_errros_reached_message(result)
message = "Too many errors processing list variable, max validation error limit reached. Execution aborted"
item_result = GraphQL::Query::InputValidationResult.from_problem(message)
result.merge_result!(nil, item_result)
end
end
end
end