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

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

Overview

API: public Shared methods for working with Dataloader inside GraphQL runtime objects.

Instance Method Summary collapse

Instance Method Details

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

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

:call-seq:

dataload(Class[GraphQL::Dataloader::Source] source_class, Array[Object] *source_args, Object load_key)


31
32
33
# File 'lib/graphql/schema/member/has_dataloader.rb', line 31

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

#dataload_all(source_class, *source_args, load_keys) ⇒ Object

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 in source_class's initialize method
  • load_keys (Array<Object>) — The keys to look up using def fetch

:call-seq:

dataload_all(Class[GraphQL::Dataloader::Source] source_class, Array[Object] *source_args, Array[Object] load_keys)


56
57
58
# File 'lib/graphql/schema/member/has_dataloader.rb', line 56

def dataload_all(source_class, *source_args, load_keys)
  dataloader.with(source_class, *source_args).load_all(load_keys)
end

#dataload_all_associations(records, association_name, scope: nil) ⇒ Object

See dataload_association Like dataload_assocation but accepts an Array of records (required param)



146
147
148
149
150
151
152
153
# File 'lib/graphql/schema/member/has_dataloader.rb', line 146

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

#dataload_all_records(model, find_by_values, find_by: nil) ⇒ Object

See dataload_record Like dataload_record, but accepts an Array of find_by_values



99
100
101
102
103
104
105
106
# File 'lib/graphql/schema/member/has_dataloader.rb', line 99

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

#dataload_association(record = object, association_name, scope: nil) ⇒ Object

Look up an associated record using a Rails association (via Dataloader::ActiveRecordAssociationSource)

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) — 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

:call-seq:

dataload_association(ActiveRecord::Base record, Symbol association_name, ActiveRecord::Relation scope:) -> ActiveRecord::Base | nil


136
137
138
139
140
141
142
143
# File 'lib/graphql/schema/member/has_dataloader.rb', line 136

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) ⇒ Object

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) — 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

:call-seq:

dataload_record(Class[ActiveRecord::Base] model, Object find_by_value, Symbol | String find_by:) -> ActiveRecord::Base | nil


88
89
90
91
92
93
94
95
96
# File 'lib/graphql/schema/member/has_dataloader.rb', line 88

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

#dataloader ⇒ Object

Returns

  • GraphQL::Dataloader — The dataloader for the currently-running query

:call-seq:

dataloader() -> GraphQL::Dataloader


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

def dataloader
  context.dataloader
end