graphql-sources 1.5.1 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +112 -68
- data/lib/graphql/sources/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fea99d5034323ff95b35a9d8e8d9ef4f549c36972c66f2958767c804a733c34a
|
4
|
+
data.tar.gz: db5ccdc4cabf4d10118e9572ebc88ccff7acf6837f333356db49a2ae5d626ddb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17b6e588ca2f514000c7ee9e3d1ace997b44bdb8ef43049611bf86d86711f1619bf65aa9c26305b33e00a05883a6873ed68902e7e06fcf8c223adad54787caa5
|
7
|
+
data.tar.gz: a66bd4b062d28915d1df9e904877a317ea5cc80f9cac6cbb7a3104503575220d2fce6730551c05f6421dfeb34ee62f8853824379f000d689b88d908a54f86f8f
|
data/README.md
CHANGED
@@ -21,147 +21,138 @@ end
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
### Loading `
|
24
|
+
### Loading `belongs_to` / `has_many` Associations
|
25
25
|
|
26
26
|
```ruby
|
27
|
-
class
|
28
|
-
|
27
|
+
class Purchase < ActiveRecord::Base
|
28
|
+
belongs_to :customer
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
class
|
34
|
-
|
33
|
+
class Customer < ActiveRecord::Base
|
34
|
+
has_many :purchases
|
35
35
|
end
|
36
36
|
```
|
37
37
|
|
38
38
|
```ruby
|
39
|
-
class
|
40
|
-
field :
|
39
|
+
class PurchaseType < GraphQL::Schema::Object
|
40
|
+
field :customer, CustomerType, null: false
|
41
41
|
|
42
|
-
# @return [
|
43
|
-
def
|
44
|
-
# SELECT
|
45
|
-
# SELECT "courses".* FROM "courses" WHERE "courses"."id" IN (...)
|
42
|
+
# @return [Customer]
|
43
|
+
def customer
|
44
|
+
# SELECT * FROM "customers" WHERE "customers"."id" IN (...)
|
46
45
|
dataloader
|
47
|
-
.with(GraphQL::Sources::ActiveRecordAssociation, :
|
46
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :customer)
|
48
47
|
.load(object)
|
49
48
|
end
|
50
49
|
end
|
51
50
|
```
|
52
51
|
|
53
52
|
```ruby
|
54
|
-
class
|
55
|
-
field :
|
53
|
+
class CustomerType < GraphQL::Schema::Object
|
54
|
+
field :purchases, [PurchaseType], null: false
|
56
55
|
|
57
|
-
|
58
|
-
|
59
|
-
# SELECT "courses_students".\* FROM "courses_students" WHERE "courses_students"."course_id" = IN (...)
|
60
|
-
# SELECT "students".\* FROM "students" WHERE "students"."id" IN (...)
|
56
|
+
def purchases
|
57
|
+
# SELECT * FROM "customers" WHERE "customers"."id" IN (...)
|
61
58
|
dataloader
|
62
|
-
.with(GraphQL::Sources::ActiveRecordAssociation, :
|
59
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :purchases)
|
63
60
|
.load(object)
|
64
61
|
end
|
65
62
|
end
|
66
63
|
```
|
67
64
|
|
68
|
-
### Loading `belongs_to` Associations
|
65
|
+
### Loading `belongs_to` / `has_one` Associations
|
69
66
|
|
70
67
|
```ruby
|
71
|
-
class
|
72
|
-
belongs_to :
|
68
|
+
class Profile < ActiveRecord::Base
|
69
|
+
belongs_to :user
|
73
70
|
end
|
74
71
|
```
|
75
72
|
|
76
73
|
```ruby
|
77
|
-
class
|
78
|
-
|
74
|
+
class User < ActiveRecord::Base
|
75
|
+
has_one :profile
|
79
76
|
end
|
80
77
|
```
|
81
78
|
|
82
79
|
```ruby
|
83
|
-
class
|
84
|
-
field :
|
80
|
+
class ProfileType < GraphQL::Schema::Object
|
81
|
+
field :user, [UserType], null: false
|
85
82
|
|
86
|
-
|
83
|
+
# @return [User]
|
84
|
+
def user
|
85
|
+
# SELECT * FROM "users" WHERE "users"."id" IN (...)
|
87
86
|
dataloader
|
88
|
-
.with(GraphQL::Sources::
|
89
|
-
.load(object
|
87
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :user)
|
88
|
+
.load(object)
|
90
89
|
end
|
91
90
|
end
|
92
91
|
```
|
93
92
|
|
94
|
-
### Loading `has_one` Associations
|
95
|
-
|
96
|
-
```ruby
|
97
|
-
class Profile < ActiveRecord::Base
|
98
|
-
belongs_to :user
|
99
|
-
end
|
100
|
-
```
|
101
|
-
|
102
|
-
```ruby
|
103
|
-
class User < ActiveRecord::Base
|
104
|
-
has_one :profile
|
105
|
-
end
|
106
|
-
```
|
107
|
-
|
108
93
|
```ruby
|
109
94
|
class UserType < GraphQL::Schema::Object
|
110
95
|
field :profile, [ProfileType], null: false
|
111
96
|
|
97
|
+
# @return [Profile]
|
112
98
|
def profile
|
99
|
+
# SELECT * FROM "profiles" WHERE "profiles"."id" IN (...)
|
113
100
|
dataloader
|
114
|
-
.with(GraphQL::Sources::
|
115
|
-
.load(object
|
101
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :profile)
|
102
|
+
.load(object)
|
116
103
|
end
|
117
104
|
end
|
118
105
|
```
|
119
106
|
|
120
|
-
|
121
|
-
SELECT "profiles".*
|
122
|
-
FROM "profiles"
|
123
|
-
WHERE "profiles"."user_id" IN (...)
|
124
|
-
ORDER BY "profiles"."id"
|
125
|
-
```
|
126
|
-
|
127
|
-
### Loading `has_many` Associations
|
107
|
+
### Loading `has_and_belongs_to_many` Associations
|
128
108
|
|
129
109
|
```ruby
|
130
|
-
class
|
131
|
-
|
110
|
+
class Student
|
111
|
+
has_and_belongs_to_many :courses
|
132
112
|
end
|
133
113
|
```
|
134
114
|
|
135
115
|
```ruby
|
136
|
-
class
|
137
|
-
|
116
|
+
class Course
|
117
|
+
has_and_belongs_to_many :students
|
138
118
|
end
|
139
119
|
```
|
140
120
|
|
141
121
|
```ruby
|
142
|
-
class
|
143
|
-
field :
|
122
|
+
class StudentType < GraphQL::Schema::Object
|
123
|
+
field :courses, [CourseType], null: false
|
144
124
|
|
145
|
-
|
125
|
+
# @return [Array<Course>]
|
126
|
+
def courses
|
127
|
+
# SELECT * FROM "courses_students" WHERE "courses_students"."student_id" = IN (...)
|
128
|
+
# SELECT * FROM "courses" WHERE "courses"."id" IN (...)
|
146
129
|
dataloader
|
147
|
-
.with(GraphQL::Sources::
|
148
|
-
.load(object
|
130
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :courses)
|
131
|
+
.load(object)
|
149
132
|
end
|
150
133
|
end
|
151
134
|
```
|
152
135
|
|
153
|
-
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
136
|
+
```ruby
|
137
|
+
class CourseType < GraphQL::Schema::Object
|
138
|
+
field :students, [StudentType], null: false
|
139
|
+
|
140
|
+
# @return [Array<Student>]
|
141
|
+
def students
|
142
|
+
# SELECT * FROM "courses_students" WHERE "courses_students"."course_id" = IN (...)
|
143
|
+
# SELECT * FROM "students" WHERE "students"."id" IN (...)
|
144
|
+
dataloader
|
145
|
+
.with(GraphQL::Sources::ActiveRecordAssociation, :students)
|
146
|
+
.load(object)
|
147
|
+
end
|
148
|
+
end
|
158
149
|
```
|
159
150
|
|
160
151
|
### Loading `has_one_attached` Associations
|
161
152
|
|
162
153
|
```ruby
|
163
154
|
class User
|
164
|
-
has_one_attached :
|
155
|
+
has_one_attached :photo
|
165
156
|
end
|
166
157
|
```
|
167
158
|
|
@@ -169,7 +160,13 @@ end
|
|
169
160
|
class UserType < GraphQL::Schema::Object
|
170
161
|
field :avatar, AttachedType, null: false
|
171
162
|
|
163
|
+
# @return [ActiveStorage::Attachment]
|
172
164
|
def avatar
|
165
|
+
# SELECT "active_storage_attachments".*
|
166
|
+
# FROM "active_storage_attachments"
|
167
|
+
# WHERE "active_storage_attachments"."name" = 'avatar'
|
168
|
+
# AND "active_storage_attachments"."record_type" = 'User'
|
169
|
+
# AND "active_storage_attachments"."record_id" IN (...)
|
173
170
|
dataloader
|
174
171
|
.with(GraphQL::Sources::ActiveStorageHasOneAttached, :avatar)
|
175
172
|
.load(object)
|
@@ -189,7 +186,13 @@ end
|
|
189
186
|
class UserType < GraphQL::Schema::Object
|
190
187
|
field :photos, [AttachedType], null: false
|
191
188
|
|
189
|
+
# @return [Array<ActiveStorage::Attachment>]
|
192
190
|
def photos
|
191
|
+
# SELECT "active_storage_attachments".*
|
192
|
+
# FROM "active_storage_attachments"
|
193
|
+
# WHERE "active_storage_attachments"."name" = 'photos'
|
194
|
+
# AND "active_storage_attachments"."record_type" = 'User'
|
195
|
+
# AND "active_storage_attachments"."record_id" IN (...)
|
193
196
|
dataloader
|
194
197
|
.with(GraphQL::Sources::ActiveStorageHasManyAttached, :photos)
|
195
198
|
.load(object)
|
@@ -197,6 +200,47 @@ class UserType < GraphQL::Schema::Object
|
|
197
200
|
end
|
198
201
|
```
|
199
202
|
|
203
|
+
### Loading ActiveRecord Object / ActiveRecord Collection
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
class Event < ActiveRecord::Base
|
207
|
+
belongs_to :device
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
class Device < ActiveRecord::Base
|
213
|
+
has_many :events
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
class EventType < GraphQL::Schema::Object
|
219
|
+
field :device, DeviceType, null: false
|
220
|
+
|
221
|
+
# @return [Device]
|
222
|
+
def device
|
223
|
+
# SELECT * FROM "devices" WHERE "devices"."id" IN (...)
|
224
|
+
dataloader
|
225
|
+
.with(GraphQL::Sources::ActiveRecordObject, ::Device, key: :id)
|
226
|
+
.load(object.device_id)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
```
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
class DeviceType < GraphQL::Schema::Object
|
233
|
+
field :events, [EventType], null: false
|
234
|
+
|
235
|
+
def events
|
236
|
+
# SELECT * FROM "events" WHERE "events"."device_id" IN (...)
|
237
|
+
dataloader
|
238
|
+
.with(GraphQL::Sources::ActiveRecordCollection, ::Event, key: :device_id)
|
239
|
+
.load(object)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
```
|
243
|
+
|
200
244
|
### Loading Counts
|
201
245
|
|
202
246
|
```ruby
|