pggraphql 0.0.6 → 0.0.7

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: 50e64d160c0600af362f81584ad44a4a7f78d591
4
- data.tar.gz: 632dcd3c7395989a483f937fb9da7f01858a7a2f
3
+ metadata.gz: 9fea48a5522b0f0f471725feb12317d463f78cd6
4
+ data.tar.gz: f9475b2bb5ee21271dd7b3695cde9ca35f0b2f32
5
5
  SHA512:
6
- metadata.gz: 6564555082bb0560edad1f28a701ea0aca4ef097adfb2a03105071cfc43321361d9855b3a81ac182edf58a11409da8bf8edbb9ec20f4402a3df489de06f1fa02
7
- data.tar.gz: 5cf566b528dfd0262cf42dea969d2bd655f134fc48b4429af2fef054035aae70cc2e72ce487b0e57ce20e6f3a7d21d6a8d5bf06885bd720ad2314e7d754dc373
6
+ metadata.gz: eb7ac6feeff8448154dd3b4dbde792e9df29e901d2756f459e2325027857d9d783ea11f84b6ccb6e2988c4cec6fc1a78dd0c36b29406a80b79e1eb41e7ae771a
7
+ data.tar.gz: a050ca27622558a21c81e9199922bee8c2efef2ff2343dda8d735ad95d668ad430b8224f91a366ad6e548e4c8118bd3af1b02b88f39b036763533fd0c9e62432
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pggraphql (0.0.1)
4
+ pggraphql (0.0.6)
5
5
  activesupport
6
6
  json
7
7
 
@@ -1,3 +1,3 @@
1
1
  module Pggraphql
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/pggraphql.rb CHANGED
@@ -30,11 +30,14 @@ module PgGraphQl
30
30
  type = link ? link.type : self.types[e[0].to_s.singularize.to_sym]
31
31
  ids = e[1][:id]
32
32
 
33
- raise "#{type.name.inspect} is not a root type" if level == 1 && !@roots.include?(type)
33
+ raise "found :id with without value on type #{type.name.inspect}" if e[1].key?(:id) && ids.nil?
34
+ raise "found empty :id array on type #{type.name.inspect}" if e[1].key?(:id) && ids.is_a?(Array) && ids.empty?
35
+
34
36
 
37
+ raise "#{type.name.inspect} is not a root type" if level == 1 && !@roots.include?(type)
35
38
  raise "missing :fk on link #{link.name.inspect}" if link && !link.fk
36
39
 
37
- columns = e[1].map do |f|
40
+ columns = {id: nil}.merge(e[1]).map do |f|
38
41
  nested_link_name = f[0]
39
42
  field_name = f[0]
40
43
 
@@ -48,16 +51,15 @@ module PgGraphQl
48
51
  (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}"))
49
52
  end.join(",")
50
53
 
51
- is_many = (link && link.many?) || !ids || ids.is_a?(Array)
54
+ # is_many = (link && link.many?) || ids.is_a?(Array) || (level == 1 && !ids && type.null_pk == :array)
55
+ is_many = (link && link.many?) || (level == 1 && ids.is_a?(Array)) || (level == 1 && !ids && type.null_pk == :array)
52
56
  order_by = link.try(:order_by) || type.try(:order_by)
53
57
 
54
58
  wheres = []
55
59
 
56
- raise "missing id for root type #{type.name.inspect}" if ((ids.is_a?(Array) && ids.empty?) || "#{ids}".empty?) && level == 1 && !type.null_pk
60
+ raise "missing :id for root type #{type.name.inspect}" if !ids && level == 1 && !type.null_pk
57
61
 
58
- # if ids && ids.to_s != "id"
59
- wheres << type.pk.call(ids) if type.pk.call(ids)
60
- # end
62
+ wheres << type.pk.call(ids) if ids && type.pk.call(ids)
61
63
 
62
64
  wheres << ("(" + type.filter + ")") if type.filter
63
65
 
@@ -100,8 +102,8 @@ module PgGraphQl
100
102
  end
101
103
 
102
104
  class Type
103
- attr_accessor :name, :table, :filter, :links, :order_by, :fields, :subtypes, :pk, :null_pk
104
- attr_reader :schema, :mappings
105
+ attr_accessor :name, :table, :links, :order_by, :filter, :subtypes, :pk, :null_pk
106
+ attr_reader :schema, :mappings, :fields
105
107
  def initialize(schema, name)
106
108
  @schema = schema
107
109
  @name = name
@@ -115,20 +117,15 @@ module PgGraphQl
115
117
  @null_pk = false
116
118
  @pk = ->(ids) do
117
119
  if ids.is_a?(Array)
118
- if ids.empty?
119
- nil
120
- else
121
- "id in (" + ids.map{|id| id.is_a?(String) ? "'#{id}'" : id.to_s}.join(',') + ")"
122
- end
120
+ "id in (" + ids.map{|id| id.is_a?(String) ? "'#{id}'" : id.to_s}.join(',') + ")"
123
121
  else
124
- if "#{ids.to_s}".empty? || ids.to_s == "id"
125
- nil
126
- else
127
- "id = " + (ids.is_a?(String) ? "'#{ids}'" : "#{ids}")
128
- end
122
+ "id = " + (ids.is_a?(String) ? "'#{ids}'" : "#{ids}")
129
123
  end
130
124
  end
131
125
  end
126
+ def fields=(fields)
127
+ @fields = (fields + [:id]).uniq
128
+ end
132
129
  def map(field, column_expr)
133
130
  @mappings[field] = column_expr
134
131
  end
@@ -35,6 +35,39 @@ module PgGraphQl
35
35
  where id = 1 limit 1) x) as value
36
36
  SQL
37
37
  ), token(res)
38
+
39
+ # ---
40
+
41
+ res = to_sql({user: {id: 1, email: "email"}}) do |s|
42
+ s.root :user
43
+ s.type :user, fields: [:email]
44
+ end
45
+
46
+ assert_equal token(<<-SQL
47
+ select 'user'::text as key,
48
+ (select to_json(x.*)
49
+ from (select id,
50
+ email
51
+ from users
52
+ where id = 1 limit 1) x) as value
53
+ SQL
54
+ ), token(res)
55
+
56
+ # ---
57
+
58
+ res = to_sql({user: {email: "email"}}) do |s|
59
+ s.root :user
60
+ s.type :user, null_pk: :array, fields: [:email]
61
+ end
62
+
63
+ assert_equal token(<<-SQL
64
+ select 'user'::text as key,
65
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
66
+ from (select id,
67
+ email
68
+ from users) x) as value
69
+ SQL
70
+ ), token(res)
38
71
  end
39
72
 
40
73
  def test_simple_fail_when_accessing_non_root
@@ -46,14 +79,14 @@ module PgGraphQl
46
79
  end
47
80
 
48
81
  def test_simple_fail_without_pk
49
- assert_raise_message "missing id for root type :user" do
82
+ assert_raise_message "missing :id for root type :user" do
50
83
  res = to_sql({user: {email: "email"}}) do |s|
51
84
  s.root :user
52
85
  s.type :user, fields: [:id, :email]
53
86
  end
54
87
  end
55
88
 
56
- assert_raise_message "missing id for root type :user" do
89
+ assert_raise_message "found empty :id array on type :user" do
57
90
  res = to_sql({user: {id: [], email: "email"}}) do |s|
58
91
  s.root :user
59
92
  s.type :user, fields: [:id, :email]
@@ -61,26 +94,10 @@ module PgGraphQl
61
94
  end
62
95
  end
63
96
 
64
- def test_simple_pk_array_empty
65
- res = to_sql({user: {id: [], email: "email"}}) do |s|
66
- s.root :user
67
- s.type :user, null_pk: true, fields: [:id, :email]
68
- end
69
-
70
- assert_equal token(<<-SQL
71
- select 'user'::text as key,
72
- (select to_json(coalesce(json_agg(x.*), '[]'::json))
73
- from (select id,
74
- email
75
- from users) x) as value
76
- SQL
77
- ), token(res)
78
- end
79
-
80
97
  def test_simple_pk_custom
81
98
  res = to_sql({user: {id: "1", email: "email"}}) do |s|
82
99
  s.root :user
83
- s.type :user, fields: [:id, :email], pk: ->(id){ "access_token = '#{id}'" }
100
+ s.type :user, fields: [:email], pk: ->(id){ "access_token = '#{id}'" }
84
101
  end
85
102
 
86
103
  assert_equal token(<<-SQL
@@ -96,7 +113,7 @@ module PgGraphQl
96
113
  def test_simple_pk_type_handling
97
114
  res = to_sql({user: {id: ["1"], email: "email"}}) do |s|
98
115
  s.root :user
99
- s.type :user, fields: [:id, :email]
116
+ s.type :user, fields: [:email]
100
117
  end
101
118
 
102
119
  assert_equal token(<<-SQL
@@ -111,7 +128,7 @@ module PgGraphQl
111
128
 
112
129
  res = to_sql({user: {id: "1", email: "email"}}) do |s|
113
130
  s.root :user
114
- s.type :user, fields: [:id, :email]
131
+ s.type :user, fields: [:email]
115
132
  end
116
133
 
117
134
  assert_equal token(<<-SQL
@@ -154,6 +171,22 @@ module PgGraphQl
154
171
  from users where id in (1,2)) x) as value
155
172
  SQL
156
173
  ), token(res)
174
+
175
+ # ---
176
+
177
+ res = to_sql({user: {id: ['1','2'], email: "email"}}) do |s|
178
+ s.root :user
179
+ s.type :user, fields: [:id, :email]
180
+ end
181
+
182
+ assert_equal token(<<-SQL
183
+ select 'user'::text as key,
184
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
185
+ from (select id,
186
+ email
187
+ from users where id in ('1','2')) x) as value
188
+ SQL
189
+ ), token(res)
157
190
  end
158
191
 
159
192
  def test_simple_filter
@@ -174,7 +207,7 @@ module PgGraphQl
174
207
  end
175
208
 
176
209
  def test_simple_multiple
177
- res = to_sql({user: {id: 1, email: "email"}, educator: {id: "id"}}) do |s|
210
+ res = to_sql({user: {id: 1, email: "email"}, educator: {id: 99}}) do |s|
178
211
  s.root :user
179
212
  s.root :educator
180
213
  s.type :user, fields: [:id, :email]
@@ -192,7 +225,7 @@ module PgGraphQl
192
225
  select 'educator'::text as key,
193
226
  (select to_json(x.*)
194
227
  from (select id
195
- from educators limit 1) x) as value
228
+ from educators where id = 99 limit 1) x) as value
196
229
  SQL
197
230
  ), token(res)
198
231
  end
@@ -204,15 +237,11 @@ module PgGraphQl
204
237
  def test_inherit
205
238
  res = to_sql({
206
239
  products: {
207
- id: [],
208
240
  type: nil,
209
241
  clickout__destination_url: nil,
210
242
  download__download_url: nil,
211
243
  download__users: {
212
- id: "id",
213
- orders: {
214
- id: "id"
215
- }
244
+ orders: {}
216
245
  }
217
246
  }
218
247
  }) do |s|
@@ -224,7 +253,7 @@ module PgGraphQl
224
253
 
225
254
  s.type :order
226
255
 
227
- s.type :product, null_pk: true, fields: [:id, :type, :clickout__destination_url, :download__download_url] do |t|
256
+ s.type :product, null_pk: :array, fields: [:type, :clickout__destination_url, :download__download_url] do |t|
228
257
 
229
258
  t.map :id, "products.id"
230
259
 
@@ -268,7 +297,31 @@ module PgGraphQl
268
297
 
269
298
 
270
299
  def test_link_one
271
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
300
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
301
+ s.root :user
302
+ s.type :user, fields: [:id, :email] do |t|
303
+ t.one :address, fk: "id = users.address_id"
304
+ end
305
+ s.type :address
306
+ end
307
+
308
+ assert_equal token(<<-SQL
309
+ select 'user'::text as key,
310
+ (select to_json(x.*)
311
+ from (select id,
312
+ email,
313
+ (select to_json(x.*)
314
+ from (select id
315
+ from addresses
316
+ where id = '99' and (id = users.address_id) limit 1) x) as address
317
+ from users
318
+ where id = 1 limit 1) x) as value
319
+ SQL
320
+ ), token(res)
321
+ end
322
+
323
+ def test_link_one_nested_pk
324
+ res = to_sql({user: {id: 1, email: "email", address: {id: 99}}}) do |s|
272
325
  s.root :user
273
326
  s.type :user, fields: [:id, :email] do |t|
274
327
  t.one :address, fk: "id = users.address_id"
@@ -276,6 +329,51 @@ module PgGraphQl
276
329
  s.type :address
277
330
  end
278
331
 
332
+ assert_equal token(<<-SQL
333
+ select 'user'::text as key,
334
+ (select to_json(x.*)
335
+ from (select id,
336
+ email,
337
+ (select to_json(x.*)
338
+ from (select id
339
+ from addresses
340
+ where id = 99 and (id = users.address_id) limit 1) x) as address
341
+ from users
342
+ where id = 1 limit 1) x) as value
343
+ SQL
344
+ ), token(res)
345
+
346
+ res = to_sql({user: {id: 1, email: "email", address: {id: [99,999]}}}) do |s|
347
+ s.root :user
348
+ s.type :user, fields: [:id, :email] do |t|
349
+ t.one :address, fk: "id = users.address_id"
350
+ end
351
+ s.type :address
352
+ end
353
+
354
+ assert_equal token(<<-SQL
355
+ select 'user'::text as key,
356
+ (select to_json(x.*)
357
+ from (select id,
358
+ email,
359
+ (select to_json(x.*)
360
+ from (select id
361
+ from addresses
362
+ where id in (99,999) and (id = users.address_id) limit 1) x) as address
363
+ from users
364
+ where id = 1 limit 1) x) as value
365
+ SQL
366
+ ), token(res) end
367
+
368
+ def test_link_one_empty_fields
369
+ res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
370
+ s.root :user
371
+ s.type :user, fields: [:email] do |t|
372
+ t.one :address, fk: "id = users.address_id"
373
+ end
374
+ s.type :address
375
+ end
376
+
279
377
  assert_equal token(<<-SQL
280
378
  select 'user'::text as key,
281
379
  (select to_json(x.*)
@@ -305,7 +403,7 @@ module PgGraphQl
305
403
 
306
404
 
307
405
  def test_link_one_fk_sql
308
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
406
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
309
407
  s.root :user
310
408
  s.type :user, fields: [:id, :email] do |t|
311
409
  t.one :address, fk: "id = (select 100)"
@@ -321,7 +419,7 @@ module PgGraphQl
321
419
  (select to_json(x.*)
322
420
  from (select id
323
421
  from addresses
324
- where (id = (select 100)) limit 1) x) as address
422
+ where id = '99' and (id = (select 100)) limit 1) x) as address
325
423
  from users
326
424
  where id = 1 limit 1) x) as value
327
425
  SQL
@@ -329,7 +427,7 @@ module PgGraphQl
329
427
  end
330
428
 
331
429
  def test_link_one_filter
332
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
430
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
333
431
  s.root :user
334
432
  s.type :user, fields: [:id, :email] do |t|
335
433
  t.one :address, fk: "user_id = users.id", filter: "id > 100"
@@ -345,7 +443,7 @@ module PgGraphQl
345
443
  (select to_json(x.*)
346
444
  from (select id
347
445
  from addresses
348
- where (user_id = users.id) and (id > 100) limit 1) x) as address
446
+ where id = '99' and (user_id = users.id) and (id > 100) limit 1) x) as address
349
447
  from users
350
448
  where id = 1 limit 1) x) as value
351
449
  SQL
@@ -353,7 +451,7 @@ module PgGraphQl
353
451
  end
354
452
 
355
453
  def test_link_one_order_by
356
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
454
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
357
455
  s.root :user
358
456
  s.type :user, fields: [:id, :email] do |t|
359
457
  t.one :address, fk: "user_id = users.id", order_by: "id desc"
@@ -369,19 +467,19 @@ module PgGraphQl
369
467
  (select to_json(x.*)
370
468
  from (select id
371
469
  from addresses
372
- where (user_id = users.id) order by id desc limit 1) x) as address
470
+ where id = '99' and (user_id = users.id) order by id desc limit 1) x) as address
373
471
  from users
374
472
  where id = 1 limit 1) x) as value
375
473
  SQL
376
474
  ), token(res)
377
475
  end
378
476
 
379
- # #####################
380
- # # one-in-one
381
- # #####################
477
+ #####################
478
+ # one-in-one
479
+ #####################
382
480
 
383
481
  def test_link_one_in_one
384
- res = to_sql({user: {id: 1, email: "email", address: {id: "id", country: {id: "id"}}}}) do |s|
482
+ res = to_sql({user: {id: 1, email: "email", address: {country: {}}}}) do |s|
385
483
  s.root :user
386
484
  s.type :user, fields: [:id, :email] do |t|
387
485
  t.one :address, fk: "user_id = users.id"
@@ -411,13 +509,61 @@ module PgGraphQl
411
509
  ), token(res)
412
510
  end
413
511
 
414
- # #####################
415
- # # many
416
- # #####################
512
+ #####################
513
+ # many
514
+ #####################
417
515
 
418
516
 
419
517
  def test_link_many
420
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
518
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
519
+ s.root :user
520
+ s.type :user, fields: [:id, :email] do |t|
521
+ t.many :address, fk: "user_id = users.id"
522
+ end
523
+ s.type :address
524
+ end
525
+
526
+ assert_equal token(<<-SQL
527
+ select 'user'::text as key,
528
+ (select to_json(x.*)
529
+ from (select id,
530
+ email,
531
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
532
+ from (select id
533
+ from addresses
534
+ where id = '99' and (user_id = users.id)) x) as address
535
+ from users
536
+ where id = 1 limit 1) x) as value
537
+ SQL
538
+ ), token(res)
539
+ end
540
+
541
+ def test_link_many_nested_pk
542
+ res = to_sql({user: {id: 1, email: "email", address: {id: ["99","999"]}}}) do |s|
543
+ s.root :user
544
+ s.type :user, fields: [:id, :email] do |t|
545
+ t.many :address, fk: "user_id = users.id"
546
+ end
547
+ s.type :address
548
+ end
549
+
550
+ assert_equal token(<<-SQL
551
+ select 'user'::text as key,
552
+ (select to_json(x.*)
553
+ from (select id,
554
+ email,
555
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
556
+ from (select id
557
+ from addresses
558
+ where id in ('99','999') and (user_id = users.id)) x) as address
559
+ from users
560
+ where id = 1 limit 1) x) as value
561
+ SQL
562
+ ), token(res)
563
+ end
564
+
565
+ def test_link_many_empty_fields
566
+ res = to_sql({user: {id: 1, email: "email", address: {}}}) do |s|
421
567
  s.root :user
422
568
  s.type :user, fields: [:id, :email] do |t|
423
569
  t.many :address, fk: "user_id = users.id"
@@ -441,7 +587,7 @@ module PgGraphQl
441
587
  end
442
588
 
443
589
  def test_link_many_filter
444
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
590
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
445
591
  s.root :user
446
592
  s.type :user, fields: [:id, :email] do |t|
447
593
  t.many :address, fk: "user_id = users.id", filter: "id % 2 = 0"
@@ -457,7 +603,7 @@ module PgGraphQl
457
603
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
458
604
  from (select id
459
605
  from addresses
460
- where (user_id = users.id) and (id % 2 = 0)) x) as address
606
+ where id = '99' and (user_id = users.id) and (id % 2 = 0)) x) as address
461
607
  from users
462
608
  where id = 1 limit 1) x) as value
463
609
  SQL
@@ -465,7 +611,7 @@ module PgGraphQl
465
611
  end
466
612
 
467
613
  def test_link_many_order_by
468
- res = to_sql({user: {id: 1, email: "email", address: {id: "id"}}}) do |s|
614
+ res = to_sql({user: {id: 1, email: "email", address: {id: "99"}}}) do |s|
469
615
  s.root :user
470
616
  s.type :user, fields: [:id, :email] do |t|
471
617
  t.many :address, fk: "user_id = users.id", order_by: "id desc"
@@ -481,7 +627,7 @@ module PgGraphQl
481
627
  (select to_json(coalesce(json_agg(x.*), '[]'::json))
482
628
  from (select id
483
629
  from addresses
484
- where (user_id = users.id) order by id desc) x) as address
630
+ where id = '99' and (user_id = users.id) order by id desc) x) as address
485
631
  from users
486
632
  where id = 1 limit 1) x) as value
487
633
  SQL
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.6
4
+ version: 0.0.7
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-09 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json