pggraphql 0.1.2 → 0.1.3

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: 99fb888e996dd5a6d047dfe87d4b486aa5bdc1ba
4
- data.tar.gz: c213c7424178a7f0471e7992d47cdb81712b343a
3
+ metadata.gz: 40388c39022da2118d8ffe8774012034c35e29c5
4
+ data.tar.gz: efba25021710edfe95ab788e60fde4013df55736
5
5
  SHA512:
6
- metadata.gz: cd11586d6134db20251c457d277f99e5c2e5d3efe7fa472798d5a41dd2680c0fe11c18e288074622956e5035b004139340c65e3ece5a9bc032fcd55e6a74dbc6
7
- data.tar.gz: 69bc3ba9ca3f7e081c743ae483208fd109e8da9dafe218e109847e1726207fdbe604314d31bc80ec11bbd617f4273b3d611e9c57dd3eace0907ef93c5cb4ee0d
6
+ metadata.gz: 44b8bec7c4351cfa93d23c5b161b3131d1c3125e477f90db0185337831e3cca0dfe5dc3fad44c38facdd8f9f2b58912391ebd043ca2f3245a29f713bc22943a7
7
+ data.tar.gz: f725f47496545cf7c63889a145c50e53d5d14d7c260b30fb948f4886824d33e9e24e67427c814ae181f568dc95080dac80b3095144ecf9d08eefde33925c1336
@@ -1,3 +1,3 @@
1
1
  module Pggraphql
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/pggraphql.rb CHANGED
@@ -104,7 +104,7 @@ module PgGraphQl
104
104
 
105
105
  requested_fields = {id: nil} # always add :id field
106
106
  requested_fields = requested_fields.merge(type: nil) unless type.subtypes.empty? # always add :subtype
107
- requested_fields = requested_fields.merge(e[1])
107
+ requested_fields = requested_fields.merge(e[1].reject{|f| f.to_s =~ /^\$/})
108
108
 
109
109
  columns = requested_fields.map do |f|
110
110
  nested_link_name = f[0]
@@ -169,6 +169,30 @@ module PgGraphQl
169
169
  is_many = (link && link.many?) || (level == 1 && ids.is_a?(Array)) || (level == 1 && !ids && type.null_pk == :array)
170
170
  order_by = link.try(:order_by) || type.try(:order_by)
171
171
 
172
+ table_query = if type.table_query
173
+ sql_part = type.table_query.is_a?(Proc) ? type.table_query.call(e[1]) : type.table_query
174
+ "(" + handle_sql_part(sql_part, params, level, table_levels).to_s + ")"
175
+ else
176
+ type.table.to_s
177
+ end
178
+
179
+ subtypes = type.subtypes.map do |f|
180
+ subtype = f[1]
181
+ fk = subtype.fk.is_a?(Proc) ? subtype.fk.call(level) : subtype.fk
182
+
183
+ fk = "{#{subtype.name}}.id = {#{type.table}}.id and {#{type.table}}.type = '#{subtype.name}'" if fk == :subtype
184
+
185
+ subtype_table_query = if subtype.table_query
186
+ "(" + handle_sql_part(subtype.table_query, params, level, table_levels).to_s + ")"
187
+ else
188
+ subtype.table.to_s
189
+ end
190
+
191
+ subtype_as = handle_sql_part("{#{subtype.name}}", params, level, table_levels)
192
+
193
+ "left join #{subtype_table_query} as #{subtype_as} on (#{handle_sql_part(fk, params, level, table_levels)})"
194
+ end
195
+
172
196
  wheres = []
173
197
 
174
198
  raise "missing :id for root type #{type.name.inspect}" if !ids && level == 1 && !type.null_pk
@@ -204,20 +228,22 @@ module PgGraphQl
204
228
  sql += "coalesce(json_agg(" if is_many
205
229
  sql += "x.*"
206
230
  sql += "), '[]'::json)" if is_many
207
- sql += ") from (select #{columns} from #{type.table} as #{table_as}"
231
+ sql += ") from (select #{columns} from #{table_query} as #{table_as}"
208
232
 
209
- unless type.subtypes.empty?
210
- sql += "\n" + type.subtypes.map do |f|
211
- subtype = f[1]
212
- fk = subtype.fk.is_a?(Proc) ? subtype.fk.call(level) : subtype.fk
213
233
 
214
- fk = "{#{subtype.name}}.id = {#{type.table}}.id and {#{type.table}}.type = '#{subtype.name}'" if fk == :subtype
215
- subtype_as = handle_sql_part("{#{subtype.name}}", params, level, table_levels)
234
+ sql += "\n" + subtypes.join("\n") unless subtypes.empty?
216
235
 
217
- "left join #{subtype.table} as #{subtype_as} on (#{handle_sql_part(fk, params, level, table_levels)})"
218
- end.join("\n")
219
- end
236
+ # unless type.subtypes.empty?
237
+ # sql += "\n" + type.subtypes.map do |f|
238
+ # subtype = f[1]
239
+ # fk = subtype.fk.is_a?(Proc) ? subtype.fk.call(level) : subtype.fk
240
+
241
+ # fk = "{#{subtype.name}}.id = {#{type.table}}.id and {#{type.table}}.type = '#{subtype.name}'" if fk == :subtype
242
+ # subtype_as = handle_sql_part("{#{subtype.name}}", params, level, table_levels)
220
243
 
244
+ # "left join #{subtype.table} as #{subtype_as} on (#{handle_sql_part(fk, params, level, table_levels)})"
245
+ # end.join("\n")
246
+ # end
221
247
 
222
248
  sql += " where #{wheres.join(' and ')}" unless wheres.empty?
223
249
  sql += " order by #{handle_sql_part(order_by, params, level, table_levels)}" if order_by
@@ -240,12 +266,13 @@ module PgGraphQl
240
266
  end
241
267
 
242
268
  class Type
243
- attr_accessor :name, :table, :links, :order_by, :filter, :subtypes, :pk, :null_pk
269
+ attr_accessor :name, :table, :table_query, :links, :order_by, :filter, :subtypes, :pk, :null_pk
244
270
  attr_reader :schema, :mappings, :fields
245
271
  def initialize(schema, name)
246
272
  @schema = schema
247
273
  @name = name
248
274
  @table = name.to_s.pluralize.to_sym
275
+ @table_query = nil
249
276
  @fields = []
250
277
  @filter = nil
251
278
  @order_by = nil
@@ -315,13 +342,14 @@ module PgGraphQl
315
342
  end
316
343
 
317
344
  class SubType
318
- attr_accessor :name, :table, :fk
345
+ attr_accessor :name, :table, :fk, :table_query
319
346
  attr_reader :type
320
347
  def initialize(type, name)
321
348
  @type = type
322
349
  @name = name
323
350
  @table = nil
324
351
  @fk = nil
352
+ @table_query = nil
325
353
  end
326
354
  def has_one(name, opts={})
327
355
  @type.has_one(:"#{@name}__#{name}", {type: name}.merge(opts))
@@ -160,6 +160,101 @@ module PgGraphQl
160
160
 
161
161
  end
162
162
 
163
+ def test_simple_igore_dollar_fields
164
+ res = to_sql({user: {id: 1, email: nil, :"$custom" => nil}}) do |s|
165
+ s.root :user
166
+ s.type :user, fields: [:email]
167
+ end
168
+
169
+ assert_equal token(<<-SQL
170
+ select 'user'::text as key,
171
+ (select to_json(x.*)
172
+ from (select users1.id,
173
+ users1.email as email
174
+ from users as users1
175
+ where users1.id = ? limit 1) x) as value
176
+ SQL
177
+ ), token(res[:sql])
178
+ end
179
+
180
+ def test_simple_table_query
181
+ res = to_sql({user: {id: 1, email: nil}}) do |s|
182
+ s.root :user
183
+ s.type :user, fields: [:email] do |t|
184
+ t.table_query = "select 1 as id, 'my@domain' as email"
185
+ end
186
+ end
187
+
188
+ assert_equal token(<<-SQL
189
+ select 'user'::text as key,
190
+ (select to_json(x.*)
191
+ from (select users1.id,
192
+ users1.email as email
193
+ from (select 1 as id, 'my@domain' as email) as users1
194
+ where users1.id = ? limit 1) x) as value
195
+ SQL
196
+ ), token(res[:sql])
197
+
198
+ # ----
199
+
200
+ res = to_sql({user: {id: 1, email: nil}}) do |s|
201
+ s.root :user
202
+ s.type :user, fields: [:email] do |t|
203
+ t.table_query = "select 1 as id, 'my@domain' as email where {users}.id > 0"
204
+ end
205
+ end
206
+
207
+ assert_equal token(<<-SQL
208
+ select 'user'::text as key,
209
+ (select to_json(x.*)
210
+ from (select users1.id,
211
+ users1.email as email
212
+ from (select 1 as id, 'my@domain' as email where users1.id > 0) as users1
213
+ where users1.id = ? limit 1) x) as value
214
+ SQL
215
+ ), token(res[:sql])
216
+
217
+ # ----
218
+
219
+ res = to_sql({user: {id: 1, email: nil}}) do |s|
220
+ s.root :user
221
+ s.type :user, fields: [:email] do |t|
222
+ t.table_query = ->(query){ "select 1 as id, 'my@domain' as email" }
223
+ end
224
+ end
225
+
226
+ assert_equal token(<<-SQL
227
+ select 'user'::text as key,
228
+ (select to_json(x.*)
229
+ from (select users1.id,
230
+ users1.email as email
231
+ from (select 1 as id, 'my@domain' as email) as users1
232
+ where users1.id = ? limit 1) x) as value
233
+ SQL
234
+ ), token(res[:sql])
235
+ end
236
+
237
+ def test_simple_table_query_with_params
238
+ res = to_sql({user: {id: 1, email: nil}}) do |s|
239
+ s.root :user
240
+ s.type :user, fields: [:email] do |t|
241
+ t.table_query = ["select ? as id, 'my@domain' as email", 99]
242
+ end
243
+ end
244
+
245
+ assert_equal token(<<-SQL
246
+ select 'user'::text as key,
247
+ (select to_json(x.*)
248
+ from (select users1.id,
249
+ users1.email as email
250
+ from (select ? as id, 'my@domain' as email) as users1
251
+ where users1.id = ? limit 1) x) as value
252
+ SQL
253
+ ), token(res[:sql])
254
+
255
+ assert_equal [99, 1], res[:params]
256
+ end
257
+
163
258
  def test_guard_field
164
259
  res = to_sql({user: {id: 1, email: nil}}) do |s|
165
260
  s.root :user
@@ -655,6 +750,66 @@ module PgGraphQl
655
750
 
656
751
  end
657
752
 
753
+ def test_inherit_with_table_query
754
+ res = to_sql({
755
+ products: {
756
+ id: 1,
757
+ clickout__destination_url: nil
758
+ }
759
+ }) do |s|
760
+ s.root :product
761
+ s.type :product, null_pk: :array, fields: [:clickout__destination_url] do |t|
762
+ t.subtype :clickout, table: :product_clickouts do |st|
763
+ st.table_query = <<-SQL
764
+ select 1 as id, 'someurl' as destination_url
765
+ SQL
766
+ end
767
+ end
768
+ end
769
+
770
+ assert_equal token(<<-SQL
771
+ select 'products'::text as key,
772
+ (select to_json(x.*)
773
+ from (select products1.id,
774
+ products1.type as type,
775
+ clickout1.destination_url as clickout__destination_url
776
+ from products as products1
777
+ left join (select 1 as id, 'someurl' as destination_url) as clickout1 on (clickout1.id = products1.id
778
+ and products1.type = 'clickout') where products1.id = ? limit 1) x) as value
779
+ SQL
780
+ ), token(res[:sql])
781
+ assert_equal [1], res[:params]
782
+
783
+ # ----
784
+
785
+ res = to_sql({
786
+ products: {
787
+ id: 1,
788
+ clickout__destination_url: nil
789
+ }
790
+ }) do |s|
791
+ s.root :product
792
+ s.type :product, null_pk: :array, fields: [:clickout__destination_url] do |t|
793
+ t.subtype :clickout, table: :product_clickouts do |st|
794
+ st.table_query = ["select 1 as id, ? as destination_url", "someurl"]
795
+ end
796
+ end
797
+ end
798
+
799
+ assert_equal token(<<-SQL
800
+ select 'products'::text as key,
801
+ (select to_json(x.*)
802
+ from (select products1.id,
803
+ products1.type as type,
804
+ clickout1.destination_url as clickout__destination_url
805
+ from products as products1
806
+ left join (select 1 as id, ? as destination_url) as clickout1 on (clickout1.id = products1.id
807
+ and products1.type = 'clickout') where products1.id = ? limit 1) x) as value
808
+ SQL
809
+ ), token(res[:sql])
810
+ assert_equal ["someurl", 1], res[:params]
811
+ end
812
+
658
813
  #####################
659
814
  # one
660
815
  #####################
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.1.2
4
+ version: 0.1.3
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-05-21 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json