forked from rmosolgo/graphql-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addition.rb
245 lines (231 loc) · 9.43 KB
/
addition.rb
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# frozen_string_literal: true
module GraphQL
class Schema
class Addition
attr_reader :directives, :possible_types, :types, :union_memberships, :references, :arguments_with_default_values
def initialize(schema:, own_types:, new_types:)
@schema = schema
@own_types = own_types
@directives = Set.new
@possible_types = {}
@types = {}
@union_memberships = {}
@references = Hash.new { |h, k| h[k] = [] }
@arguments_with_default_values = []
add_type_and_traverse(new_types)
end
private
def references_to(thing, from:)
@references[thing] << from
end
def get_type(name)
local_type = @types[name]
# This isn't really sophisticated, but
# I think it's good enough to support the current usage of LateBoundTypes
if local_type.is_a?(Array)
local_type = local_type.first
end
local_type || @schema.get_type(name)
end
# Lookup using `own_types` here because it's ok to override
# inherited types by name
def get_local_type(name)
@types[name] || @own_types[name]
end
def add_directives_from(owner)
dirs = owner.directives.map(&:class)
@directives.merge(dirs)
add_type_and_traverse(dirs)
end
def add_type_and_traverse(new_types)
late_types = []
new_types.each { |t| add_type(t, owner: nil, late_types: late_types, path: [t.graphql_name]) }
missed_late_types = 0
while (late_type_vals = late_types.shift)
type_owner, lt = late_type_vals
if lt.is_a?(String)
type = Member::BuildType.constantize(lt)
# Reset the counter, since we might succeed next go-round
missed_late_types = 0
update_type_owner(type_owner, type)
add_type(type, owner: type_owner, late_types: late_types, path: [type.graphql_name])
elsif lt.is_a?(LateBoundType)
if (type = get_type(lt.name))
# Reset the counter, since we might succeed next go-round
missed_late_types = 0
update_type_owner(type_owner, type)
add_type(type, owner: type_owner, late_types: late_types, path: [type.graphql_name])
else
missed_late_types += 1
# Add it back to the list, maybe we'll be able to resolve it later.
late_types << [type_owner, lt]
if missed_late_types == late_types.size
# We've looked at all of them and haven't resolved one.
raise UnresolvedLateBoundTypeError.new(type: lt)
else
# Try the next one
end
end
else
raise ArgumentError, "Unexpected late type: #{lt.inspect}"
end
end
nil
end
def update_type_owner(owner, type)
case owner
when Module
if owner.kind.union?
# It's a union with possible_types
# Replace the item by class name
owner.assign_type_membership_object_type(type)
@possible_types[owner.graphql_name] = owner.possible_types
elsif type.kind.interface? && (owner.kind.object? || owner.kind.interface?)
new_interfaces = []
owner.interfaces.each do |int_t|
if int_t.is_a?(String) && int_t == type.graphql_name
new_interfaces << type
elsif int_t.is_a?(LateBoundType) && int_t.graphql_name == type.graphql_name
new_interfaces << type
else
# Don't re-add proper interface definitions,
# they were probably already added, maybe with options.
end
end
owner.implements(*new_interfaces)
new_interfaces.each do |int|
pt = @possible_types[int.graphql_name] ||= []
if !pt.include?(owner) && owner.is_a?(Class)
pt << owner
end
int.interfaces.each do |indirect_int|
if indirect_int.is_a?(LateBoundType) && (indirect_int_type = get_type(indirect_int.graphql_name))
update_type_owner(owner, indirect_int_type)
end
end
end
end
when nil
# It's a root type
@types[type.graphql_name] = type
when GraphQL::Schema::Field, GraphQL::Schema::Argument
orig_type = owner.type
# Apply list/non-null wrapper as needed
if orig_type.respond_to?(:of_type)
transforms = []
while (orig_type.respond_to?(:of_type))
if orig_type.kind.non_null?
transforms << :to_non_null_type
elsif orig_type.kind.list?
transforms << :to_list_type
else
raise "Invariant: :of_type isn't non-null or list"
end
orig_type = orig_type.of_type
end
transforms.reverse_each { |t| type = type.public_send(t) }
end
owner.type = type
else
raise "Unexpected update: #{owner.inspect} #{type.inspect}"
end
end
def add_type(type, owner:, late_types:, path:)
if type.is_a?(String) || type.is_a?(GraphQL::Schema::LateBoundType)
late_types << [owner, type]
return
end
if owner.is_a?(Class) && owner < GraphQL::Schema::Union
um = @union_memberships[type.graphql_name] ||= []
um << owner
end
if (prev_type = get_local_type(type.graphql_name)) && (prev_type == type || (prev_type.is_a?(Array) && prev_type.include?(type)))
# No need to re-visit
elsif type.is_a?(Class) && type < GraphQL::Schema::Directive
@directives << type
type.all_argument_definitions.each do |arg|
arg_type = arg.type.unwrap
references_to(arg_type, from: arg)
add_type(arg_type, owner: arg, late_types: late_types, path: path + [arg.graphql_name])
if arg.default_value?
@arguments_with_default_values << arg
end
end
else
prev_type = @types[type.graphql_name]
if prev_type.nil?
@types[type.graphql_name] = type
elsif prev_type.is_a?(Array)
prev_type << type
else
@types[type.graphql_name] = [prev_type, type]
end
add_directives_from(type)
if type.kind.fields?
type.all_field_definitions.each do |field|
name = field.graphql_name
field_type = field.type.unwrap
references_to(field_type, from: field)
field_path = path + [name]
add_type(field_type, owner: field, late_types: late_types, path: field_path)
add_directives_from(field)
field.all_argument_definitions.each do |arg|
add_directives_from(arg)
arg_type = arg.type.unwrap
references_to(arg_type, from: arg)
add_type(arg_type, owner: arg, late_types: late_types, path: field_path + [arg.graphql_name])
if arg.default_value?
@arguments_with_default_values << arg
end
end
end
end
if type.kind.input_object?
type.all_argument_definitions.each do |arg|
add_directives_from(arg)
arg_type = arg.type.unwrap
references_to(arg_type, from: arg)
add_type(arg_type, owner: arg, late_types: late_types, path: path + [arg.graphql_name])
if arg.default_value?
@arguments_with_default_values << arg
end
end
end
if type.kind.union?
@possible_types[type.graphql_name] = type.all_possible_types
type.all_possible_types.each do |t|
add_type(t, owner: type, late_types: late_types, path: path + ["possible_types"])
end
end
if type.kind.interface?
type.orphan_types.each do |t|
add_type(t, owner: type, late_types: late_types, path: path + ["orphan_types"])
end
end
if type.kind.object?
possible_types_for_this_name = @possible_types[type.graphql_name] ||= []
possible_types_for_this_name << type
end
if type.kind.object? || type.kind.interface?
type.interface_type_memberships.each do |interface_type_membership|
case interface_type_membership
when Schema::TypeMembership
interface_type = interface_type_membership.abstract_type
# We can get these now; we'll have to get late-bound types later
if interface_type.is_a?(Module) && type.is_a?(Class)
implementers = @possible_types[interface_type.graphql_name] ||= []
implementers << type
end
when String, Schema::LateBoundType
interface_type = interface_type_membership
else
raise ArgumentError, "Invariant: unexpected type membership for #{type.graphql_name}: #{interface_type_membership.class} (#{interface_type_membership.inspect})"
end
add_type(interface_type, owner: type, late_types: late_types, path: path + ["implements"])
end
end
end
end
end
end
end