pggraphql 0.0.15 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30009a5c466252f930ace1828c0111d0e6f78fd4
4
- data.tar.gz: 51f4d7fdf7d354cc81797c11abf22c3c54a224ee
3
+ metadata.gz: 80f1221418fdd91945d982eb88b17657d5929222
4
+ data.tar.gz: 28e9a2d5d4c06083d25784ac758e6707231c4e4f
5
5
  SHA512:
6
- metadata.gz: 801bdabcbd2cb22177ed107c71a7eb49dbc968f27c10fe25f864d72b0ebb0da580d58daefb86c8921e84263aa9f51f6e9dce684f93417c4015c3b59b0c770d67
7
- data.tar.gz: 6a8c862a0bee46b0f4248c795ba64c7814054f507f66962bef395d9b95b2835711574f978f8d168b7c86d8fb41b4a731ac897350856d0087988d54f7834718b9
6
+ metadata.gz: d9077968f4bd9e50bd49b729460a9a1ed2ffa8dd76ec8826837a99a4b89e5273b957d29810c961fdd8c20c812ad2e515c5eb88e92445915c70c89c897ed4f56d
7
+ data.tar.gz: 6bdd8bc357ebc7adbe32484f6b6bf6ac71318f2bba1932fb92f1d67047b97f827ec0a7c2dfb448a68395618c2f79240815a2c64355aba409a22ff3ce52bf14cf
@@ -1,3 +1,3 @@
1
1
  module Pggraphql
2
- VERSION = "0.0.15"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/pggraphql.rb CHANGED
@@ -24,8 +24,8 @@ module PgGraphQl
24
24
  yield(type) if block_given?
25
25
  end
26
26
 
27
- def handle_sql_part(part, params)
28
- if part.is_a?(Array)
27
+ def handle_sql_part(part, params, level, table_levels)
28
+ next_part = if part.is_a?(Array)
29
29
  part.slice(1..-1).each{|param| params << param}
30
30
  part[0]
31
31
  elsif part.is_a?(String)
@@ -33,9 +33,38 @@ module PgGraphQl
33
33
  else
34
34
  raise "unsupported sql part: #{part}"
35
35
  end
36
+
37
+ next_part.gsub(/\{([^}]+)\}/) do
38
+ m = Regexp.last_match[1]
39
+
40
+ table_name, arg = m.split(":")
41
+ table_name = table_name.to_sym
42
+
43
+ sql_part = if arg =~ /-1$/
44
+ "#{table_name}#{level-1}"
45
+ elsif arg == "root"
46
+ "#{table_name}1"
47
+ elsif arg == "closest"
48
+ table_level_arr = table_levels[table_name]
49
+
50
+ raise "table #{table_name} not visited in table_levels" unless table_level_arr
51
+ closest_table_level = table_level_arr[table_level_arr.length - 1]
52
+
53
+ closest_table_level = closest_table_level - 1 if closest_table_level == level
54
+
55
+ "#{table_name}#{closest_table_level}"
56
+ else
57
+ "#{table_name}#{level}"
58
+ end
59
+
60
+ table_levels[table_name] = [] unless table_levels[table_name]
61
+ table_levels[table_name] << level unless table_levels[table_name].include?(level)
62
+
63
+ sql_part
64
+ end
36
65
  end
37
66
 
38
- def to_sql(query, level=0, params=[], parent=nil, link_name=nil)
67
+ def to_sql(query, level=0, params=[], table_levels={}, parent=nil, link_name=nil)
39
68
  if level > 0
40
69
  query.map do |e|
41
70
  link = parent ? parent.links[link_name.to_s.split("@").first.to_sym] : nil
@@ -63,24 +92,27 @@ module PgGraphQl
63
92
  raise "unknown link #{field_name.inspect} on type #{type.name.inspect}" if f[1].is_a?(Hash) && !type.links.include?(field_name.to_s.split("@").first.to_sym)
64
93
 
65
94
  if f[1].is_a?(Hash)
66
- "(" + to_sql([f].to_h, level + 1, params, type, nested_link_name) + ") as \"#{field_name}\""
95
+ "(" + to_sql([f].to_h, level + 1, params, table_levels, type, nested_link_name) + ") as \"#{field_name}\""
67
96
  else
68
97
  field_def = type.fields.detect{|f| f[:name] == field_name}
69
98
 
70
- column_name = field_def[:name].to_s.index("__") ? field_def[:name].to_s.gsub(/__/, ".").to_sym : field_def[:name]
71
- # column_expr = type.mappings[field_name] || column_name
72
-
73
- column_expr = if field_def[:expr]
74
- handle_sql_part(field_def[:expr].call(column_name), params)
99
+ column_name = if field_def[:name].to_s.index("__")
100
+ field_def[:name].to_s.gsub(/(.*?)__(.*?)/, "{\\1}.\\2").to_sym
75
101
  else
76
- column_name
102
+ field_def[:name]
103
+ end
104
+
105
+ unless column_name.to_s.index(".")
106
+ column_name = :"{#{type.table}}.#{column_name}"
77
107
  end
78
108
 
79
- if (column_name == field_name && column_name == column_expr)
80
- column_name.to_s
109
+ column_expr = if field_def[:expr]
110
+ handle_sql_part(field_def[:expr].call(column_name), params, level, table_levels)
81
111
  else
82
- "#{column_expr}" + (field_def[:as] ? " as #{field_def[:as]}" : "")
112
+ handle_sql_part("#{column_name}", params, level, table_levels)
83
113
  end
114
+
115
+ "#{column_expr}" + (field_def[:as] ? " as #{field_def[:as]}" : "")
84
116
  end
85
117
 
86
118
  end.join(",")
@@ -93,10 +125,10 @@ module PgGraphQl
93
125
  raise "missing :id for root type #{type.name.inspect}" if !ids && level == 1 && !type.null_pk
94
126
 
95
127
  if ids && type.pk.call(ids, level)
96
- wheres << handle_sql_part(type.pk.call(ids, level), params)
128
+ wheres << handle_sql_part(type.pk.call(ids, level), params, level, table_levels)
97
129
  end
98
130
 
99
- wheres << ("(" + handle_sql_part(type.filter, params) + ")") if type.filter
131
+ wheres << ("(" + handle_sql_part(type.filter, params, level, table_levels) + ")") if type.filter
100
132
 
101
133
  if link
102
134
  fk = link.fk.is_a?(Proc) ? link.fk.call(level) : link.fk
@@ -104,48 +136,49 @@ module PgGraphQl
104
136
  if link_name.to_s.index("__")
105
137
  subtype_type, subtype_link_name = link_name.to_s.split("__")
106
138
 
107
- fk = "#{link.type.table}.id = #{subtype_type}.#{subtype_link_name}_id" if fk == :belongs_to
108
- fk = "#{link.type.table}.#{parent.name}_id = #{subtype_type}.id" if fk == :has_one
109
- fk = "#{link.type.table}.#{parent.name}_id = #{subtype_type}.id" if fk == :many
139
+ fk = "{#{link.type.table}}.id = {#{subtype_type}:-1}.#{subtype_link_name}_id" if fk == :belongs_to
140
+ fk = "{#{link.type.table}}.#{parent.name}_id = {#{subtype_type}:-1}.id" if fk == :has_one
141
+ fk = "{#{link.type.table}}.#{parent.name}_id = {#{subtype_type}:-1}.id" if fk == :many
110
142
  else
111
- fk = "#{link.type.table}.id = #{parent.table}.#{link.name}_id" if fk == :belongs_to
112
- fk = "#{link.type.table}.#{parent.name}_id = #{parent.table}.id" if fk == :has_one
113
- fk = "#{link.type.table}.#{parent.name}_id = #{parent.table}.id" if fk == :many
143
+ fk = "{#{link.type.table}}.id = {#{parent.table}:-1}.#{link.name}_id" if fk == :belongs_to
144
+ fk = "{#{link.type.table}}.#{parent.name}_id = {#{parent.table}:-1}.id" if fk == :has_one
145
+ fk = "{#{link.type.table}}.#{parent.name}_id = {#{parent.table}:-1}.id" if fk == :many
114
146
  end
115
147
 
116
- wheres << ("(" + handle_sql_part(fk, params) + ")")
117
- wheres << ("(" + handle_sql_part(link.filter, params) + ")") if link.filter
148
+ wheres << ("(" + handle_sql_part(fk, params, level, table_levels) + ")")
149
+ wheres << ("(" + handle_sql_part(link.filter, params, level, table_levels) + ")") if link.filter
118
150
  end
119
151
 
152
+ table_as = handle_sql_part("{#{type.table}}", params, level, table_levels)
153
+
120
154
  sql = "select to_json("
121
155
  sql += "coalesce(json_agg(" if is_many
122
156
  sql += "x.*"
123
157
  sql += "), '[]'::json)" if is_many
124
- sql += ") from (select #{columns} from #{type.table}"
158
+ sql += ") from (select #{columns} from #{type.table} as #{table_as}"
125
159
 
126
160
  unless type.subtypes.empty?
127
161
  sql += "\n" + type.subtypes.map do |f|
128
162
  subtype = f[1]
129
163
  fk = subtype.fk.is_a?(Proc) ? subtype.fk.call(level) : subtype.fk
130
164
 
131
- fk = "#{subtype.name}.id = #{type.table}.id and #{type.table}.type = '#{subtype.name}'" if fk == :subtype
165
+ fk = "{#{subtype.name}}.id = {#{type.table}}.id and {#{type.table}}.type = '#{subtype.name}'" if fk == :subtype
166
+ subtype_as = handle_sql_part("{#{subtype.name}}", params, level, table_levels)
132
167
 
133
- subtype_as = subtype.fk.is_a?(Proc) ? "#{subtype.name}#{level}" : subtype.name
134
- # subtype_as = (link && parent && link.type == parent.name) ? "#{subtype.name}#{level}" : subtype.name
135
- "left join #{subtype.table} as #{subtype_as} on (#{handle_sql_part(fk, params)})"
168
+ "left join #{subtype.table} as #{subtype_as} on (#{handle_sql_part(fk, params, level, table_levels)})"
136
169
  end.join("\n")
137
170
  end
138
171
 
139
172
 
140
173
  sql += " where #{wheres.join(' and ')}" unless wheres.empty?
141
- sql += " order by #{order_by}" if order_by
174
+ sql += " order by #{handle_sql_part(order_by, params, level, table_levels)}" if order_by
142
175
  sql += " limit 1" if !is_many
143
176
  sql += ") x"
144
177
 
145
178
  end.join
146
179
  else
147
180
  root_sql = wrap_root(query.map do |e|
148
- sql = to_sql([e].to_h, 1, params)
181
+ sql = to_sql([e].to_h, 1, params, table_levels)
149
182
  "select '#{e[0]}'::text as key, (#{sql}) as value"
150
183
  end.join("\nunion all\n"))
151
184
 
@@ -171,12 +204,10 @@ module PgGraphQl
171
204
  @subtypes = {}
172
205
  @null_pk = false
173
206
  @pk = ->(ids, level) do
174
- id_column = "#{@table}.id"
207
+ id_column = "{#{@table}}.id"
175
208
  if ids.is_a?(Array)
176
- # "#{id_column} in (" + ids.map{|id| id.is_a?(String) ? "'#{id}'" : id.to_s}.join(',') + ")"
177
209
  ["#{id_column} in ?", ids]
178
210
  else
179
- # "#{id_column} = " + (ids.is_a?(String) ? "'#{ids}'" : "#{ids}")
180
211
  ["#{id_column} = ?", ids]
181
212
  end
182
213
  end
@@ -188,7 +219,7 @@ module PgGraphQl
188
219
  @fields = fields.map{|f| create_field(f)}
189
220
  end
190
221
  def fields
191
- @fields + [create_field({name: :id, as: nil, expr: ->(c){ "#{@table}.#{c}" }})] + (@subtypes.empty? ? [] : [create_field(:type)])
222
+ @fields + [create_field({name: :id, as: nil, expr: ->(c){ "#{c}" }})] + (@subtypes.empty? ? [] : [create_field(:type)])
192
223
  end
193
224
  def create_field(field)
194
225
  if field.is_a?(Symbol)
@@ -23,6 +23,88 @@ module PgGraphQl
23
23
  res
24
24
  end
25
25
 
26
+ def test_simple_level_aware
27
+ res = to_sql({user: {id: 1, email: "email"}}) do |s|
28
+ s.root :user
29
+ s.type :user, fields: [:email]
30
+ end
31
+
32
+ assert_equal token(<<-SQL
33
+ select 'user'::text as key,
34
+ (select to_json(x.*)
35
+ from (select users1.id,
36
+ users1.email as email
37
+ from users as users1
38
+ where users1.id = ? limit 1) x) as value
39
+ SQL
40
+ ), token(res[:sql])
41
+
42
+ assert_equal [1], res[:params]
43
+ end
44
+
45
+ def test_nested_level_aware
46
+ res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
47
+ s.root :user
48
+ s.type :user, fields: [:email] do |t|
49
+ t.has_one :address, order_by: "{users:root}.id desc"
50
+ end
51
+ s.type :address
52
+ end
53
+
54
+ assert_equal token(<<-SQL
55
+ select 'user'::text as key,
56
+ (select to_json(x.*)
57
+ from (select users1.id,
58
+ users1.email as email,
59
+ (select to_json(x.*)
60
+ from (select addresses2.id
61
+ from addresses as addresses2
62
+ where (addresses2.user_id = users1.id) order by users1.id desc limit 1) x) as "address"
63
+ from users as users1
64
+ where users1.id = ? limit 1) x) as value
65
+ SQL
66
+ ), token(res[:sql])
67
+
68
+ assert_equal [1], res[:params]
69
+
70
+ # ---
71
+
72
+
73
+ res = to_sql({user: {id: 1, email: "email", address: {person: {}}}}) do |s|
74
+ s.root :user
75
+ s.type :user, fields: [:email] do |t|
76
+ t.has_one :address
77
+ end
78
+ s.type :address do |t|
79
+ t.has_one :person
80
+ end
81
+
82
+ s.type :person, order_by: "{users:closest}.id desc"
83
+ end
84
+
85
+ assert_equal token(<<-SQL
86
+ select 'user'::text as key,
87
+ (select to_json(x.*)
88
+ from (select users1.id,
89
+ users1.email as email,
90
+ (select to_json(x.*)
91
+ from (select addresses2.id,
92
+ (select to_json(x.*)
93
+ from (select people3.id
94
+ from people as people3
95
+ where (people3.address_id = addresses2.id) order by users1.id desc limit 1) x) as "person"
96
+ from addresses as addresses2
97
+ where (addresses2.user_id = users1.id) limit 1) x) as "address"
98
+ from users as users1
99
+ where users1.id = ? limit 1) x) as value
100
+ SQL
101
+ ), token(res[:sql])
102
+
103
+ assert_equal [1], res[:params]
104
+
105
+ end
106
+
107
+
26
108
  def test_simple
27
109
  res = to_sql({user: {id: 1, email: "email"}}) do |s|
28
110
  s.root :user
@@ -32,10 +114,10 @@ module PgGraphQl
32
114
  assert_equal token(<<-SQL
33
115
  select 'user'::text as key,
34
116
  (select to_json(x.*)
35
- from (select users.id,
36
- email
37
- from users
38
- where users.id = ? limit 1) x) as value
117
+ from (select users1.id,
118
+ users1.email as email
119
+ from users as users1
120
+ where users1.id = ? limit 1) x) as value
39
121
  SQL
40
122
  ), token(res[:sql])
41
123
 
@@ -51,10 +133,10 @@ module PgGraphQl
51
133
  assert_equal token(<<-SQL
52
134
  select 'user'::text as key,
53
135
  (select to_json(x.*)
54
- from (select users.id,
55
- email
56
- from users
57
- where users.id = ? limit 1) x) as value
136
+ from (select users1.id,
137
+ users1.email as email
138
+ from users as users1
139
+ where users1.id = ? limit 1) x) as value
58
140
  SQL
59
141
  ), token(res[:sql])
60
142
  assert_equal [1], res[:params]
@@ -69,9 +151,9 @@ module PgGraphQl
69
151
  assert_equal token(<<-SQL
70
152
  select 'user'::text as key,
71
153
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
72
- from (select users.id,
73
- email
74
- from users) x) as value
154
+ from (select users1.id,
155
+ users1.email as email
156
+ from users as users1) x) as value
75
157
  SQL
76
158
  ), token(res[:sql])
77
159
  assert_equal [], res[:params]
@@ -120,9 +202,9 @@ module PgGraphQl
120
202
  assert_equal token(<<-SQL
121
203
  select 'user'::text as key,
122
204
  (select to_json(x.*)
123
- from (select users.id,
124
- email
125
- from users where access_token = ? limit 1) x) as value
205
+ from (select users1.id,
206
+ users1.email as email
207
+ from users as users1 where access_token = ? limit 1) x) as value
126
208
  SQL
127
209
  ), token(res[:sql])
128
210
  assert_equal ["1"], res[:params]
@@ -137,9 +219,9 @@ module PgGraphQl
137
219
  assert_equal token(<<-SQL
138
220
  select 'user'::text as key,
139
221
  (select to_json(x.*)
140
- from (select users.id,
141
- email
142
- from users where level1 = ? limit 1) x) as value
222
+ from (select users1.id,
223
+ users1.email as email
224
+ from users as users1 where level1 = ? limit 1) x) as value
143
225
  SQL
144
226
  ), token(res[:sql])
145
227
  assert_equal ["99"], res[:params]
@@ -154,9 +236,9 @@ module PgGraphQl
154
236
  assert_equal token(<<-SQL
155
237
  select 'user'::text as key,
156
238
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
157
- from (select users.id,
158
- email
159
- from users where users.id in ?) x) as value
239
+ from (select users1.id,
240
+ users1.email as email
241
+ from users as users1 where users1.id in ?) x) as value
160
242
  SQL
161
243
  ), token(res[:sql])
162
244
  assert_equal [["1"]], res[:params]
@@ -171,9 +253,9 @@ module PgGraphQl
171
253
  assert_equal token(<<-SQL
172
254
  select 'user'::text as key,
173
255
  (select to_json(x.*)
174
- from (select users.id,
175
- email
176
- from users where users.id = ? limit 1) x) as value
256
+ from (select users1.id,
257
+ users1.email as email
258
+ from users as users1 where users1.id = ? limit 1) x) as value
177
259
  SQL
178
260
  ), token(res[:sql])
179
261
  assert_equal ["1"], res[:params]
@@ -188,9 +270,9 @@ module PgGraphQl
188
270
  assert_equal token(<<-SQL
189
271
  select 'user'::text as key,
190
272
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
191
- from (select users.id,
192
- email
193
- from users where users.id in ?) x) as value
273
+ from (select users1.id,
274
+ users1.email as email
275
+ from users as users1 where users1.id in ?) x) as value
194
276
  SQL
195
277
  ), token(res[:sql])
196
278
  assert_equal [[1]], res[:params]
@@ -205,9 +287,9 @@ module PgGraphQl
205
287
  assert_equal token(<<-SQL
206
288
  select 'user'::text as key,
207
289
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
208
- from (select users.id,
209
- email
210
- from users where users.id in ?) x) as value
290
+ from (select users1.id,
291
+ users1.email as email
292
+ from users as users1 where users1.id in ?) x) as value
211
293
  SQL
212
294
  ), token(res[:sql])
213
295
  assert_equal [[1,2]], res[:params]
@@ -222,9 +304,9 @@ module PgGraphQl
222
304
  assert_equal token(<<-SQL
223
305
  select 'user'::text as key,
224
306
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
225
- from (select users.id,
226
- email
227
- from users where users.id in ?) x) as value
307
+ from (select users1.id,
308
+ users1.email as email
309
+ from users as users1 where users1.id in ?) x) as value
228
310
  SQL
229
311
  ), token(res[:sql])
230
312
  assert_equal [["1","2"]], res[:params]
@@ -239,10 +321,10 @@ module PgGraphQl
239
321
  assert_equal token(<<-SQL
240
322
  select 'user'::text as key,
241
323
  (select to_json(x.*)
242
- from (select users.id,
243
- email
244
- from users
245
- where users.id = ? and (id > 100) limit 1) x) as value
324
+ from (select users1.id,
325
+ users1.email as email
326
+ from users as users1
327
+ where users1.id = ? and (id > 100) limit 1) x) as value
246
328
  SQL
247
329
  ), token(res[:sql])
248
330
  assert_equal [1], res[:params]
@@ -259,15 +341,15 @@ module PgGraphQl
259
341
  assert_equal token(<<-SQL
260
342
  select 'user'::text as key,
261
343
  (select to_json(x.*)
262
- from (select users.id,
263
- email
264
- from users
265
- where users.id = ? limit 1) x) as value
344
+ from (select users1.id,
345
+ users1.email as email
346
+ from users as users1
347
+ where users1.id = ? limit 1) x) as value
266
348
  union all
267
349
  select 'educator'::text as key,
268
350
  (select to_json(x.*)
269
- from (select educators.id
270
- from educators where educators.id = ? limit 1) x) as value
351
+ from (select educators1.id
352
+ from educators as educators1 where educators1.id = ? limit 1) x) as value
271
353
  SQL
272
354
  ), token(res[:sql])
273
355
  assert_equal [1, 99], res[:params]
@@ -285,7 +367,7 @@ module PgGraphQl
285
367
  end
286
368
 
287
369
  assert_equal token(<<-SQL
288
- select 'flow'::text as key, (select to_json(x.*) from (select flows.id, data from flows where flows.id = ? limit 1) x) as value
370
+ select 'flow'::text as key, (select to_json(x.*) from (select flows1.id, flows1.data as data from flows as flows1 where flows1.id = ? limit 1) x) as value
289
371
  SQL
290
372
  ), token(res[:sql])
291
373
  assert_equal [1], res[:params]
@@ -300,7 +382,7 @@ module PgGraphQl
300
382
  end
301
383
 
302
384
  assert_equal token(<<-SQL
303
- select 'flow'::text as key, (select to_json(x.*) from (select flows.id, data from flows where flows.id = ? limit 1) x) as value
385
+ select 'flow'::text as key, (select to_json(x.*) from (select flows1.id, flows1.data from flows as flows1 where flows1.id = ? limit 1) x) as value
304
386
  SQL
305
387
  ), token(res[:sql])
306
388
  assert_equal [1], res[:params]
@@ -315,7 +397,7 @@ module PgGraphQl
315
397
  end
316
398
 
317
399
  assert_equal token(<<-SQL
318
- select 'flow'::text as key, (select to_json(x.*) from (select flows.id, to_json(data) as data from flows where flows.id = ? limit 1) x) as value
400
+ select 'flow'::text as key, (select to_json(x.*) from (select flows1.id, to_json(flows1.data) as data from flows as flows1 where flows1.id = ? limit 1) x) as value
319
401
  SQL
320
402
  ), token(res[:sql])
321
403
  assert_equal [1], res[:params]
@@ -330,7 +412,7 @@ module PgGraphQl
330
412
  end
331
413
 
332
414
  assert_equal token(<<-SQL
333
- select 'flow'::text as key, (select to_json(x.*) from (select flows.id, to_json(data) from flows where flows.id = ? limit 1) x) as value
415
+ select 'flow'::text as key, (select to_json(x.*) from (select flows1.id, to_json(flows1.data) from flows as flows1 where flows1.id = ? limit 1) x) as value
334
416
  SQL
335
417
  ), token(res[:sql])
336
418
  assert_equal [1], res[:params]
@@ -342,55 +424,6 @@ module PgGraphQl
342
424
  #####################
343
425
 
344
426
  def test_inherit
345
- res = to_sql({
346
- products: {
347
- type: nil,
348
- clickout__destination_url: nil,
349
- download__download_url: nil,
350
- download__users: {
351
- orders: {}
352
- }
353
- }
354
- }) do |s|
355
- s.root :product
356
- s.type :user, fields: [:email] do |t|
357
- t.many :orders, fk: "user_id = users.id"
358
- end
359
-
360
- s.type :order
361
- s.type :product, null_pk: :array, fields: [:type, :clickout__destination_url, :download__download_url] do |t|
362
- t.subtype :download, table: :product_downloads, fk: "download.id = products.id and products.type = 'download'"
363
- t.subtype :clickout, table: :product_clickouts, fk: "clickout.id = products.id and products.type = 'clickout'"
364
- t.many :download__users, type: :user, fk: "id = download.id"
365
- end
366
- end
367
-
368
- assert_equal token(<<-SQL
369
- select 'products'::text as key,
370
- (select to_json(coalesce(json_agg(x.*), '[]'::json))
371
- from (select products.id,
372
- type,
373
- clickout.destination_url as clickout__destination_url,
374
- download.download_url as download__download_url,
375
- (select to_json(coalesce(json_agg(x.*), '[]'::json))
376
- from (select users.id,
377
- (select to_json(coalesce(json_agg(x.*), '[]'::json))
378
- from (select orders.id
379
- from orders
380
- where (user_id = users.id)) x) as "orders"
381
- from users
382
- where (id = download.id)) x) as "download__users"
383
- from products
384
- left join product_downloads as download on (download.id = products.id
385
- and products.type = 'download')
386
- left join product_clickouts as clickout on (clickout.id = products.id
387
- and products.type = 'clickout')) x) as value
388
- SQL
389
- ), token(res[:sql])
390
- assert_equal [], res[:params]
391
-
392
- # ------
393
-
394
427
  res = to_sql({
395
428
  products: {
396
429
  type: nil,
@@ -408,32 +441,33 @@ module PgGraphQl
408
441
 
409
442
  s.type :order
410
443
  s.type :product, null_pk: :array, fields: [:type, :clickout__destination_url, :download__download_url] do |t|
411
- t.subtype :download, table: :product_downloads
412
444
  t.subtype :clickout, table: :product_clickouts
413
- t.many :download__users, type: :user
445
+ t.subtype :download, table: :product_downloads do |st|
446
+ st.many :users, type: :user
447
+ end
414
448
  end
415
449
  end
416
450
 
417
451
  assert_equal token(<<-SQL
418
452
  select 'products'::text as key,
419
453
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
420
- from (select products.id,
421
- type,
422
- clickout.destination_url as clickout__destination_url,
423
- download.download_url as download__download_url,
454
+ from (select products1.id,
455
+ products1.type as type,
456
+ clickout1.destination_url as clickout__destination_url,
457
+ download1.download_url as download__download_url,
424
458
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
425
- from (select users.id,
459
+ from (select users2.id,
426
460
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
427
- from (select orders.id
428
- from orders
429
- where (orders.user_id = users.id)) x) as "orders"
430
- from users
431
- where (users.product_id = download.id)) x) as "download__users"
432
- from products
433
- left join product_downloads as download on (download.id = products.id
434
- and products.type = 'download')
435
- left join product_clickouts as clickout on (clickout.id = products.id
436
- and products.type = 'clickout')) x) as value
461
+ from (select orders3.id
462
+ from orders as orders3
463
+ where (orders3.user_id = users2.id)) x) as "orders"
464
+ from users as users2
465
+ where (users2.product_id = download1.id)) x) as "download__users"
466
+ from products as products1
467
+ left join product_clickouts as clickout1 on (clickout1.id = products1.id
468
+ and products1.type = 'clickout')
469
+ left join product_downloads as download1 on (download1.id = products1.id
470
+ and products1.type = 'download')) x) as value
437
471
  SQL
438
472
  ), token(res[:sql])
439
473
  assert_equal [], res[:params]
@@ -467,23 +501,23 @@ module PgGraphQl
467
501
  assert_equal token(<<-SQL
468
502
  select 'products'::text as key,
469
503
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
470
- from (select products.id,
471
- type,
472
- clickout.destination_url as clickout__destination_url,
473
- download.download_url as download__download_url,
504
+ from (select products1.id,
505
+ products1.type as type,
506
+ clickout1.destination_url as clickout__destination_url,
507
+ download1.download_url as download__download_url,
474
508
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
475
- from (select users.id,
509
+ from (select users2.id,
476
510
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
477
- from (select orders.id
478
- from orders
479
- where (orders.user_id = users.id)) x) as "orders"
480
- from users
481
- where (users.product_id = download.id)) x) as "download__users"
482
- from products
483
- left join product_downloads as download on (download.id = products.id
484
- and products.type = 'download')
485
- left join product_clickouts as clickout on (clickout.id = products.id
486
- and products.type = 'clickout')) x) as value
511
+ from (select orders3.id
512
+ from orders as orders3
513
+ where (orders3.user_id = users2.id)) x) as "orders"
514
+ from users as users2
515
+ where (users2.product_id = download1.id)) x) as "download__users"
516
+ from products as products1
517
+ left join product_downloads as download1 on (download1.id = products1.id
518
+ and products1.type = 'download')
519
+ left join product_clickouts as clickout1 on (clickout1.id = products1.id
520
+ and products1.type = 'clickout')) x) as value
487
521
  SQL
488
522
  ), token(res[:sql])
489
523
  assert_equal [], res[:params]
@@ -498,18 +532,19 @@ module PgGraphQl
498
532
  }) do |s|
499
533
  s.root :product
500
534
  s.type :product, null_pk: :array, fields: [:clickout__destination_url, :download__download_url] do |t|
501
- t.subtype :clickout, table: :product_clickouts, fk: "clickout.id = products.id and products.type = 'clickout'"
535
+ t.subtype :clickout, table: :product_clickouts
502
536
  end
503
537
  end
504
538
 
505
539
  assert_equal token(<<-SQL
506
540
  select 'products'::text as key,
507
541
  (select to_json(x.*)
508
- from (select products.id, type,
509
- clickout.destination_url as clickout__destination_url
510
- from products
511
- left join product_clickouts as clickout on (clickout.id = products.id
512
- and products.type = 'clickout') where products.id = ? limit 1) x) as value
542
+ from (select products1.id,
543
+ products1.type as type,
544
+ clickout1.destination_url as clickout__destination_url
545
+ from products as products1
546
+ left join product_clickouts as clickout1 on (clickout1.id = products1.id
547
+ and products1.type = 'clickout') where products1.id = ? limit 1) x) as value
513
548
  SQL
514
549
  ), token(res[:sql])
515
550
  assert_equal [1], res[:params]
@@ -524,7 +559,7 @@ module PgGraphQl
524
559
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
525
560
  s.root :user
526
561
  s.type :user, fields: [:email] do |t|
527
- t.one :address, fk: :belongs_to
562
+ t.belongs_to :address
528
563
  end
529
564
  s.type :address
530
565
  end
@@ -532,14 +567,14 @@ module PgGraphQl
532
567
  assert_equal token(<<-SQL
533
568
  select 'user'::text as key,
534
569
  (select to_json(x.*)
535
- from (select users.id,
536
- email,
570
+ from (select users1.id,
571
+ users1.email as email,
537
572
  (select to_json(x.*)
538
- from (select addresses.id
539
- from addresses
540
- where addresses.id = ? and (addresses.id = users.address_id) limit 1) x) as "address"
541
- from users
542
- where users.id = ? limit 1) x) as value
573
+ from (select addresses2.id
574
+ from addresses as addresses2
575
+ where addresses2.id = ? and (addresses2.id = users1.address_id) limit 1) x) as "address"
576
+ from users as users1
577
+ where users1.id = ? limit 1) x) as value
543
578
  SQL
544
579
  ), token(res[:sql])
545
580
  assert_equal ["99", 1], res[:params]
@@ -549,7 +584,7 @@ module PgGraphQl
549
584
  res = to_sql({user: {id: 1, email: "email", other_address: {id: "99"}}}) do |s|
550
585
  s.root :user
551
586
  s.type :user, fields: [:email] do |t|
552
- t.one :other_address, type: :address, fk: :belongs_to
587
+ t.belongs_to :other_address, type: :address
553
588
  end
554
589
  s.type :address
555
590
  end
@@ -557,14 +592,14 @@ module PgGraphQl
557
592
  assert_equal token(<<-SQL
558
593
  select 'user'::text as key,
559
594
  (select to_json(x.*)
560
- from (select users.id,
561
- email,
595
+ from (select users1.id,
596
+ users1.email as email,
562
597
  (select to_json(x.*)
563
- from (select addresses.id
564
- from addresses
565
- where addresses.id = ? and (addresses.id = users.other_address_id) limit 1) x) as "other_address"
566
- from users
567
- where users.id = ? limit 1) x) as value
598
+ from (select addresses2.id
599
+ from addresses as addresses2
600
+ where addresses2.id = ? and (addresses2.id = users1.other_address_id) limit 1) x) as "other_address"
601
+ from users as users1
602
+ where users1.id = ? limit 1) x) as value
568
603
  SQL
569
604
  ), token(res[:sql])
570
605
  assert_equal ["99", 1], res[:params]
@@ -574,32 +609,7 @@ module PgGraphQl
574
609
  res = to_sql({user: {id: 1, email: "email", other_address: {id: "99"}}}) do |s|
575
610
  s.root :user
576
611
  s.type :user, fields: [:email] do |t|
577
- t.one :other_address, type: :address, fk: :has_one
578
- end
579
- s.type :address
580
- end
581
-
582
- assert_equal token(<<-SQL
583
- select 'user'::text as key,
584
- (select to_json(x.*)
585
- from (select users.id,
586
- email,
587
- (select to_json(x.*)
588
- from (select addresses.id
589
- from addresses
590
- where addresses.id = ? and (addresses.user_id = users.id) limit 1) x) as "other_address"
591
- from users
592
- where users.id = ? limit 1) x) as value
593
- SQL
594
- ), token(res[:sql])
595
- assert_equal ["99", 1], res[:params]
596
- end
597
-
598
- def test_link_has_one
599
- res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
600
- s.root :user
601
- s.type :user, fields: [:email] do |t|
602
- t.one :address, fk: :has_one
612
+ t.has_one :other_address, type: :address
603
613
  end
604
614
  s.type :address
605
615
  end
@@ -607,14 +617,14 @@ module PgGraphQl
607
617
  assert_equal token(<<-SQL
608
618
  select 'user'::text as key,
609
619
  (select to_json(x.*)
610
- from (select users.id,
611
- email,
620
+ from (select users1.id,
621
+ users1.email as email,
612
622
  (select to_json(x.*)
613
- from (select addresses.id
614
- from addresses
615
- where addresses.id = ? and (addresses.user_id = users.id) limit 1) x) as "address"
616
- from users
617
- where users.id = ? limit 1) x) as value
623
+ from (select addresses2.id
624
+ from addresses as addresses2
625
+ where addresses2.id = ? and (addresses2.user_id = users1.id) limit 1) x) as "other_address"
626
+ from users as users1
627
+ where users1.id = ? limit 1) x) as value
618
628
  SQL
619
629
  ), token(res[:sql])
620
630
  assert_equal ["99", 1], res[:params]
@@ -624,7 +634,7 @@ module PgGraphQl
624
634
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
625
635
  s.root :user
626
636
  s.type :user, fields: [:email] do |t|
627
- t.one :address, fk: "id = users.address_id"
637
+ t.belongs_to :address
628
638
  end
629
639
  s.type :address
630
640
  end
@@ -632,14 +642,14 @@ module PgGraphQl
632
642
  assert_equal token(<<-SQL
633
643
  select 'user'::text as key,
634
644
  (select to_json(x.*)
635
- from (select users.id,
636
- email,
645
+ from (select users1.id,
646
+ users1.email as email,
637
647
  (select to_json(x.*)
638
- from (select addresses.id
639
- from addresses
640
- where addresses.id = ? and (id = users.address_id) limit 1) x) as "address"
641
- from users
642
- where users.id = ? limit 1) x) as value
648
+ from (select addresses2.id
649
+ from addresses as addresses2
650
+ where addresses2.id = ? and (addresses2.id = users1.address_id) limit 1) x) as "address"
651
+ from users as users1
652
+ where users1.id = ? limit 1) x) as value
643
653
  SQL
644
654
  ), token(res[:sql])
645
655
  assert_equal ["99", 1], res[:params]
@@ -649,7 +659,7 @@ module PgGraphQl
649
659
  res = to_sql({user: {id: 1, email: "email", address: {id: 99}}}) do |s|
650
660
  s.root :user
651
661
  s.type :user, fields: [:email] do |t|
652
- t.one :address, fk: "id = users.address_id"
662
+ t.belongs_to :address
653
663
  end
654
664
  s.type :address
655
665
  end
@@ -657,14 +667,14 @@ module PgGraphQl
657
667
  assert_equal token(<<-SQL
658
668
  select 'user'::text as key,
659
669
  (select to_json(x.*)
660
- from (select users.id,
661
- email,
670
+ from (select users1.id,
671
+ users1.email as email,
662
672
  (select to_json(x.*)
663
- from (select addresses.id
664
- from addresses
665
- where addresses.id = ? and (id = users.address_id) limit 1) x) as "address"
666
- from users
667
- where users.id = ? limit 1) x) as value
673
+ from (select addresses2.id
674
+ from addresses as addresses2
675
+ where addresses2.id = ? and (addresses2.id = users1.address_id) limit 1) x) as "address"
676
+ from users as users1
677
+ where users1.id = ? limit 1) x) as value
668
678
  SQL
669
679
  ), token(res[:sql])
670
680
  assert_equal [99, 1], res[:params]
@@ -672,7 +682,7 @@ module PgGraphQl
672
682
  res = to_sql({user: {id: 1, email: "email", address: {id: [99,999]}}}) do |s|
673
683
  s.root :user
674
684
  s.type :user, fields: [:email] do |t|
675
- t.one :address, fk: "id = users.address_id"
685
+ t.belongs_to :address
676
686
  end
677
687
  s.type :address
678
688
  end
@@ -680,14 +690,14 @@ module PgGraphQl
680
690
  assert_equal token(<<-SQL
681
691
  select 'user'::text as key,
682
692
  (select to_json(x.*)
683
- from (select users.id,
684
- email,
693
+ from (select users1.id,
694
+ users1.email as email,
685
695
  (select to_json(x.*)
686
- from (select addresses.id
687
- from addresses
688
- where addresses.id in ? and (id = users.address_id) limit 1) x) as "address"
689
- from users
690
- where users.id = ? limit 1) x) as value
696
+ from (select addresses2.id
697
+ from addresses as addresses2
698
+ where addresses2.id in ? and (addresses2.id = users1.address_id) limit 1) x) as "address"
699
+ from users as users1
700
+ where users1.id = ? limit 1) x) as value
691
701
  SQL
692
702
  ), token(res[:sql])
693
703
  assert_equal [[99,999], 1], res[:params]
@@ -697,7 +707,7 @@ module PgGraphQl
697
707
  res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
698
708
  s.root :user
699
709
  s.type :user, fields: [:email] do |t|
700
- t.one :address, fk: "id = users.address_id"
710
+ t.belongs_to :address
701
711
  end
702
712
  s.type :address
703
713
  end
@@ -705,14 +715,14 @@ module PgGraphQl
705
715
  assert_equal token(<<-SQL
706
716
  select 'user'::text as key,
707
717
  (select to_json(x.*)
708
- from (select users.id,
709
- email,
718
+ from (select users1.id,
719
+ users1.email as email,
710
720
  (select to_json(x.*)
711
- from (select addresses.id
712
- from addresses
713
- where (id = users.address_id) limit 1) x) as "address"
714
- from users
715
- where users.id = ? limit 1) x) as value
721
+ from (select addresses2.id
722
+ from addresses as addresses2
723
+ where (addresses2.id = users1.address_id) limit 1) x) as "address"
724
+ from users as users1
725
+ where users1.id = ? limit 1) x) as value
716
726
  SQL
717
727
  ), token(res[:sql])
718
728
  assert_equal [1], res[:params]
@@ -743,14 +753,14 @@ module PgGraphQl
743
753
  assert_equal token(<<-SQL
744
754
  select 'user'::text as key,
745
755
  (select to_json(x.*)
746
- from (select users.id,
747
- email,
756
+ from (select users1.id,
757
+ users1.email as email,
748
758
  (select to_json(x.*)
749
- from (select addresses.id
750
- from addresses
751
- where addresses.id = ? and (id = (select 100)) limit 1) x) as "address"
752
- from users
753
- where users.id = ? limit 1) x) as value
759
+ from (select addresses2.id
760
+ from addresses as addresses2
761
+ where addresses2.id = ? and (id = (select 100)) limit 1) x) as "address"
762
+ from users as users1
763
+ where users1.id = ? limit 1) x) as value
754
764
  SQL
755
765
  ), token(res[:sql])
756
766
  assert_equal ["99", 1], res[:params]
@@ -760,7 +770,7 @@ module PgGraphQl
760
770
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
761
771
  s.root :user
762
772
  s.type :user, fields: [:email] do |t|
763
- t.one :address, fk: "user_id = users.id", filter: "id > 100"
773
+ t.has_one :address, filter: "id > 100"
764
774
  end
765
775
  s.type :address
766
776
  end
@@ -768,14 +778,14 @@ module PgGraphQl
768
778
  assert_equal token(<<-SQL
769
779
  select 'user'::text as key,
770
780
  (select to_json(x.*)
771
- from (select users.id,
772
- email,
781
+ from (select users1.id,
782
+ users1.email as email,
773
783
  (select to_json(x.*)
774
- from (select addresses.id
775
- from addresses
776
- where addresses.id = ? and (user_id = users.id) and (id > 100) limit 1) x) as "address"
777
- from users
778
- where users.id = ? limit 1) x) as value
784
+ from (select addresses2.id
785
+ from addresses as addresses2
786
+ where addresses2.id = ? and (addresses2.user_id = users1.id) and (id > 100) limit 1) x) as "address"
787
+ from users as users1
788
+ where users1.id = ? limit 1) x) as value
779
789
  SQL
780
790
  ), token(res[:sql])
781
791
  assert_equal ["99", 1], res[:params]
@@ -785,7 +795,7 @@ module PgGraphQl
785
795
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
786
796
  s.root :user
787
797
  s.type :user, fields: [:email] do |t|
788
- t.one :address, fk: "user_id = users.id", order_by: "id desc"
798
+ t.has_one :address, order_by: "id desc"
789
799
  end
790
800
  s.type :address
791
801
  end
@@ -793,31 +803,31 @@ module PgGraphQl
793
803
  assert_equal token(<<-SQL
794
804
  select 'user'::text as key,
795
805
  (select to_json(x.*)
796
- from (select users.id,
797
- email,
806
+ from (select users1.id,
807
+ users1.email as email,
798
808
  (select to_json(x.*)
799
- from (select addresses.id
800
- from addresses
801
- where addresses.id = ? and (user_id = users.id) order by id desc limit 1) x) as "address"
802
- from users
803
- where users.id = ? limit 1) x) as value
809
+ from (select addresses2.id
810
+ from addresses as addresses2
811
+ where addresses2.id = ? and (addresses2.user_id = users1.id) order by id desc limit 1) x) as "address"
812
+ from users as users1
813
+ where users1.id = ? limit 1) x) as value
804
814
  SQL
805
815
  ), token(res[:sql])
806
816
  assert_equal ["99", 1], res[:params]
807
817
  end
808
818
 
809
- #####################
810
- # one-in-one
811
- #####################
819
+ # #####################
820
+ # # one-in-one
821
+ # #####################
812
822
 
813
823
  def test_link_one_in_one
814
824
  res = to_sql({user: {id: 1, email: "email", address: {country: {}}}}) do |s|
815
825
  s.root :user
816
826
  s.type :user, fields: [:email] do |t|
817
- t.one :address, fk: "user_id = users.id"
827
+ t.has_one :address
818
828
  end
819
829
  s.type :address do |t|
820
- t.one :country, fk: "id = addresses.country_id"
830
+ t.belongs_to :country
821
831
  end
822
832
  s.type :country
823
833
  end
@@ -825,33 +835,33 @@ module PgGraphQl
825
835
  assert_equal token(<<-SQL
826
836
  select 'user'::text as key,
827
837
  (select to_json(x.*)
828
- from (select users.id,
829
- email,
838
+ from (select users1.id,
839
+ users1.email as email,
830
840
  (select to_json(x.*)
831
- from (select addresses.id,
841
+ from (select addresses2.id,
832
842
  (select to_json(x.*)
833
- from (select countries.id
834
- from countries
835
- where (id = addresses.country_id) limit 1) x) as "country"
836
- from addresses
837
- where (user_id = users.id) limit 1) x) as "address"
838
- from users
839
- where users.id = ? limit 1) x) as value
843
+ from (select countries3.id
844
+ from countries as countries3
845
+ where (countries3.id = addresses2.country_id) limit 1) x) as "country"
846
+ from addresses as addresses2
847
+ where (addresses2.user_id = users1.id) limit 1) x) as "address"
848
+ from users as users1
849
+ where users1.id = ? limit 1) x) as value
840
850
  SQL
841
851
  ), token(res[:sql])
842
852
  assert_equal [1], res[:params]
843
853
  end
844
854
 
845
- #####################
846
- # many
847
- #####################
855
+ # #####################
856
+ # # many
857
+ # #####################
848
858
 
849
859
 
850
860
  def test_link_many
851
861
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
852
862
  s.root :user
853
863
  s.type :user, fields: [:email] do |t|
854
- t.many :address, fk: "user_id = users.id"
864
+ t.many :address
855
865
  end
856
866
  s.type :address
857
867
  end
@@ -859,14 +869,14 @@ module PgGraphQl
859
869
  assert_equal token(<<-SQL
860
870
  select 'user'::text as key,
861
871
  (select to_json(x.*)
862
- from (select users.id,
863
- email,
872
+ from (select users1.id,
873
+ users1.email as email,
864
874
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
865
- from (select addresses.id
866
- from addresses
867
- where addresses.id = ? and (user_id = users.id)) x) as "address"
868
- from users
869
- where users.id = ? limit 1) x) as value
875
+ from (select addresses2.id
876
+ from addresses as addresses2
877
+ where addresses2.id = ? and (addresses2.user_id = users1.id)) x) as "address"
878
+ from users as users1
879
+ where users1.id = ? limit 1) x) as value
870
880
  SQL
871
881
  ), token(res[:sql])
872
882
  assert_equal ["99", 1], res[:params]
@@ -876,7 +886,7 @@ module PgGraphQl
876
886
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
877
887
  s.root :user
878
888
  s.type :user, fields: [:email] do |t|
879
- t.many :address, fk: :many
889
+ t.many :address
880
890
  end
881
891
  s.type :address
882
892
  end
@@ -884,14 +894,14 @@ module PgGraphQl
884
894
  assert_equal token(<<-SQL
885
895
  select 'user'::text as key,
886
896
  (select to_json(x.*)
887
- from (select users.id,
888
- email,
897
+ from (select users1.id,
898
+ users1.email as email,
889
899
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
890
- from (select addresses.id
891
- from addresses
892
- where addresses.id = ? and (addresses.user_id = users.id)) x) as "address"
893
- from users
894
- where users.id = ? limit 1) x) as value
900
+ from (select addresses2.id
901
+ from addresses as addresses2
902
+ where addresses2.id = ? and (addresses2.user_id = users1.id)) x) as "address"
903
+ from users as users1
904
+ where users1.id = ? limit 1) x) as value
895
905
  SQL
896
906
  ), token(res[:sql])
897
907
  assert_equal ["99", 1], res[:params]
@@ -901,7 +911,7 @@ module PgGraphQl
901
911
  res = to_sql({user: {id: 1, email: "email", other_addresses: {id: "99"}}}) do |s|
902
912
  s.root :user
903
913
  s.type :user, fields: [:email] do |t|
904
- t.many :other_addresses, type: :address, fk: :many
914
+ t.many :other_addresses, type: :address
905
915
  end
906
916
  s.type :address
907
917
  end
@@ -909,14 +919,14 @@ module PgGraphQl
909
919
  assert_equal token(<<-SQL
910
920
  select 'user'::text as key,
911
921
  (select to_json(x.*)
912
- from (select users.id,
913
- email,
922
+ from (select users1.id,
923
+ users1.email as email,
914
924
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
915
- from (select addresses.id
916
- from addresses
917
- where addresses.id = ? and (addresses.user_id = users.id)) x) as "other_addresses"
918
- from users
919
- where users.id = ? limit 1) x) as value
925
+ from (select addresses2.id
926
+ from addresses as addresses2
927
+ where addresses2.id = ? and (addresses2.user_id = users1.id)) x) as "other_addresses"
928
+ from users as users1
929
+ where users1.id = ? limit 1) x) as value
920
930
  SQL
921
931
  ), token(res[:sql])
922
932
  assert_equal ["99", 1], res[:params]
@@ -926,7 +936,7 @@ module PgGraphQl
926
936
  res = to_sql({user: {id: 1, email: "email", address: {id: ["99","999"]}}}) do |s|
927
937
  s.root :user
928
938
  s.type :user, fields: [:email] do |t|
929
- t.many :address, fk: "user_id = users.id"
939
+ t.many :address
930
940
  end
931
941
  s.type :address
932
942
  end
@@ -934,14 +944,14 @@ module PgGraphQl
934
944
  assert_equal token(<<-SQL
935
945
  select 'user'::text as key,
936
946
  (select to_json(x.*)
937
- from (select users.id,
938
- email,
947
+ from (select users1.id,
948
+ users1.email as email,
939
949
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
940
- from (select addresses.id
941
- from addresses
942
- where addresses.id in ? and (user_id = users.id)) x) as "address"
943
- from users
944
- where users.id = ? limit 1) x) as value
950
+ from (select addresses2.id
951
+ from addresses as addresses2
952
+ where addresses2.id in ? and (addresses2.user_id = users1.id)) x) as "address"
953
+ from users as users1
954
+ where users1.id = ? limit 1) x) as value
945
955
  SQL
946
956
  ), token(res[:sql])
947
957
  assert_equal [["99","999"], 1], res[:params]
@@ -951,7 +961,7 @@ module PgGraphQl
951
961
  res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
952
962
  s.root :user
953
963
  s.type :user, fields: [:email] do |t|
954
- t.many :address, fk: "user_id = users.id"
964
+ t.many :address
955
965
  end
956
966
  s.type :address
957
967
  end
@@ -959,14 +969,14 @@ module PgGraphQl
959
969
  assert_equal token(<<-SQL
960
970
  select 'user'::text as key,
961
971
  (select to_json(x.*)
962
- from (select users.id,
963
- email,
972
+ from (select users1.id,
973
+ users1.email as email,
964
974
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
965
- from (select addresses.id
966
- from addresses
967
- where (user_id = users.id)) x) as "address"
968
- from users
969
- where users.id = ? limit 1) x) as value
975
+ from (select addresses2.id
976
+ from addresses as addresses2
977
+ where (addresses2.user_id = users1.id)) x) as "address"
978
+ from users as users1
979
+ where users1.id = ? limit 1) x) as value
970
980
  SQL
971
981
  ), token(res[:sql])
972
982
  assert_equal [1], res[:params]
@@ -976,7 +986,7 @@ module PgGraphQl
976
986
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
977
987
  s.root :user
978
988
  s.type :user, fields: [:email] do |t|
979
- t.many :address, fk: "user_id = users.id", filter: "id % 2 = 0"
989
+ t.many :address, filter: "id % 2 = 0"
980
990
  end
981
991
  s.type :address
982
992
  end
@@ -984,14 +994,14 @@ module PgGraphQl
984
994
  assert_equal token(<<-SQL
985
995
  select 'user'::text as key,
986
996
  (select to_json(x.*)
987
- from (select users.id,
988
- email,
997
+ from (select users1.id,
998
+ users1.email as email,
989
999
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
990
- from (select addresses.id
991
- from addresses
992
- where addresses.id = ? and (user_id = users.id) and (id % 2 = 0)) x) as "address"
993
- from users
994
- where users.id = ? limit 1) x) as value
1000
+ from (select addresses2.id
1001
+ from addresses as addresses2
1002
+ where addresses2.id = ? and (addresses2.user_id = users1.id) and (id % 2 = 0)) x) as "address"
1003
+ from users as users1
1004
+ where users1.id = ? limit 1) x) as value
995
1005
  SQL
996
1006
  ), token(res[:sql])
997
1007
  assert_equal ["99", 1], res[:params]
@@ -1001,7 +1011,7 @@ module PgGraphQl
1001
1011
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
1002
1012
  s.root :user
1003
1013
  s.type :user, fields: [:email] do |t|
1004
- t.many :address, fk: "user_id = users.id", order_by: "id desc"
1014
+ t.many :address, order_by: "id desc"
1005
1015
  end
1006
1016
  s.type :address
1007
1017
  end
@@ -1009,14 +1019,14 @@ module PgGraphQl
1009
1019
  assert_equal token(<<-SQL
1010
1020
  select 'user'::text as key,
1011
1021
  (select to_json(x.*)
1012
- from (select users.id,
1013
- email,
1022
+ from (select users1.id,
1023
+ users1.email as email,
1014
1024
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
1015
- from (select addresses.id
1016
- from addresses
1017
- where addresses.id = ? and (user_id = users.id) order by id desc) x) as "address"
1018
- from users
1019
- where users.id = ? limit 1) x) as value
1025
+ from (select addresses2.id
1026
+ from addresses as addresses2
1027
+ where addresses2.id = ? and (addresses2.user_id = users1.id) order by id desc) x) as "address"
1028
+ from users as users1
1029
+ where users1.id = ? limit 1) x) as value
1020
1030
  SQL
1021
1031
  ), token(res[:sql])
1022
1032
  assert_equal ["99", 1], res[:params]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pggraphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek