graphql-sources 1.4.0 → 1.5.0

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: b83f8f866fbc38f5645d8887f0de9952e56126bdd0a48b0e2d5bca5099b4b110
4
- data.tar.gz: bba362e0571ae8a9abdb344a6d17cb4894fac92a5d8f67ac7f556e0a336d6459
3
+ metadata.gz: 94b282ba84b9627d96828c4ebe8b73b609bcd61645bb67ee3894d7850fadbffb
4
+ data.tar.gz: e8aee59b0d9ad8cfe65dc2203b285ca6e174c7a2fa7dae1250bb9b3e015176eb
5
5
  SHA512:
6
- metadata.gz: 696372d3f88a6db7d223c26ec3a27c77138a4473d03dc4285c8904f79271d2f044762133af5e7fa88c475c4ae2b035a66a4fa4caab699004b7dd2103ae9ce63a
7
- data.tar.gz: 9f7014bd5a85a5c6f89e55da33f989f4939caf00411876d024f93cafa5b115e412668aa3cd8bb80a6cc168d4c9ababdc4484c340e1b772f8ed2a721ba2492c5a
6
+ metadata.gz: 6ada131108e0dc2a804eb6e35eedcfc1fc0d20fd259271109091761cdebeda39a20ff25677a3638300489b25480ab71b821e48ceca997ad5602a7414e7081416
7
+ data.tar.gz: 84643b509932cd1f82cd9ebc9cdbb739c5a7ef8e7460114f84d0929c592b3f376284d14b428b47e0a044e9284b13a656961a68745cc59f6bd93eb538769d3e7f
data/Gemfile CHANGED
@@ -7,3 +7,4 @@ gemspec
7
7
  gem 'rake'
8
8
  gem 'rspec'
9
9
  gem 'rubocop'
10
+ gem 'yard'
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Sources
5
- VERSION = '1.4.0'
5
+ VERSION = '1.5.0'
6
6
  end
7
7
  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.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-02 00:00:00.000000000 Z
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
@@ -249,7 +250,7 @@ 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.11
253
+ rubygems_version: 3.5.22
253
254
  signing_key:
254
255
  specification_version: 4
255
256
  summary: A set of common GraphQL DataLoader sources.