Class: GraphQL::Sources::ActiveRecordCollection
- Inherits:
-
ActiveRecordBase
- Object
- Dataloader::Source
- ActiveRecordBase
- GraphQL::Sources::ActiveRecordCollection
- Defined in:
- lib/graphql/sources/active_record_collection.rb
Overview
A class for loading ‘has_many` style associations.
class User
has_many :comments
end
class Comment
belongs_to :user
end
class UserType < GraphQL::Schema::Object
field :comments, [CommentType], null: false
def comments
dataloader
.with(GraphQL::Sources::ActiveRecordCollection, ::Comment, key: :user_id)
.load(object.id)
end
end
The resulting SQL query is:
SELECT "comments".*
FROM "comments"
WHERE "comments"."user_id" IN (...)
ORDER BY "comments"."id"
Instance Method Summary collapse
-
#fetch(keys) ⇒ Array
Grouped records mirroring the keys.
Methods inherited from ActiveRecordBase
Constructor Details
This class inherits a constructor from GraphQL::Sources::ActiveRecordBase
Instance Method Details
#fetch(keys) ⇒ Array
Returns grouped records mirroring the keys.
34 35 36 37 38 39 40 |
# File 'lib/graphql/sources/active_record_collection.rb', line 34 def fetch(keys) models = models(keys: keys).order(:id).load_async dataloader.yield map = models.group_by { |model| model[@key] } keys.map { |key| map[key] || [] } end |