module GraphQL::Schema::Member::HasDataloader
API: public Shared methods for working with Dataloader inside GraphQL runtime objects.
Public Instance Methods
# File lib/graphql/schema/member/has_dataloader.rb, line 30 def dataload(source_class, *source_args, load_key) dataloader.with(source_class, *source_args).load(load_key) end
A shortcut method for loading a key from a source. Identical to dataloader.with(source_class, *source_args).load(load_key)
Parameters
-
source_class(Class<GraphQL::Dataloader::Source>) -
source_args(Array<Object>) β Any extra parameters defined insource_classβsinitializemethod -
load_key(Object) β The key to look up usingdef fetch
# File lib/graphql/schema/member/has_dataloader.rb, line 55 def dataload_all(source_class, *source_args, load_keys) dataloader.with(source_class, *source_args).load_all(load_keys) end
A shortcut method for loading many keys from a source. Identical to dataloader.with(source_class, *source_args).load_all(load_keys)
Examples
Example: field :score, Integer, resolve_batch: true
def self.score(posts) dataload_all(PostScoreSource, posts.map(&:id)) end
Parameters
-
source_class(Class<GraphQL::Dataloader::Source>) -
source_args(Array<Object>) β Any extra parameters defined insource_classβsinitializemethod -
load_keys(Array<Object>) β The keys to look up usingdef fetch
# File lib/graphql/schema/member/has_dataloader.rb, line 145 def dataload_all_associations(records, association_name, scope: nil) source = if scope dataloader.with(Dataloader::ActiveRecordAssociationSource, association_name, scope) else dataloader.with(Dataloader::ActiveRecordAssociationSource, association_name) end source.load_all(records) end
See dataload_association Like dataload_assocation but accepts an Array of records (required param)
# File lib/graphql/schema/member/has_dataloader.rb, line 98 def dataload_all_records(model, find_by_values, find_by: nil) source = if find_by dataloader.with(Dataloader::ActiveRecordSource, model, find_by: find_by) else dataloader.with(Dataloader::ActiveRecordSource, model) end source.load_all(find_by_values) end
See dataload_record Like dataload_record, but accepts an Array of find_by_values
# File lib/graphql/schema/member/has_dataloader.rb, line 135 def dataload_association(record = object, association_name, scope: nil) source = if scope dataloader.with(Dataloader::ActiveRecordAssociationSource, association_name, scope) else dataloader.with(Dataloader::ActiveRecordAssociationSource, association_name) end source.load(record) end
Look up an associated record using a Rails association (via Dataloader::ActiveRecordAssociationSource)
Parameters
-
association_name(Symbol) β Abelongs_toorhas_oneassociation. (If ahas_manyassociation is named here, it will be selected without pagination.) -
record(ActiveRecord::Base) β The object that the association belongs to. -
scope(ActiveRecord::Relation) β A scope to look up the associated record in
Returns
-
ActiveRecord::Base, nilβ The associated record, if there is one
Examples
Example: Looking up a belongs_to on the current object
dataload_association(:parent) # Equivalent to `object.parent`, but dataloaded
Example: Looking up an associated record on some other object
dataload_association(comment, :post) # Equivalent to `comment.post`, but dataloaded
# File lib/graphql/schema/member/has_dataloader.rb, line 87 def dataload_record(model, find_by_value, find_by: nil) source = if find_by dataloader.with(Dataloader::ActiveRecordSource, model, find_by: find_by) else dataloader.with(Dataloader::ActiveRecordSource, model) end source.load(find_by_value) end
Find an object with ActiveRecord via Dataloader::ActiveRecordSource.
Parameters
-
model(Class<ActiveRecord::Base>) -
find_by_value(Object) β Usually anid, might be another value iffind_by:is also provided -
find_by(Symbol, String) β A column name to look the record up by. (Defaults to the modelβs primary key.)
Returns
-
ActiveRecord::Base, nil
Examples
Example: Finding a record by ID
dataload_record(Post, 5) # Like `Post.find(5)`, but dataloaded
Example: Finding a record by another attribute
dataload_record(User, "matz", find_by: :handle) # Like `User.find_by(handle: "matz")`, but dataloaded
# File lib/graphql/schema/member/has_dataloader.rb, line 15 def dataloader context.dataloader end
Returns
-
GraphQL::Dataloaderβ The dataloader for the currently-running query