graphql-sources 1.4.0 → 1.5.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 +4 -4
- data/Gemfile +1 -0
- data/README.md +44 -0
- data/lib/graphql/sources/active_record_association.rb +127 -0
- data/lib/graphql/sources/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91917cfffbb3f024443e890da6294499029638a4673e9ef96d1b061117356016
|
4
|
+
data.tar.gz: 5f27027876a07800b499a9cd22832707e0b2fdb74778896d3b8b53693ce3df17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61889bbe42856b8d18e11bf3cd0dbb3f8eeea81e979f97b79f5f8334083636514637746db0d064caa2e4db45eaefbfe3af842361f0f03c436f59f3f70a40bdc0
|
7
|
+
data.tar.gz: 932074f604d8490897ef8868e0f8562e04c715a3c0e89f670682f245644b25513743146c8e22d2ca35fbe969f79035485269a12c31914fbe69c6f19419beb39d
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -21,6 +21,50 @@ end
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
+
### Loading `has_and_belongs_to_many` Associations
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Student
|
28
|
+
has_and_belongs_to_many :courses
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class Course
|
34
|
+
has_and_belongs_to_many :students
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class StudentType < GraphQL::Schema::Object
|
40
|
+
field :courses, [CourseType], null: false
|
41
|
+
|
42
|
+
# @return [Array<Course>]
|
43
|
+
def courses
|
44
|
+
# SELECT "courses_students".* FROM "courses_students" WHERE "courses_students"."student_id" = IN (...)
|
45
|
+
# SELECT "courses".* FROM "courses" WHERE "courses"."id" IN (...)
|
46
|
+
dataloader
|
47
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :courses)
|
48
|
+
.load(object)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class CourseType < GraphQL::Schema::Object
|
55
|
+
field :students, [StudentType], null: false
|
56
|
+
|
57
|
+
# @return [Array<Student>]
|
58
|
+
def students
|
59
|
+
# SELECT "courses_students".\* FROM "courses_students" WHERE "courses_students"."course_id" = IN (...)
|
60
|
+
# SELECT "students".\* FROM "students" WHERE "students"."id" IN (...)
|
61
|
+
dataloader
|
62
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :students)
|
63
|
+
.load(object)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
24
68
|
### Loading `belongs_to` Associations
|
25
69
|
|
26
70
|
```ruby
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Sources
|
5
|
+
# A class for loading an ActiveRecord association.
|
6
|
+
#
|
7
|
+
# @example `has_and_belongs_to_many`
|
8
|
+
#
|
9
|
+
# class Student
|
10
|
+
# has_and_belongs_to_many :courses
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# class Course
|
14
|
+
# has_and_belongs_to_many :students
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# class StudentType < GraphQL::Schema::Object
|
18
|
+
# field :courses, [CourseType], null: false
|
19
|
+
#
|
20
|
+
# # @return [Array<Course>]
|
21
|
+
# def courses
|
22
|
+
# # SELECT "courses_students".* FROM "courses_students" WHERE "courses_students"."student_id" = IN (...)
|
23
|
+
# # SELECT "courses".* FROM "courses" WHERE "courses"."id" IN (...)
|
24
|
+
# dataloader
|
25
|
+
# .with(GraphQL::Sources::ActiveRecordAssociation, :courses)
|
26
|
+
# .load(object)
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# class CourseType < GraphQL::Schema::Object
|
31
|
+
# field :students, [StudentType], null: false
|
32
|
+
#
|
33
|
+
# # @return [Array<Student>]
|
34
|
+
# def students
|
35
|
+
# # SELECT "courses_students".* FROM "courses_students" WHERE "courses_students"."course_id" = IN (...)
|
36
|
+
# # SELECT "students".* FROM "students" WHERE "students"."id" IN (...)
|
37
|
+
# dataloader
|
38
|
+
# .with(GraphQL::Sources::ActiveRecordAssociation, :students)
|
39
|
+
# .load(object)
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
#
|
44
|
+
# @example `has_many` / `belongs_to`
|
45
|
+
#
|
46
|
+
# class User
|
47
|
+
# has_many :comments
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# class Comment
|
51
|
+
# belongs_to :user
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# class UserType < GraphQL::Schema::Object
|
55
|
+
# field :comments, [CommentType], null: false
|
56
|
+
#
|
57
|
+
# # @return [Array<Comment>]
|
58
|
+
# def comments
|
59
|
+
# # SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = IN (...)
|
60
|
+
# dataloader
|
61
|
+
# .with(GraphQL::Sources::ActiveRecordAssociation, :comments)
|
62
|
+
# .load(object)
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# class CommentType < GraphQL::Schema::Object
|
67
|
+
# field :user, UserType, null: false
|
68
|
+
#
|
69
|
+
# # @return [User]
|
70
|
+
# def user
|
71
|
+
# # SELECT "users".* FROM "users" WHERE "users"."id" IN (...)
|
72
|
+
# dataloader
|
73
|
+
# .with(GraphQL::Sources::ActiveRecordAssociation, :user)
|
74
|
+
# .load(object)
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# @example `has_one` / `belongs_to`
|
79
|
+
#
|
80
|
+
# class User
|
81
|
+
# has_one :profile
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# class Profile
|
85
|
+
# belongs_to :user
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# class UserType < GraphQL::Schema::Object
|
89
|
+
# field :profile, ProfileType, null: false
|
90
|
+
#
|
91
|
+
# # @return [Profile]
|
92
|
+
# def profile
|
93
|
+
# # SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = IN (...)
|
94
|
+
# dataloader
|
95
|
+
# .with(GraphQL::Sources::ActiveRecordAssociation, :profile)
|
96
|
+
# .load(object)
|
97
|
+
# end
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# class ProfileType < GraphQL::Schema::Object
|
101
|
+
# field :user, UserType, null: false
|
102
|
+
#
|
103
|
+
# # @return [User]
|
104
|
+
# def user
|
105
|
+
# # SELECT "users".* FROM "users" WHERE "users"."id" IN (...)
|
106
|
+
# dataloader
|
107
|
+
# .with(GraphQL::Sources::ActiveRecordAssociation, :user)
|
108
|
+
# .load(object)
|
109
|
+
# end
|
110
|
+
# end
|
111
|
+
class ActiveRecordAssociation < GraphQL::Dataloader::Source
|
112
|
+
# @param association [Symbol] an association to use for loading (e.g. :user)
|
113
|
+
def initialize(association)
|
114
|
+
super()
|
115
|
+
@association = association
|
116
|
+
end
|
117
|
+
|
118
|
+
# @param records [Array<ActiveRecord::Base>] an array of records
|
119
|
+
def fetch(records)
|
120
|
+
preloader = ActiveRecord::Associations::Preloader.new(records: records, associations: [@association])
|
121
|
+
preloader.call
|
122
|
+
|
123
|
+
records.map { |record| record.association(@association).target }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-sources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|
@@ -217,6 +217,7 @@ files:
|
|
217
217
|
- LICENSE
|
218
218
|
- README.md
|
219
219
|
- lib/graphql/sources.rb
|
220
|
+
- lib/graphql/sources/active_record_association.rb
|
220
221
|
- lib/graphql/sources/active_record_base.rb
|
221
222
|
- lib/graphql/sources/active_record_collection.rb
|
222
223
|
- lib/graphql/sources/active_record_count.rb
|
@@ -234,7 +235,7 @@ metadata:
|
|
234
235
|
homepage_uri: https://github.com/ksylvest/graphql-sources
|
235
236
|
source_code_uri: https://github.com/ksylvest/graphql-sources
|
236
237
|
changelog_uri: https://github.com/ksylvest/graphql-sources/blob/main/CHANGELOG.md
|
237
|
-
post_install_message:
|
238
|
+
post_install_message:
|
238
239
|
rdoc_options: []
|
239
240
|
require_paths:
|
240
241
|
- lib
|
@@ -249,8 +250,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
249
250
|
- !ruby/object:Gem::Version
|
250
251
|
version: '0'
|
251
252
|
requirements: []
|
252
|
-
rubygems_version: 3.5.
|
253
|
-
signing_key:
|
253
|
+
rubygems_version: 3.5.18
|
254
|
+
signing_key:
|
254
255
|
specification_version: 4
|
255
256
|
summary: A set of common GraphQL DataLoader sources.
|
256
257
|
test_files: []
|