Skip to content

Commit b07f1d2

Browse files
committed
Better docs for has_one_attached / has_many_attached
1 parent ad117ab commit b07f1d2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ class CourseType < GraphQL::Schema::Object
148148
end
149149
```
150150

151+
### Loading `has_one_attached` Associations
152+
153+
```ruby
154+
class User
155+
has_one_attached :photo
156+
end
157+
```
158+
159+
```ruby
160+
class UserType < GraphQL::Schema::Object
161+
field :avatar, AttachedType, null: false
162+
163+
# @return [ActiveStorage::Attachment]
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 (...)
170+
dataloader
171+
.with(GraphQL::Sources::ActiveStorageHasOneAttached, :avatar)
172+
.load(object)
173+
end
174+
end
175+
```
176+
151177
### Loading `has_many_attached` Associations
152178

153179
```ruby
@@ -160,7 +186,13 @@ end
160186
class UserType < GraphQL::Schema::Object
161187
field :photos, [AttachedType], null: false
162188

189+
# @return [Array<ActiveStorage::Attachment>]
163190
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 (...)
164196
dataloader
165197
.with(GraphQL::Sources::ActiveStorageHasManyAttached, :photos)
166198
.load(object)

0 commit comments

Comments
 (0)