pggraphql 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60ab99d71efd2955788a1616ec4c8a3ebcbe971a
4
- data.tar.gz: ec9403901a2fa867d64e08954ff2a69a181df330
3
+ metadata.gz: 740c71df80baf59979c79d246a8956a8381bada1
4
+ data.tar.gz: 86c5e60636bc5f3d28c210f096ed7684f2a62905
5
5
  SHA512:
6
- metadata.gz: 79b377023aa0b3df1297fce755f8a7139ea41055f053099f2d30e1949789e8b7ea34f62fcb68a2b540d63d547d92baef62e8830134ece865f47863f08793c7f2
7
- data.tar.gz: 07f8413aaf1fc7ea03c7f82db48330d7bd18f561c41f7e1f8985e2f373a4b13d8526064045967245b2c4cc9854db20684766744f70f5aaef19265bf02d050dae
6
+ metadata.gz: 40b4f96fc5588dacf2ee4d5810f654f54bfc16c36139bba646dfad743488429712b07c48cdcc7cd3d60e31be29d6a64222734b9cf8078c928b20c1aad8b4fb15
7
+ data.tar.gz: 8e073df1c7d2f6d7050520b843e1999256a13e58eed043d17b97398d132959e7ba7047673c727380bb115715d06484f6519049d4fabc7ad1800c5ec2b18dd8ec
@@ -1,3 +1,3 @@
1
1
  module Pggraphql
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
data/lib/pggraphql.rb CHANGED
@@ -45,17 +45,32 @@ module PgGraphQl
45
45
  nested_link_name = f[0]
46
46
  field_name = f[0]
47
47
 
48
- raise "unknown field #{field_name.inspect} on type #{type.name.inspect}" if !f[1].is_a?(Hash) && !type.fields.include?(field_name)
48
+ raise "unknown field #{field_name.inspect} on type #{type.name.inspect}" if !f[1].is_a?(Hash) && !type.fields.detect{|f| f[:name] == field_name}
49
49
  raise "unknown link #{field_name.inspect} on type #{type.name.inspect}" if f[1].is_a?(Hash) && !type.links.include?(field_name)
50
50
 
51
- column_name = field_name.to_s.index("__") ? field_name.to_s.gsub(/__/, ".").to_sym : field_name
51
+ if f[1].is_a?(Hash)
52
+ "(" + to_sql([f].to_h, level + 1, type, nested_link_name) + ") as #{field_name}"
53
+ else
54
+ field_def = type.fields.detect{|f| f[:name] == field_name}
52
55
 
53
- column_expr = type.mappings[field_name] || column_name
56
+ column_name = field_def[:name].to_s.index("__") ? field_def[:name].to_s.gsub(/__/, ".").to_sym : field_def[:name]
57
+ # column_expr = type.mappings[field_name] || column_name
58
+
59
+ column_expr = if field_def[:expr]
60
+ field_def[:expr].call(column_name)
61
+ else
62
+ column_name
63
+ end
64
+
65
+ if (column_name == field_name && column_name == column_expr)
66
+ column_name.to_s
67
+ else
68
+ "#{column_expr}" + (field_def[:as] ? " as #{field_def[:as]}" : "")
69
+ end
70
+ end
54
71
 
55
- (f[1].is_a?(Hash) ? "(" + to_sql([f].to_h, level + 1, type, nested_link_name) + ") as #{field_name}" : ((column_name == field_name && column_name == column_expr) ? column_name.to_s : "#{column_expr} as #{field_name}"))
56
72
  end.join(",")
57
73
 
58
- # is_many = (link && link.many?) || ids.is_a?(Array) || (level == 1 && !ids && type.null_pk == :array)
59
74
  is_many = (link && link.many?) || (level == 1 && ids.is_a?(Array)) || (level == 1 && !ids && type.null_pk == :array)
60
75
  order_by = link.try(:order_by) || type.try(:order_by)
61
76
 
@@ -117,10 +132,9 @@ module PgGraphQl
117
132
  @order_by = nil
118
133
  @links = {}
119
134
  @subtypes = {}
120
- @mappings = {}
121
135
  @null_pk = false
122
136
  @pk = ->(ids, level) do
123
- id_column = @mappings[:id] || "id"
137
+ id_column = "#{@table}.id"
124
138
  if ids.is_a?(Array)
125
139
  "#{id_column} in (" + ids.map{|id| id.is_a?(String) ? "'#{id}'" : id.to_s}.join(',') + ")"
126
140
  else
@@ -129,14 +143,26 @@ module PgGraphQl
129
143
  end
130
144
  end
131
145
  def fields=(fields)
132
- @fields = fields
146
+ fields.each do |f|
147
+ raise "do not add :id in fields; it will be added automatically" if f == :id || (f.is_a?(Hash) && f[:name] == :id)
148
+ end
149
+ @fields = fields.map{|f| create_field(f)}
133
150
  end
134
151
  def fields
135
- @fields + [:id] + (@subtypes.empty? ? [] : [:type])
152
+ @fields + [create_field({name: :id, as: nil, expr: ->(c){ "#{@table}.#{c}" }})] + (@subtypes.empty? ? [] : [create_field(:type)])
136
153
  end
137
- def map(field, column_expr)
138
- @mappings[field] = column_expr
154
+ def create_field(field)
155
+ if field.is_a?(Symbol)
156
+ {name: field, as: field}
157
+ elsif field.is_a?(Hash)
158
+ raise "missing field :name #{field.inspect}" unless field[:name]
159
+ field[:as] = field[:name] unless field.key?(:as)
160
+ field
161
+ else
162
+ raise "unsupported field #{field.inspect}"
163
+ end
139
164
  end
165
+
140
166
  def one(name, opts={})
141
167
  create_link(name, false, opts)
142
168
  end
@@ -23,16 +23,16 @@ module PgGraphQl
23
23
  def test_simple
24
24
  res = to_sql({user: {id: 1, email: "email"}}) do |s|
25
25
  s.root :user
26
- s.type :user, fields: [:id, :email]
26
+ s.type :user, fields: [:email]
27
27
  end
28
28
 
29
29
  assert_equal token(<<-SQL
30
30
  select 'user'::text as key,
31
31
  (select to_json(x.*)
32
- from (select id,
32
+ from (select users.id,
33
33
  email
34
34
  from users
35
- where id = 1 limit 1) x) as value
35
+ where users.id = 1 limit 1) x) as value
36
36
  SQL
37
37
  ), token(res)
38
38
 
@@ -46,10 +46,10 @@ module PgGraphQl
46
46
  assert_equal token(<<-SQL
47
47
  select 'user'::text as key,
48
48
  (select to_json(x.*)
49
- from (select id,
49
+ from (select users.id,
50
50
  email
51
51
  from users
52
- where id = 1 limit 1) x) as value
52
+ where users.id = 1 limit 1) x) as value
53
53
  SQL
54
54
  ), token(res)
55
55
 
@@ -63,7 +63,7 @@ module PgGraphQl
63
63
  assert_equal token(<<-SQL
64
64
  select 'user'::text as key,
65
65
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
66
- from (select id,
66
+ from (select users.id,
67
67
  email
68
68
  from users) x) as value
69
69
  SQL
@@ -73,6 +73,15 @@ module PgGraphQl
73
73
  def test_simple_fail_when_accessing_non_root
74
74
  assert_raise_message ":user is not a root type" do
75
75
  res = to_sql({user: {id: 1, email: "email"}}) do |s|
76
+ s.type :user, fields: [:email]
77
+ end
78
+ end
79
+ end
80
+
81
+ def test_simple_fail_when_pass_id_field
82
+ assert_raise_message "do not add :id in fields; it will be added automatically" do
83
+ res = to_sql({user: {id: 1, email: "email"}}) do |s|
84
+ s.root :user
76
85
  s.type :user, fields: [:id, :email]
77
86
  end
78
87
  end
@@ -82,14 +91,14 @@ module PgGraphQl
82
91
  assert_raise_message "missing :id for root type :user" do
83
92
  res = to_sql({user: {email: "email"}}) do |s|
84
93
  s.root :user
85
- s.type :user, fields: [:id, :email]
94
+ s.type :user, fields: [:email]
86
95
  end
87
96
  end
88
97
 
89
98
  assert_raise_message "found empty :id array on type :user" do
90
99
  res = to_sql({user: {id: [], email: "email"}}) do |s|
91
100
  s.root :user
92
- s.type :user, fields: [:id, :email]
101
+ s.type :user, fields: [:email]
93
102
  end
94
103
  end
95
104
  end
@@ -103,7 +112,7 @@ module PgGraphQl
103
112
  assert_equal token(<<-SQL
104
113
  select 'user'::text as key,
105
114
  (select to_json(x.*)
106
- from (select id,
115
+ from (select users.id,
107
116
  email
108
117
  from users where access_token = '1' limit 1) x) as value
109
118
  SQL
@@ -119,7 +128,7 @@ module PgGraphQl
119
128
  assert_equal token(<<-SQL
120
129
  select 'user'::text as key,
121
130
  (select to_json(x.*)
122
- from (select id,
131
+ from (select users.id,
123
132
  email
124
133
  from users where level1 = '99' limit 1) x) as value
125
134
  SQL
@@ -135,12 +144,13 @@ module PgGraphQl
135
144
  assert_equal token(<<-SQL
136
145
  select 'user'::text as key,
137
146
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
138
- from (select id,
147
+ from (select users.id,
139
148
  email
140
- from users where id in ('1')) x) as value
149
+ from users where users.id in ('1')) x) as value
141
150
  SQL
142
151
  ), token(res)
143
152
 
153
+ # ---
144
154
 
145
155
  res = to_sql({user: {id: "1", email: "email"}}) do |s|
146
156
  s.root :user
@@ -150,9 +160,9 @@ module PgGraphQl
150
160
  assert_equal token(<<-SQL
151
161
  select 'user'::text as key,
152
162
  (select to_json(x.*)
153
- from (select id,
163
+ from (select users.id,
154
164
  email
155
- from users where id = '1' limit 1) x) as value
165
+ from users where users.id = '1' limit 1) x) as value
156
166
  SQL
157
167
  ), token(res)
158
168
  end
@@ -160,15 +170,15 @@ module PgGraphQl
160
170
  def test_simple_pk_array_one
161
171
  res = to_sql({user: {id: [1], email: "email"}}) do |s|
162
172
  s.root :user
163
- s.type :user, fields: [:id, :email]
173
+ s.type :user, fields: [:email]
164
174
  end
165
175
 
166
176
  assert_equal token(<<-SQL
167
177
  select 'user'::text as key,
168
178
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
169
- from (select id,
179
+ from (select users.id,
170
180
  email
171
- from users where id in (1)) x) as value
181
+ from users where users.id in (1)) x) as value
172
182
  SQL
173
183
  ), token(res)
174
184
  end
@@ -176,15 +186,15 @@ module PgGraphQl
176
186
  def test_simple_pk_array_multiple
177
187
  res = to_sql({user: {id: [1,2], email: "email"}}) do |s|
178
188
  s.root :user
179
- s.type :user, fields: [:id, :email]
189
+ s.type :user, fields: [:email]
180
190
  end
181
191
 
182
192
  assert_equal token(<<-SQL
183
193
  select 'user'::text as key,
184
194
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
185
- from (select id,
195
+ from (select users.id,
186
196
  email
187
- from users where id in (1,2)) x) as value
197
+ from users where users.id in (1,2)) x) as value
188
198
  SQL
189
199
  ), token(res)
190
200
 
@@ -192,15 +202,15 @@ module PgGraphQl
192
202
 
193
203
  res = to_sql({user: {id: ['1','2'], email: "email"}}) do |s|
194
204
  s.root :user
195
- s.type :user, fields: [:id, :email]
205
+ s.type :user, fields: [:email]
196
206
  end
197
207
 
198
208
  assert_equal token(<<-SQL
199
209
  select 'user'::text as key,
200
210
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
201
- from (select id,
211
+ from (select users.id,
202
212
  email
203
- from users where id in ('1','2')) x) as value
213
+ from users where users.id in ('1','2')) x) as value
204
214
  SQL
205
215
  ), token(res)
206
216
  end
@@ -208,16 +218,16 @@ module PgGraphQl
208
218
  def test_simple_filter
209
219
  res = to_sql({user: {id: 1, email: "email"}}) do |s|
210
220
  s.root :user
211
- s.type :user, fields: [:id, :email], filter: "id > 100"
221
+ s.type :user, fields: [:email], filter: "id > 100"
212
222
  end
213
223
 
214
224
  assert_equal token(<<-SQL
215
225
  select 'user'::text as key,
216
226
  (select to_json(x.*)
217
- from (select id,
227
+ from (select users.id,
218
228
  email
219
229
  from users
220
- where id = 1 and (id > 100) limit 1) x) as value
230
+ where users.id = 1 and (id > 100) limit 1) x) as value
221
231
  SQL
222
232
  ), token(res)
223
233
  end
@@ -226,26 +236,86 @@ module PgGraphQl
226
236
  res = to_sql({user: {id: 1, email: "email"}, educator: {id: 99}}) do |s|
227
237
  s.root :user
228
238
  s.root :educator
229
- s.type :user, fields: [:id, :email]
239
+ s.type :user, fields: [:email]
230
240
  s.type :educator, null_pk: true
231
241
  end
232
242
 
233
243
  assert_equal token(<<-SQL
234
244
  select 'user'::text as key,
235
245
  (select to_json(x.*)
236
- from (select id,
246
+ from (select users.id,
237
247
  email
238
248
  from users
239
- where id = 1 limit 1) x) as value
249
+ where users.id = 1 limit 1) x) as value
240
250
  union all
241
251
  select 'educator'::text as key,
242
252
  (select to_json(x.*)
243
- from (select id
244
- from educators where id = 99 limit 1) x) as value
253
+ from (select educators.id
254
+ from educators where educators.id = 99 limit 1) x) as value
245
255
  SQL
246
256
  ), token(res)
247
257
  end
248
258
 
259
+ def test_simple_strange_nested_to_json_for_json_datatype_with_column_alias
260
+ # fails: select 'flow'::text as key, (select to_json(x.*) from (select id, data from flows where id = '1' limit 1) x) as value;
261
+ # fine: select 'flow'::text as key, (select to_json(x.*) from (select id, to_json(data) from flows where id = '1' limit 1) x) as value;
262
+
263
+ res = to_sql({flow: {id: 1, data: nil}}) do |s|
264
+ s.root :flow
265
+ s.type :flow do |t|
266
+ t.fields = [{name: :data}]
267
+ end
268
+ end
269
+
270
+ assert_equal token(<<-SQL
271
+ select 'flow'::text as key, (select to_json(x.*) from (select flows.id, data from flows where flows.id = 1 limit 1) x) as value
272
+ SQL
273
+ ), token(res)
274
+
275
+ # ------
276
+
277
+ res = to_sql({flow: {id: 1, data: nil}}) do |s|
278
+ s.root :flow
279
+ s.type :flow do |t|
280
+ t.fields = [{name: :data, as: nil}]
281
+ end
282
+ end
283
+
284
+ assert_equal token(<<-SQL
285
+ select 'flow'::text as key, (select to_json(x.*) from (select flows.id, data from flows where flows.id = 1 limit 1) x) as value
286
+ SQL
287
+ ), token(res)
288
+
289
+ # ------
290
+
291
+ res = to_sql({flow: {id: 1, data: nil}}) do |s|
292
+ s.root :flow
293
+ s.type :flow do |t|
294
+ t.fields = [{name: :data, expr: ->(c){ "to_json(#{c})" } }]
295
+ end
296
+ end
297
+
298
+ assert_equal token(<<-SQL
299
+ select 'flow'::text as key, (select to_json(x.*) from (select flows.id, to_json(data) as data from flows where flows.id = 1 limit 1) x) as value
300
+ SQL
301
+ ), token(res)
302
+
303
+ # ------ positive check
304
+
305
+ res = to_sql({flow: {id: 1, data: nil}}) do |s|
306
+ s.root :flow
307
+ s.type :flow do |t|
308
+ t.fields = [{name: :data, as: nil, expr: ->(c){ "to_json(#{c})" } }]
309
+ end
310
+ end
311
+
312
+ assert_equal token(<<-SQL
313
+ select 'flow'::text as key, (select to_json(x.*) from (select flows.id, to_json(data) from flows where flows.id = 1 limit 1) x) as value
314
+ SQL
315
+ ), token(res)
316
+
317
+ end
318
+
249
319
  #####################
250
320
  # inherit
251
321
  #####################
@@ -263,7 +333,7 @@ module PgGraphQl
263
333
  }) do |s|
264
334
  s.root :product
265
335
 
266
- s.type :user, fields: [:id, :email] do |t|
336
+ s.type :user, fields: [:email] do |t|
267
337
  t.many :orders, fk: "user_id = users.id"
268
338
  end
269
339
 
@@ -271,8 +341,6 @@ module PgGraphQl
271
341
 
272
342
  s.type :product, null_pk: :array, fields: [:type, :clickout__destination_url, :download__download_url] do |t|
273
343
 
274
- t.map :id, "products.id"
275
-
276
344
  t.subtype :download, table: :product_downloads, fk: "download.id = products.id and products.type = 'download'"
277
345
  t.subtype :clickout, table: :product_clickouts, fk: "clickout.id = products.id and products.type = 'clickout'"
278
346
 
@@ -284,14 +352,14 @@ module PgGraphQl
284
352
  assert_equal token(<<-SQL
285
353
  select 'products'::text as key,
286
354
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
287
- from (select products.id as id,
355
+ from (select products.id,
288
356
  type,
289
357
  clickout.destination_url as clickout__destination_url,
290
358
  download.download_url as download__download_url,
291
359
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
292
- from (select id,
360
+ from (select users.id,
293
361
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
294
- from (select id
362
+ from (select orders.id
295
363
  from orders
296
364
  where (user_id = users.id)) x) as orders
297
365
  from users
@@ -302,8 +370,7 @@ module PgGraphQl
302
370
  left join product_clickouts as clickout on (clickout.id = products.id
303
371
  and products.type = 'clickout')) x) as value
304
372
  SQL
305
- ), token(res)
306
-
373
+ ), token(res)
307
374
  end
308
375
 
309
376
  def test_inherit_with_pk
@@ -315,7 +382,6 @@ module PgGraphQl
315
382
  }) do |s|
316
383
  s.root :product
317
384
  s.type :product, null_pk: :array, fields: [:clickout__destination_url, :download__download_url] do |t|
318
- t.map :id, "products.id"
319
385
  t.subtype :clickout, table: :product_clickouts, fk: "clickout.id = products.id and products.type = 'clickout'"
320
386
  end
321
387
  end
@@ -323,7 +389,7 @@ module PgGraphQl
323
389
  assert_equal token(<<-SQL
324
390
  select 'products'::text as key,
325
391
  (select to_json(x.*)
326
- from (select products.id as id, type,
392
+ from (select products.id, type,
327
393
  clickout.destination_url as clickout__destination_url
328
394
  from products
329
395
  left join product_clickouts as clickout on (clickout.id = products.id
@@ -341,7 +407,7 @@ module PgGraphQl
341
407
  def test_link_one
342
408
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
343
409
  s.root :user
344
- s.type :user, fields: [:id, :email] do |t|
410
+ s.type :user, fields: [:email] do |t|
345
411
  t.one :address, fk: "id = users.address_id"
346
412
  end
347
413
  s.type :address
@@ -350,14 +416,14 @@ module PgGraphQl
350
416
  assert_equal token(<<-SQL
351
417
  select 'user'::text as key,
352
418
  (select to_json(x.*)
353
- from (select id,
419
+ from (select users.id,
354
420
  email,
355
421
  (select to_json(x.*)
356
- from (select id
422
+ from (select addresses.id
357
423
  from addresses
358
- where id = '99' and (id = users.address_id) limit 1) x) as address
424
+ where addresses.id = '99' and (id = users.address_id) limit 1) x) as address
359
425
  from users
360
- where id = 1 limit 1) x) as value
426
+ where users.id = 1 limit 1) x) as value
361
427
  SQL
362
428
  ), token(res)
363
429
  end
@@ -365,7 +431,7 @@ module PgGraphQl
365
431
  def test_link_one_nested_pk
366
432
  res = to_sql({user: {id: 1, email: "email", address: {id: 99}}}) do |s|
367
433
  s.root :user
368
- s.type :user, fields: [:id, :email] do |t|
434
+ s.type :user, fields: [:email] do |t|
369
435
  t.one :address, fk: "id = users.address_id"
370
436
  end
371
437
  s.type :address
@@ -374,20 +440,20 @@ module PgGraphQl
374
440
  assert_equal token(<<-SQL
375
441
  select 'user'::text as key,
376
442
  (select to_json(x.*)
377
- from (select id,
443
+ from (select users.id,
378
444
  email,
379
445
  (select to_json(x.*)
380
- from (select id
446
+ from (select addresses.id
381
447
  from addresses
382
- where id = 99 and (id = users.address_id) limit 1) x) as address
448
+ where addresses.id = 99 and (id = users.address_id) limit 1) x) as address
383
449
  from users
384
- where id = 1 limit 1) x) as value
450
+ where users.id = 1 limit 1) x) as value
385
451
  SQL
386
452
  ), token(res)
387
453
 
388
454
  res = to_sql({user: {id: 1, email: "email", address: {id: [99,999]}}}) do |s|
389
455
  s.root :user
390
- s.type :user, fields: [:id, :email] do |t|
456
+ s.type :user, fields: [:email] do |t|
391
457
  t.one :address, fk: "id = users.address_id"
392
458
  end
393
459
  s.type :address
@@ -396,16 +462,17 @@ module PgGraphQl
396
462
  assert_equal token(<<-SQL
397
463
  select 'user'::text as key,
398
464
  (select to_json(x.*)
399
- from (select id,
465
+ from (select users.id,
400
466
  email,
401
467
  (select to_json(x.*)
402
- from (select id
468
+ from (select addresses.id
403
469
  from addresses
404
- where id in (99,999) and (id = users.address_id) limit 1) x) as address
470
+ where addresses.id in (99,999) and (id = users.address_id) limit 1) x) as address
405
471
  from users
406
- where id = 1 limit 1) x) as value
472
+ where users.id = 1 limit 1) x) as value
407
473
  SQL
408
- ), token(res) end
474
+ ), token(res)
475
+ end
409
476
 
410
477
  def test_link_one_empty_fields
411
478
  res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
@@ -419,14 +486,14 @@ module PgGraphQl
419
486
  assert_equal token(<<-SQL
420
487
  select 'user'::text as key,
421
488
  (select to_json(x.*)
422
- from (select id,
489
+ from (select users.id,
423
490
  email,
424
491
  (select to_json(x.*)
425
- from (select id
492
+ from (select addresses.id
426
493
  from addresses
427
494
  where (id = users.address_id) limit 1) x) as address
428
495
  from users
429
- where id = 1 limit 1) x) as value
496
+ where users.id = 1 limit 1) x) as value
430
497
  SQL
431
498
  ), token(res)
432
499
  end
@@ -435,7 +502,7 @@ module PgGraphQl
435
502
  assert_raise_message "missing :fk on link :address" do
436
503
  to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
437
504
  s.root :user
438
- s.type :user, fields: [:id, :email] do |t|
505
+ s.type :user, fields: [:email] do |t|
439
506
  t.one :address
440
507
  end
441
508
  s.type :address
@@ -447,7 +514,7 @@ module PgGraphQl
447
514
  def test_link_one_fk_sql
448
515
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
449
516
  s.root :user
450
- s.type :user, fields: [:id, :email] do |t|
517
+ s.type :user, fields: [:email] do |t|
451
518
  t.one :address, fk: "id = (select 100)"
452
519
  end
453
520
  s.type :address
@@ -456,14 +523,14 @@ module PgGraphQl
456
523
  assert_equal token(<<-SQL
457
524
  select 'user'::text as key,
458
525
  (select to_json(x.*)
459
- from (select id,
526
+ from (select users.id,
460
527
  email,
461
528
  (select to_json(x.*)
462
- from (select id
529
+ from (select addresses.id
463
530
  from addresses
464
- where id = '99' and (id = (select 100)) limit 1) x) as address
531
+ where addresses.id = '99' and (id = (select 100)) limit 1) x) as address
465
532
  from users
466
- where id = 1 limit 1) x) as value
533
+ where users.id = 1 limit 1) x) as value
467
534
  SQL
468
535
  ), token(res)
469
536
  end
@@ -471,7 +538,7 @@ module PgGraphQl
471
538
  def test_link_one_filter
472
539
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
473
540
  s.root :user
474
- s.type :user, fields: [:id, :email] do |t|
541
+ s.type :user, fields: [:email] do |t|
475
542
  t.one :address, fk: "user_id = users.id", filter: "id > 100"
476
543
  end
477
544
  s.type :address
@@ -480,14 +547,14 @@ module PgGraphQl
480
547
  assert_equal token(<<-SQL
481
548
  select 'user'::text as key,
482
549
  (select to_json(x.*)
483
- from (select id,
550
+ from (select users.id,
484
551
  email,
485
552
  (select to_json(x.*)
486
- from (select id
553
+ from (select addresses.id
487
554
  from addresses
488
- where id = '99' and (user_id = users.id) and (id > 100) limit 1) x) as address
555
+ where addresses.id = '99' and (user_id = users.id) and (id > 100) limit 1) x) as address
489
556
  from users
490
- where id = 1 limit 1) x) as value
557
+ where users.id = 1 limit 1) x) as value
491
558
  SQL
492
559
  ), token(res)
493
560
  end
@@ -495,7 +562,7 @@ module PgGraphQl
495
562
  def test_link_one_order_by
496
563
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
497
564
  s.root :user
498
- s.type :user, fields: [:id, :email] do |t|
565
+ s.type :user, fields: [:email] do |t|
499
566
  t.one :address, fk: "user_id = users.id", order_by: "id desc"
500
567
  end
501
568
  s.type :address
@@ -504,14 +571,14 @@ module PgGraphQl
504
571
  assert_equal token(<<-SQL
505
572
  select 'user'::text as key,
506
573
  (select to_json(x.*)
507
- from (select id,
574
+ from (select users.id,
508
575
  email,
509
576
  (select to_json(x.*)
510
- from (select id
577
+ from (select addresses.id
511
578
  from addresses
512
- where id = '99' and (user_id = users.id) order by id desc limit 1) x) as address
579
+ where addresses.id = '99' and (user_id = users.id) order by id desc limit 1) x) as address
513
580
  from users
514
- where id = 1 limit 1) x) as value
581
+ where users.id = 1 limit 1) x) as value
515
582
  SQL
516
583
  ), token(res)
517
584
  end
@@ -523,7 +590,7 @@ module PgGraphQl
523
590
  def test_link_one_in_one
524
591
  res = to_sql({user: {id: 1, email: "email", address: {country: {}}}}) do |s|
525
592
  s.root :user
526
- s.type :user, fields: [:id, :email] do |t|
593
+ s.type :user, fields: [:email] do |t|
527
594
  t.one :address, fk: "user_id = users.id"
528
595
  end
529
596
  s.type :address do |t|
@@ -535,31 +602,31 @@ module PgGraphQl
535
602
  assert_equal token(<<-SQL
536
603
  select 'user'::text as key,
537
604
  (select to_json(x.*)
538
- from (select id,
605
+ from (select users.id,
539
606
  email,
540
607
  (select to_json(x.*)
541
- from (select id,
608
+ from (select addresses.id,
542
609
  (select to_json(x.*)
543
- from (select id
610
+ from (select countries.id
544
611
  from countries
545
612
  where (id = addresses.country_id) limit 1) x) as country
546
613
  from addresses
547
614
  where (user_id = users.id) limit 1) x) as address
548
615
  from users
549
- where id = 1 limit 1) x) as value
616
+ where users.id = 1 limit 1) x) as value
550
617
  SQL
551
618
  ), token(res)
552
619
  end
553
620
 
554
- #####################
555
- # many
556
- #####################
621
+ # #####################
622
+ # # many
623
+ # #####################
557
624
 
558
625
 
559
626
  def test_link_many
560
627
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
561
628
  s.root :user
562
- s.type :user, fields: [:id, :email] do |t|
629
+ s.type :user, fields: [:email] do |t|
563
630
  t.many :address, fk: "user_id = users.id"
564
631
  end
565
632
  s.type :address
@@ -568,14 +635,14 @@ module PgGraphQl
568
635
  assert_equal token(<<-SQL
569
636
  select 'user'::text as key,
570
637
  (select to_json(x.*)
571
- from (select id,
638
+ from (select users.id,
572
639
  email,
573
640
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
574
- from (select id
641
+ from (select addresses.id
575
642
  from addresses
576
- where id = '99' and (user_id = users.id)) x) as address
643
+ where addresses.id = '99' and (user_id = users.id)) x) as address
577
644
  from users
578
- where id = 1 limit 1) x) as value
645
+ where users.id = 1 limit 1) x) as value
579
646
  SQL
580
647
  ), token(res)
581
648
  end
@@ -583,7 +650,7 @@ module PgGraphQl
583
650
  def test_link_many_nested_pk
584
651
  res = to_sql({user: {id: 1, email: "email", address: {id: ["99","999"]}}}) do |s|
585
652
  s.root :user
586
- s.type :user, fields: [:id, :email] do |t|
653
+ s.type :user, fields: [:email] do |t|
587
654
  t.many :address, fk: "user_id = users.id"
588
655
  end
589
656
  s.type :address
@@ -592,14 +659,14 @@ module PgGraphQl
592
659
  assert_equal token(<<-SQL
593
660
  select 'user'::text as key,
594
661
  (select to_json(x.*)
595
- from (select id,
662
+ from (select users.id,
596
663
  email,
597
664
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
598
- from (select id
665
+ from (select addresses.id
599
666
  from addresses
600
- where id in ('99','999') and (user_id = users.id)) x) as address
667
+ where addresses.id in ('99','999') and (user_id = users.id)) x) as address
601
668
  from users
602
- where id = 1 limit 1) x) as value
669
+ where users.id = 1 limit 1) x) as value
603
670
  SQL
604
671
  ), token(res)
605
672
  end
@@ -607,7 +674,7 @@ module PgGraphQl
607
674
  def test_link_many_empty_fields
608
675
  res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
609
676
  s.root :user
610
- s.type :user, fields: [:id, :email] do |t|
677
+ s.type :user, fields: [:email] do |t|
611
678
  t.many :address, fk: "user_id = users.id"
612
679
  end
613
680
  s.type :address
@@ -616,14 +683,14 @@ module PgGraphQl
616
683
  assert_equal token(<<-SQL
617
684
  select 'user'::text as key,
618
685
  (select to_json(x.*)
619
- from (select id,
686
+ from (select users.id,
620
687
  email,
621
688
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
622
- from (select id
689
+ from (select addresses.id
623
690
  from addresses
624
691
  where (user_id = users.id)) x) as address
625
692
  from users
626
- where id = 1 limit 1) x) as value
693
+ where users.id = 1 limit 1) x) as value
627
694
  SQL
628
695
  ), token(res)
629
696
  end
@@ -631,7 +698,7 @@ module PgGraphQl
631
698
  def test_link_many_filter
632
699
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
633
700
  s.root :user
634
- s.type :user, fields: [:id, :email] do |t|
701
+ s.type :user, fields: [:email] do |t|
635
702
  t.many :address, fk: "user_id = users.id", filter: "id % 2 = 0"
636
703
  end
637
704
  s.type :address
@@ -640,14 +707,14 @@ module PgGraphQl
640
707
  assert_equal token(<<-SQL
641
708
  select 'user'::text as key,
642
709
  (select to_json(x.*)
643
- from (select id,
710
+ from (select users.id,
644
711
  email,
645
712
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
646
- from (select id
713
+ from (select addresses.id
647
714
  from addresses
648
- where id = '99' and (user_id = users.id) and (id % 2 = 0)) x) as address
715
+ where addresses.id = '99' and (user_id = users.id) and (id % 2 = 0)) x) as address
649
716
  from users
650
- where id = 1 limit 1) x) as value
717
+ where users.id = 1 limit 1) x) as value
651
718
  SQL
652
719
  ), token(res)
653
720
  end
@@ -655,7 +722,7 @@ module PgGraphQl
655
722
  def test_link_many_order_by
656
723
  res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
657
724
  s.root :user
658
- s.type :user, fields: [:id, :email] do |t|
725
+ s.type :user, fields: [:email] do |t|
659
726
  t.many :address, fk: "user_id = users.id", order_by: "id desc"
660
727
  end
661
728
  s.type :address
@@ -664,14 +731,14 @@ module PgGraphQl
664
731
  assert_equal token(<<-SQL
665
732
  select 'user'::text as key,
666
733
  (select to_json(x.*)
667
- from (select id,
734
+ from (select users.id,
668
735
  email,
669
736
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
670
- from (select id
737
+ from (select addresses.id
671
738
  from addresses
672
- where id = '99' and (user_id = users.id) order by id desc) x) as address
739
+ where addresses.id = '99' and (user_id = users.id) order by id desc) x) as address
673
740
  from users
674
- where id = 1 limit 1) x) as value
741
+ where users.id = 1 limit 1) x) as value
675
742
  SQL
676
743
  ), token(res)
677
744
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pggraphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-22 00:00:00.000000000 Z
11
+ date: 2015-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json