Class: GraphQL::Sources::ActiveRecordExists
- Inherits:
-
ActiveRecordBase
- Object
- Dataloader::Source
- ActiveRecordBase
- GraphQL::Sources::ActiveRecordExists
- Defined in:
- lib/graphql/sources/active_record_exists.rb
Overview
A class for checking existence of records.
class Post
has_many :likes
end
class Like
belongs_to :post
end
class PostType < GraphQL::Schema::Object
field :likes, Integer, null: false
def likes
dataloader
.with(GraphQL::Sources::ActiveRecordExists, ::Like, key: :post_id)
.load(object.id)
end
end
The resulting SQL query is:
SELECT "likes"."post_id"
FROM "likes"
WHERE "likes"."post_id" IN (1, 2, 3, ...)
GROUP BY "likes"."post_id"
Instance Method Summary collapse
-
#fetch(keys) ⇒ Array<Boolean>
An array of booleans.
Methods inherited from ActiveRecordBase
Constructor Details
This class inherits a constructor from GraphQL::Sources::ActiveRecordBase
Instance Method Details
#fetch(keys) ⇒ Array<Boolean>
Returns an array of booleans.
34 35 36 37 |
# File 'lib/graphql/sources/active_record_exists.rb', line 34 def fetch(keys) set = Set.new(models(keys: keys).group(@key).pluck(@key)) keys.map { |key| set.member?(key) } end |