Module: GraphQL::Schema::Member::HasDataloader Private

Included in:
Interface::DefinitionMethods, Object, Resolver
Defined in:
lib/graphql/schema/member/has_dataloader.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#dataload(source_class, *source_args, load_key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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 in source_class’s initialize method

  • load_key (Object)

    The key to look up using def fetch



17
18
19
# File 'lib/graphql/schema/member/has_dataloader.rb', line 17

def dataload(source_class, *source_args, load_key)
  dataloader.with(source_class, *source_args).load(load_key)
end

#dataload_association(record = object, association_name, scope: nil) ⇒ ActiveRecord::Base?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Look up an associated record using a Rails association.

Examples:

Looking up a belongs_to on the current object

dataload_association(:parent) # Equivalent to `object.parent`, but dataloaded

Looking up an associated record on some other object

dataload_association(:post, comment) # Equivalent to `comment.post`, but dataloaded

Parameters:

  • association_name (Symbol)

    A belongs_to or has_one association. (If a has_many association is named here, it will be selected without pagination.)

  • record (ActiveRecord::Base) (defaults to: object)

    The object that the association belongs to.

  • scope (ActiveRecord::Relation) (defaults to: nil)

    A scope to look up the associated record in

Returns:

  • (ActiveRecord::Base, nil)

    The associated record, if there is one



45
46
47
48
49
50
51
52
# File 'lib/graphql/schema/member/has_dataloader.rb', line 45

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

#dataload_record(model, find_by_value, find_by: nil) ⇒ ActiveRecord::Base?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find an object with ActiveRecord via Dataloader::ActiveRecordSource.

Parameters:

  • model (Class<ActiveRecord::Base>)
  • find_by_value (Object)

    Usually an id, might be another value if find_by: is also provided

  • find_by (Symbol, String) (defaults to: nil)

    A column name to look the record up by. (Defaults to the model’s primary key.)

Returns:

  • (ActiveRecord::Base, nil)


26
27
28
29
30
31
32
33
34
# File 'lib/graphql/schema/member/has_dataloader.rb', line 26

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

#dataloaderGraphQL::Dataloader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The dataloader for the currently-running query.

Returns:



8
9
10
# File 'lib/graphql/schema/member/has_dataloader.rb', line 8

def dataloader
  context.dataloader
end