graphql-association_batch_resolver 0.1.1 → 0.1.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
  SHA256:
3
- metadata.gz: f0d358169832729efb0321f747663d77a5f0a6b508245b9d31f717925fb2a6a1
4
- data.tar.gz: 5b051213e91cc212f7a8591088267f3f3b3004dce5c3384680ce6b96d759c2a4
3
+ metadata.gz: 81c7c496fd558d2f36960ae703d3b785a59a843bfde46fbc8cd286a82c47a7e1
4
+ data.tar.gz: eeacd401c91ea7cc795d9d02d362a16d8eb07ed8f4a86e9096a0a23383fda773
5
5
  SHA512:
6
- metadata.gz: 9b197fd21cc259e2950200fcb1325146306991af9b30d37e4664d43fd4569ba8157c1f63d49ac25086afdde3d4f36a92503d4f28780748fca44cde1cedb2d59d
7
- data.tar.gz: c33f52fb4ca9e34c06416769708df019abf413456d83ad6c9711864a64db228934d76eca6f29311a83216d1dd1bd8f662ac3abf8d340e28f3c2bf4d903954b1c
6
+ metadata.gz: a0cb7ca2b4d67e15f175578bbe21ed20a57f1548eb7b2adc379606069950ac281b0293a77587696c84531a512f29ca7e44106186451c5d659935290c933ba044
7
+ data.tar.gz: 100dbcdc2189b2d55a17386ddcff89fc2410337a080335b4fcf2a377555b0211e0418ac97ff9f04a83151a2a4a777003b1b6b1fe5ea5b46b26c1672ed9db0f21
@@ -49,30 +49,35 @@ module GraphQL
49
49
  raise ArgumentError, "No association #{association_name} on #{model}" unless association_exists
50
50
  end
51
51
 
52
- # rubocop:disable Metrics/AbcSize
53
52
  def preload_association(records)
54
- select_model_primary_keys = ColumnAggregator.aggregate([model.arel_table[model_primary_key]])
55
-
56
- association_records = model.where(model_primary_key => records)
57
- .joins(association_name)
58
- .select(association_model.arel_table[Arel.star])
59
- .select(select_model_primary_keys.as('model_primary_keys'))
60
- .group(association_model.arel_table[association_primary_key])
61
-
53
+ association_records = associations_for(records)
62
54
  association_records = options[:scope].call(association_records, context) if options[:scope].respond_to?(:call)
55
+ find_by_sql = association_records.to_sql
63
56
 
64
- # .merge(Pundit.policy_scope!(context[:user], association.klass))
65
-
66
- self.scope = association_model.find_by_sql(association_records.to_sql).to_a
57
+ self.scope = if find_by_sql.present?
58
+ association_model.find_by_sql(find_by_sql).to_a
59
+ else
60
+ []
61
+ end
67
62
  end
68
63
 
64
+ # rubocop:disable Metrics/AbcSize
65
+ def associations_for(records)
66
+ select_model_primary_keys = ColumnAggregator.aggregate([model.arel_table[model_primary_key]])
67
+ self.scope = model.where(model_primary_key => records)
68
+ .joins(association_name)
69
+ .select(association_model.arel_table[Arel.star])
70
+ .select(select_model_primary_keys.as('model_primary_keys'))
71
+ .group(association_model.arel_table[association_primary_key])
72
+ end
69
73
  # rubocop:enable Metrics/AbcSize
70
74
 
71
75
  def read_association(model_record)
72
76
  key = model_record.send(model_primary_key)
73
77
 
78
+ type = model.type_for_attribute(model_primary_key)
74
79
  association_scope = scope.select do |association_record|
75
- ColumnAggregator.deserialize(association_record.model_primary_keys).include?(key.to_s)
80
+ ColumnAggregator.deserialize(association_record.model_primary_keys, type).include?(key)
76
81
  end
77
82
 
78
83
  is_collection ? association_scope : association_scope.first
@@ -34,7 +34,7 @@ module GraphQL
34
34
  def initialize(*args, loader_class: GraphQL::AssociationBatchResolver.configuration.loader, **keywargs, &block)
35
35
  super(*args, **keywargs, &block)
36
36
  initialization_arguments = [model, association]
37
- initialization_arguments << options if loader_class.method(:new).arity == 3
37
+ initialization_arguments << options if options
38
38
 
39
39
  @loader = loader_class.for(*initialization_arguments)
40
40
  @loader.context = context if @loader.respond_to?(:context=)
@@ -10,8 +10,8 @@ module GraphQL
10
10
  adapter.aggregate(expression)
11
11
  end
12
12
 
13
- def self.deserialize(column)
14
- adapter.deserialize(column)
13
+ def self.deserialize(column, type)
14
+ adapter.deserialize(column, type)
15
15
  end
16
16
 
17
17
  def self.adapter
@@ -7,8 +7,8 @@ module GraphQL
7
7
  Arel::Nodes::NamedFunction.new('GROUP_CONCAT', expression)
8
8
  end
9
9
 
10
- def self.deserialize(column)
11
- column.split(',')
10
+ def self.deserialize(column, type)
11
+ column.split(',').map(&type.method(:deserialize))
12
12
  end
13
13
  end
14
14
  end
@@ -7,7 +7,7 @@ module GraphQL
7
7
  Arel::Nodes::NamedFunction.new('ARRAY_AGG', expression)
8
8
  end
9
9
 
10
- def self.deserialize(column)
10
+ def self.deserialize(column, _type)
11
11
  column
12
12
  end
13
13
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module AssociationBatchResolver
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.11'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-association_batch_resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Derenge