graphql-association_batch_resolver 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: e1d2f53b7e68d2fde0df4435ceaf595c0cab576a7b465275e1888a11348c977f
4
- data.tar.gz: b0f420d3ddaa0ceda0a5a23d724197beaff213d85b50d6e11898bd206c632258
3
+ metadata.gz: f0d358169832729efb0321f747663d77a5f0a6b508245b9d31f717925fb2a6a1
4
+ data.tar.gz: 5b051213e91cc212f7a8591088267f3f3b3004dce5c3384680ce6b96d759c2a4
5
5
  SHA512:
6
- metadata.gz: f0182b2ab4ef4400eea1682fd788026068e6c1462f022b7fbfa7e18e13703533853baca820074c09dc4c26b80460230917a0c7b1fab0ed3be035aae9b4079782
7
- data.tar.gz: b914a4945c385e33da8bc3881546bbdc6ddbae54317a9530194f66fa0f866e8653faf94bcbdf7e14ce8559bd4e39d965ccf78101d8210c2d55a58a1448d8398f
6
+ metadata.gz: 9b197fd21cc259e2950200fcb1325146306991af9b30d37e4664d43fd4569ba8157c1f63d49ac25086afdde3d4f36a92503d4f28780748fca44cde1cedb2d59d
7
+ data.tar.gz: c33f52fb4ca9e34c06416769708df019abf413456d83ad6c9711864a64db228934d76eca6f29311a83216d1dd1bd8f662ac3abf8d340e28f3c2bf4d903954b1c
data/.rubocop.yml CHANGED
@@ -4,3 +4,5 @@ Style/Documentation:
4
4
  Metrics/LineLength:
5
5
  Max: 120
6
6
 
7
+ Metrics/ClassLength:
8
+ Max: 200
@@ -12,7 +12,6 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = 'GraphQL Resolver for ActiveRecord Associations'
14
14
 
15
-
16
15
  # Specify which files should be added to the gem when it is released.
17
16
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
17
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
@@ -19,8 +19,8 @@ module GraphQL
19
19
  end
20
20
  end
21
21
 
22
- def self.for(model, association)
23
- ResolverBuilder.new(model, association).resolver
22
+ def self.for(model, association, opts = {})
23
+ ResolverBuilder.new(model, association, opts).resolver
24
24
  end
25
25
 
26
26
  def self.configure
@@ -6,17 +6,18 @@ module GraphQL
6
6
  module AssociationBatchResolver
7
7
  class AssociationLoader < GraphQL::Batch::Loader
8
8
  attr_reader :model, :model_primary_key, :association_name, :is_collection, :association_model,
9
- :association_primary_key
10
- attr_accessor :scope, :model_primary_key_to_association_primary_keys
9
+ :association_primary_key, :options
10
+ attr_accessor :scope, :context
11
11
 
12
- def self.validate(model, association_name)
13
- new(model, association_name)
12
+ def self.validate(model, association_name, options = {})
13
+ new(model, association_name, options)
14
14
  nil
15
15
  end
16
16
 
17
- def initialize(model, association_name)
17
+ def initialize(model, association_name, options = {})
18
18
  @model = model
19
19
  @association_name = association_name
20
+ @options = options
20
21
  validate
21
22
  @model_primary_key = model.primary_key
22
23
  association = @model.reflect_on_association(association_name)
@@ -52,16 +53,19 @@ module GraphQL
52
53
  def preload_association(records)
53
54
  select_model_primary_keys = ColumnAggregator.aggregate([model.arel_table[model_primary_key]])
54
55
 
55
- id_map = model.where(model_primary_key => records)
56
- .joins(association_name)
57
- .select(association_model.arel_table[Arel.star])
58
- .select(select_model_primary_keys.as('model_primary_keys'))
59
- .group(association_model.arel_table[association_primary_key])
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
+
62
+ association_records = options[:scope].call(association_records, context) if options[:scope].respond_to?(:call)
60
63
 
61
64
  # .merge(Pundit.policy_scope!(context[:user], association.klass))
62
65
 
63
- self.scope = association_model.find_by_sql(id_map.to_sql).to_a
66
+ self.scope = association_model.find_by_sql(association_records.to_sql).to_a
64
67
  end
68
+
65
69
  # rubocop:enable Metrics/AbcSize
66
70
 
67
71
  def read_association(model_record)
@@ -6,7 +6,7 @@ module GraphQL
6
6
  module AssociationBatchResolver
7
7
  class AssociationResolver < GraphQL::Schema::Resolver
8
8
  class << self
9
- attr_accessor :model, :association
9
+ attr_accessor :model, :association, :options
10
10
 
11
11
  def validate!
12
12
  validate_model!
@@ -28,12 +28,16 @@ module GraphQL
28
28
 
29
29
  extend Forwardable
30
30
 
31
- def_delegators :'self.class', :model, :association
31
+ def_delegators :'self.class', :model, :association, :options
32
32
  attr_accessor :loader
33
33
 
34
34
  def initialize(*args, loader_class: GraphQL::AssociationBatchResolver.configuration.loader, **keywargs, &block)
35
35
  super(*args, **keywargs, &block)
36
- @loader = loader_class.for(model, association)
36
+ initialization_arguments = [model, association]
37
+ initialization_arguments << options if loader_class.method(:new).arity == 3
38
+
39
+ @loader = loader_class.for(*initialization_arguments)
40
+ @loader.context = context if @loader.respond_to?(:context=)
37
41
  end
38
42
 
39
43
  def resolve(*)
@@ -6,11 +6,12 @@ require 'graphql/association_batch_resolver/association_loader'
6
6
  module GraphQL
7
7
  module AssociationBatchResolver
8
8
  class ResolverBuilder
9
- attr_accessor :model, :association
9
+ attr_accessor :model, :association, :options
10
10
 
11
- def initialize(model, association)
11
+ def initialize(model, association, opts)
12
12
  @model = model
13
13
  @association = association
14
+ @options = opts
14
15
  end
15
16
 
16
17
  def resolver_class_name
@@ -31,6 +32,7 @@ module GraphQL
31
32
  resolver = Class.new(AssociationResolver)
32
33
  resolver.model = model
33
34
  resolver.association = association
35
+ resolver.options = options
34
36
 
35
37
  model.const_set(resolver_class_name, resolver)
36
38
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module AssociationBatchResolver
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-association_batch_resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Derenge
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-21 00:00:00.000000000 Z
11
+ date: 2019-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord