graphql-association_batch_resolver 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1d2f53b7e68d2fde0df4435ceaf595c0cab576a7b465275e1888a11348c977f
4
+ data.tar.gz: b0f420d3ddaa0ceda0a5a23d724197beaff213d85b50d6e11898bd206c632258
5
+ SHA512:
6
+ metadata.gz: f0182b2ab4ef4400eea1682fd788026068e6c1462f022b7fbfa7e18e13703533853baca820074c09dc4c26b80460230917a0c7b1fab0ed3be035aae9b4079782
7
+ data.tar.gz: b914a4945c385e33da8bc3881546bbdc6ddbae54317a9530194f66fa0f866e8653faf94bcbdf7e14ce8559bd4e39d965ccf78101d8210c2d55a58a1448d8398f
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ /.idea/
11
+ /.byebug_history
@@ -0,0 +1,6 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.6
7
+ before_install: gem install bundler -v 1.17.3
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in graphql-association_batch_resolver.gemspec
8
+ gemspec
@@ -0,0 +1,51 @@
1
+ # GraphQL::AssociationBatchResolver
2
+
3
+ A resolver for [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) that batch loads active record associations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'association_batch_resolver'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install graphql-association_batch_resolver
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # Model definition
25
+ class Player < ActiveRecord::Base
26
+ belongs_to :team
27
+
28
+ end
29
+
30
+ # Type definition
31
+ class PlayerType < GraphQL::Schema::Object
32
+ field :team, resolver: GraphQL::AssociationBatchResolver.for(Player, :team)
33
+ end
34
+
35
+ ```
36
+
37
+ **GraphQL::AssociationBatchResolver#for**
38
+ * Arguments
39
+ * model - ActiveRecord::Base descendant
40
+ * association - Any ActiveRecord association name on `model`
41
+
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/derenge/graphql-association_batch_resolver.
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'graphql/association_batch_resolver'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'graphql/association_batch_resolver/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'graphql-association_batch_resolver'
9
+ spec.version = GraphQL::AssociationBatchResolver::VERSION
10
+ spec.authors = ['Andrew Derenge']
11
+ spec.email = ['andrew@rigup.com']
12
+
13
+ spec.summary = 'GraphQL Resolver for ActiveRecord Associations'
14
+
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activerecord', '~> 5.2.0'
26
+ spec.add_dependency 'graphql', '~> 1.9.0'
27
+ spec.add_dependency 'graphql-batch', '~> 0.4.0'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.17'
30
+ spec.add_development_dependency 'minitest', '~> 5.0'
31
+ spec.add_development_dependency 'pry-byebug', '~> 3.7.0'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'rubocop', '~> 0.74.0'
34
+ spec.add_development_dependency 'sqlite3', '~> 1.4.1'
35
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql/association_batch_resolver/version'
4
+ require 'graphql'
5
+ require 'graphql/batch'
6
+ require 'graphql/association_batch_resolver/resolver_builder'
7
+
8
+ module GraphQL
9
+ module AssociationBatchResolver
10
+ class Error < StandardError; end
11
+ class InvalidModel < Error; end
12
+ class InvalidAssociation < Error; end
13
+
14
+ class << self
15
+ attr_writer :configuration
16
+
17
+ def configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+ end
21
+
22
+ def self.for(model, association)
23
+ ResolverBuilder.new(model, association).resolver
24
+ end
25
+
26
+ def self.configure
27
+ self.configuration ||= Configuration.new
28
+ yield(configuration)
29
+ end
30
+
31
+ class Configuration
32
+ attr_accessor :loader
33
+
34
+ def initialize
35
+ @loader = GraphQL::AssociationBatchResolver::AssociationLoader
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'column_aggregator'
4
+
5
+ module GraphQL
6
+ module AssociationBatchResolver
7
+ class AssociationLoader < GraphQL::Batch::Loader
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
11
+
12
+ def self.validate(model, association_name)
13
+ new(model, association_name)
14
+ nil
15
+ end
16
+
17
+ def initialize(model, association_name)
18
+ @model = model
19
+ @association_name = association_name
20
+ validate
21
+ @model_primary_key = model.primary_key
22
+ association = @model.reflect_on_association(association_name)
23
+ @is_collection = association.collection?
24
+ @association_model = association.klass
25
+ @association_primary_key = @association_model.primary_key
26
+ end
27
+
28
+ def load(record)
29
+ raise TypeError, "#{model} loader can't load association for #{record.class}" unless record.is_a?(model)
30
+
31
+ super
32
+ end
33
+
34
+ # We want to load the associations on all records, even if they have the same id
35
+ def cache_key(record)
36
+ record.object_id
37
+ end
38
+
39
+ def perform(records)
40
+ preload_association(records)
41
+ records.each { |record| fulfill(record, read_association(record)) }
42
+ end
43
+
44
+ private
45
+
46
+ def validate
47
+ association_exists = !model.reflect_on_association(association_name).nil?
48
+ raise ArgumentError, "No association #{association_name} on #{model}" unless association_exists
49
+ end
50
+
51
+ # rubocop:disable Metrics/AbcSize
52
+ def preload_association(records)
53
+ select_model_primary_keys = ColumnAggregator.aggregate([model.arel_table[model_primary_key]])
54
+
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])
60
+
61
+ # .merge(Pundit.policy_scope!(context[:user], association.klass))
62
+
63
+ self.scope = association_model.find_by_sql(id_map.to_sql).to_a
64
+ end
65
+ # rubocop:enable Metrics/AbcSize
66
+
67
+ def read_association(model_record)
68
+ key = model_record.send(model_primary_key)
69
+
70
+ association_scope = scope.select do |association_record|
71
+ ColumnAggregator.deserialize(association_record.model_primary_keys).include?(key.to_s)
72
+ end
73
+
74
+ is_collection ? association_scope : association_scope.first
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module GraphQL
6
+ module AssociationBatchResolver
7
+ class AssociationResolver < GraphQL::Schema::Resolver
8
+ class << self
9
+ attr_accessor :model, :association
10
+
11
+ def validate!
12
+ validate_model!
13
+ validate_association!
14
+ end
15
+
16
+ def validate_model!
17
+ is_active_record_model = model < ActiveRecord::Base
18
+
19
+ raise InvalidModel, "Model (#{model}) must be an ActiveRecord::Base descendant" unless is_active_record_model
20
+ end
21
+
22
+ def validate_association!
23
+ is_association = !model.reflect_on_association(association).nil?
24
+
25
+ raise InvalidAssociation, "Association :#{association} does not exist on #{model}" unless is_association
26
+ end
27
+ end
28
+
29
+ extend Forwardable
30
+
31
+ def_delegators :'self.class', :model, :association
32
+ attr_accessor :loader
33
+
34
+ def initialize(*args, loader_class: GraphQL::AssociationBatchResolver.configuration.loader, **keywargs, &block)
35
+ super(*args, **keywargs, &block)
36
+ @loader = loader_class.for(model, association)
37
+ end
38
+
39
+ def resolve(*)
40
+ loader.load(object)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'column_aggregators/mysql_column_aggregator'
4
+ require_relative 'column_aggregators/postgres_column_aggregator'
5
+
6
+ module GraphQL
7
+ module AssociationBatchResolver
8
+ module ColumnAggregator
9
+ def self.aggregate(expression)
10
+ adapter.aggregate(expression)
11
+ end
12
+
13
+ def self.deserialize(column)
14
+ adapter.deserialize(column)
15
+ end
16
+
17
+ def self.adapter
18
+ case ActiveRecord::Base.connection.adapter_name
19
+ when 'SQLite', 'Mysql2'
20
+ MysqlColumnAggregator
21
+ when 'PostgreSQL'
22
+ PostgresColumnAggregator
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module AssociationBatchResolver
5
+ module MysqlColumnAggregator
6
+ def self.aggregate(expression)
7
+ Arel::Nodes::NamedFunction.new('GROUP_CONCAT', expression)
8
+ end
9
+
10
+ def self.deserialize(column)
11
+ column.split(',')
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module AssociationBatchResolver
5
+ module PostgresColumnAggregator
6
+ def self.aggregate(expression)
7
+ Arel::Nodes::NamedFunction.new('ARRAY_AGG', expression)
8
+ end
9
+
10
+ def self.deserialize(column)
11
+ column
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql/association_batch_resolver/association_resolver'
4
+ require 'graphql/association_batch_resolver/association_loader'
5
+
6
+ module GraphQL
7
+ module AssociationBatchResolver
8
+ class ResolverBuilder
9
+ attr_accessor :model, :association
10
+
11
+ def initialize(model, association)
12
+ @model = model
13
+ @association = association
14
+ end
15
+
16
+ def resolver_class_name
17
+ @resolver_class_name ||= "#{association_class_name}Resolver"
18
+ end
19
+
20
+ def association_class_name
21
+ association.to_s.classify
22
+ end
23
+
24
+ def resolver
25
+ define_resolver unless model.const_defined?(resolver_class_name)
26
+
27
+ model.const_get(resolver_class_name)
28
+ end
29
+
30
+ def define_resolver
31
+ resolver = Class.new(AssociationResolver)
32
+ resolver.model = model
33
+ resolver.association = association
34
+
35
+ model.const_set(resolver_class_name, resolver)
36
+
37
+ resolver.validate!
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module AssociationBatchResolver
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-association_batch_resolver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Derenge
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: graphql
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.9.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: graphql-batch
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.7.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.7.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.74.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.74.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.4.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.4.1
139
+ description:
140
+ email:
141
+ - andrew@rigup.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rubocop.yml"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - README.md
151
+ - Rakefile
152
+ - bin/console
153
+ - bin/setup
154
+ - graphql-association_batch_resolver.gemspec
155
+ - lib/graphql/association_batch_resolver.rb
156
+ - lib/graphql/association_batch_resolver/association_loader.rb
157
+ - lib/graphql/association_batch_resolver/association_resolver.rb
158
+ - lib/graphql/association_batch_resolver/column_aggregator.rb
159
+ - lib/graphql/association_batch_resolver/column_aggregators/mysql_column_aggregator.rb
160
+ - lib/graphql/association_batch_resolver/column_aggregators/postgres_column_aggregator.rb
161
+ - lib/graphql/association_batch_resolver/resolver_builder.rb
162
+ - lib/graphql/association_batch_resolver/version.rb
163
+ homepage:
164
+ licenses: []
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubygems_version: 3.0.3
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: GraphQL Resolver for ActiveRecord Associations
185
+ test_files: []