pggraphql 0.0.5 → 0.0.6

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: ce3ae8e16af5b6972f61cacef41f24d9b19f8672
4
- data.tar.gz: bec017114b149de19a7cdca9a4f5e9d0663e7086
3
+ metadata.gz: 50e64d160c0600af362f81584ad44a4a7f78d591
4
+ data.tar.gz: 632dcd3c7395989a483f937fb9da7f01858a7a2f
5
5
  SHA512:
6
- metadata.gz: 0aa31bbf4d066e4d2265f410e00436dcf773488c5723cacf339b8c13221e948736398d96647c869e0605e03091de3047537d4b30a8bb394de2a0de8559f56cc0
7
- data.tar.gz: 448db0dd34a71332eec63993039d423ea752df5b2f05521f35b9065c82644da8f4d852939f84e3450f2febf2ff32584b0b4db127b084096e62c877b2992b2049
6
+ metadata.gz: 6564555082bb0560edad1f28a701ea0aca4ef097adfb2a03105071cfc43321361d9855b3a81ac182edf58a11409da8bf8edbb9ec20f4402a3df489de06f1fa02
7
+ data.tar.gz: 5cf566b528dfd0262cf42dea969d2bd655f134fc48b4429af2fef054035aae70cc2e72ce487b0e57ce20e6f3a7d21d6a8d5bf06885bd720ad2314e7d754dc373
@@ -1,3 +1,3 @@
1
1
  module Pggraphql
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/pggraphql.rb CHANGED
@@ -30,7 +30,7 @@ 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
- # puts "#{e[0].inspect}, #{link_name.inspect} ... type: #{type.inspect}"
33
+ raise "#{type.name.inspect} is not a root type" if level == 1 && !@roots.include?(type)
34
34
 
35
35
  raise "missing :fk on link #{link.name.inspect}" if link && !link.fk
36
36
 
@@ -53,9 +53,11 @@ module PgGraphQl
53
53
 
54
54
  wheres = []
55
55
 
56
- if ids && ids.to_s != "id"
56
+ raise "missing id for root type #{type.name.inspect}" if ((ids.is_a?(Array) && ids.empty?) || "#{ids}".empty?) && level == 1 && !type.null_pk
57
+
58
+ # if ids && ids.to_s != "id"
57
59
  wheres << type.pk.call(ids) if type.pk.call(ids)
58
- end
60
+ # end
59
61
 
60
62
  wheres << ("(" + type.filter + ")") if type.filter
61
63
 
@@ -86,7 +88,7 @@ module PgGraphQl
86
88
 
87
89
  end.join
88
90
  else
89
- wrap_root(query.map do |e|
91
+ wrap_root(query.map do |e|
90
92
  sql = to_sql([e].to_h, 1)
91
93
  "select '#{e[0]}'::text as key, (#{sql}) as value"
92
94
  end.join("\nunion all\n"))
@@ -98,7 +100,7 @@ module PgGraphQl
98
100
  end
99
101
 
100
102
  class Type
101
- attr_accessor :name, :table, :filter, :links, :order_by, :fields, :subtypes, :pk
103
+ attr_accessor :name, :table, :filter, :links, :order_by, :fields, :subtypes, :pk, :null_pk
102
104
  attr_reader :schema, :mappings
103
105
  def initialize(schema, name)
104
106
  @schema = schema
@@ -110,15 +112,20 @@ module PgGraphQl
110
112
  @links = {}
111
113
  @subtypes = {}
112
114
  @mappings = {}
115
+ @null_pk = false
113
116
  @pk = ->(ids) do
114
117
  if ids.is_a?(Array)
115
118
  if ids.empty?
116
119
  nil
117
120
  else
118
- "id in (#{ids.join(',')})"
121
+ "id in (" + ids.map{|id| id.is_a?(String) ? "'#{id}'" : id.to_s}.join(',') + ")"
119
122
  end
120
123
  else
121
- "id = #{ids}"
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
129
  end
123
130
  end
124
131
  end
@@ -37,10 +37,34 @@ module PgGraphQl
37
37
  ), token(res)
38
38
  end
39
39
 
40
+ def test_simple_fail_when_accessing_non_root
41
+ assert_raise_message ":user is not a root type" do
42
+ res = to_sql({user: {id: 1, email: "email"}}) do |s|
43
+ s.type :user, fields: [:id, :email]
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_simple_fail_without_pk
49
+ assert_raise_message "missing id for root type :user" do
50
+ res = to_sql({user: {email: "email"}}) do |s|
51
+ s.root :user
52
+ s.type :user, fields: [:id, :email]
53
+ end
54
+ end
55
+
56
+ assert_raise_message "missing id for root type :user" do
57
+ res = to_sql({user: {id: [], email: "email"}}) do |s|
58
+ s.root :user
59
+ s.type :user, fields: [:id, :email]
60
+ end
61
+ end
62
+ end
63
+
40
64
  def test_simple_pk_array_empty
41
65
  res = to_sql({user: {id: [], email: "email"}}) do |s|
42
66
  s.root :user
43
- s.type :user, fields: [:id, :email]
67
+ s.type :user, null_pk: true, fields: [:id, :email]
44
68
  end
45
69
 
46
70
  assert_equal token(<<-SQL
@@ -69,6 +93,37 @@ module PgGraphQl
69
93
  ), token(res)
70
94
  end
71
95
 
96
+ def test_simple_pk_type_handling
97
+ res = to_sql({user: {id: ["1"], email: "email"}}) do |s|
98
+ s.root :user
99
+ s.type :user, fields: [:id, :email]
100
+ end
101
+
102
+ assert_equal token(<<-SQL
103
+ select 'user'::text as key,
104
+ (select to_json(coalesce(json_agg(x.*), '[]'::json))
105
+ from (select id,
106
+ email
107
+ from users where id in ('1')) x) as value
108
+ SQL
109
+ ), token(res)
110
+
111
+
112
+ res = to_sql({user: {id: "1", email: "email"}}) do |s|
113
+ s.root :user
114
+ s.type :user, fields: [:id, :email]
115
+ end
116
+
117
+ assert_equal token(<<-SQL
118
+ select 'user'::text as key,
119
+ (select to_json(x.*)
120
+ from (select id,
121
+ email
122
+ from users where id = '1' limit 1) x) as value
123
+ SQL
124
+ ), token(res)
125
+ end
126
+
72
127
  def test_simple_pk_array_one
73
128
  res = to_sql({user: {id: [1], email: "email"}}) do |s|
74
129
  s.root :user
@@ -121,8 +176,9 @@ module PgGraphQl
121
176
  def test_simple_multiple
122
177
  res = to_sql({user: {id: 1, email: "email"}, educator: {id: "id"}}) do |s|
123
178
  s.root :user
179
+ s.root :educator
124
180
  s.type :user, fields: [:id, :email]
125
- s.type :educator
181
+ s.type :educator, null_pk: true
126
182
  end
127
183
 
128
184
  assert_equal token(<<-SQL
@@ -168,7 +224,7 @@ module PgGraphQl
168
224
 
169
225
  s.type :order
170
226
 
171
- s.type :product, fields: [:id, :type, :clickout__destination_url, :download__download_url] do |t|
227
+ s.type :product, null_pk: true, fields: [:id, :type, :clickout__destination_url, :download__download_url] do |t|
172
228
 
173
229
  t.map :id, "products.id"
174
230
 
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek