Class: GraphQL::Sources::ActiveStorageHasManyAttached
- Inherits:
-
ActiveStorageBase
- Object
- Dataloader::Source
- ActiveStorageBase
- GraphQL::Sources::ActiveStorageHasManyAttached
- Defined in:
- lib/graphql/sources/active_storage_has_many_attached.rb
Overview
A class for loading ‘has_many_attached` style associations.
class User
has_many_attached :photos
end
class UserType < GraphQL::Schema::Object
field :photos, [AttachedType], null: false
def photos
dataloader
.with(GraphQL::Sources::ActiveStorageHasManyAttached, :photos)
.load(object)
end
end
The resulting SQL query is:
SELECT "active_storage_attachments".*
FROM "active_storage_attachments"
WHERE "active_storage_attachments"."name" = 'photos'
AND "active_storage_attachments"."record_type" = 'User'
AND "active_storage_attachments"."record_id" IN (...)
Instance Method Summary collapse
-
#fetch(records) ⇒ Array
Grouped attachments mirroring the keys.
Methods inherited from ActiveStorageBase
Constructor Details
This class inherits a constructor from GraphQL::Sources::ActiveStorageBase
Instance Method Details
#fetch(records) ⇒ Array
Returns grouped attachments mirroring the keys.
31 32 33 34 35 36 37 |
# File 'lib/graphql/sources/active_storage_has_many_attached.rb', line 31 def fetch(records) = (records: records).load_async dataloader.yield map = .group_by { || [.record_type, .record_id] } records.map { |record| map[[record.class.name, record.id]] || [] } end |