Class: GraphQL::Dataloader::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/dataloader/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataloaderObject (readonly)

Returns the value of attribute dataloader.



18
19
20
# File 'lib/graphql/dataloader/source.rb', line 18

def dataloader
  @dataloader
end

#pending_keysObject (readonly)

Returns the value of attribute pending_keys.



140
141
142
# File 'lib/graphql/dataloader/source.rb', line 140

def pending_keys
  @pending_keys
end

Class Method Details

.batch_key_for(*batch_args, **batch_kwargs) ⇒ Object

These arguments are given to dataloader.with(source_class, ...). The object returned from this method is used to de-duplicate batch loads under the hood by using it as a Hash key.

By default, the arguments are all put in an Array. To customize how this source’s batches are merged, override this method to return something else.

For example, if you pass ActiveRecord::Relations to .with(...), you could override this method to call .to_sql on them, thus merging .load(...) calls when they apply to equivalent relations.

Parameters:

  • batch_args (Array<Object>)
  • batch_kwargs (Hash)

Returns:

  • (Object)


136
137
138
# File 'lib/graphql/dataloader/source.rb', line 136

def self.batch_key_for(*batch_args, **batch_kwargs)
  [*batch_args, **batch_kwargs]
end

Instance Method Details

#fetch(keys) ⇒ Array<Object>

Subclasses must implement this method to return a value for each of keys

Parameters:

Returns:

  • (Array<Object>)

    A loaded value for each of keys. The array must match one-for-one to the list of keys.



62
63
64
65
# File 'lib/graphql/dataloader/source.rb', line 62

def fetch(keys)
  # somehow retrieve these from the backend
  raise "Implement `#{self.class}#fetch(#{keys.inspect}) to return a record for each of the keys"
end

#load(key) ⇒ Object

Returns The result from #fetch for key. If key hasn’t been loaded yet, the Fiber will yield until it’s loaded.

Parameters:

  • key (Object)

    A loading key which will be passed to #fetch if it isn’t already in the internal cache.

Returns:

  • (Object)

    The result from #fetch for key. If key hasn’t been loaded yet, the Fiber will yield until it’s loaded.



37
38
39
40
41
42
43
44
45
# File 'lib/graphql/dataloader/source.rb', line 37

def load(key)
  if @results.key?(key)
    result_for(key)
  else
    @pending_keys << key
    sync
    result_for(key)
  end
end

#load_all(keys) ⇒ Object

Returns The result from #fetch for keys. If keys haven’t been loaded yet, the Fiber will yield until they’re loaded.

Parameters:

  • keys (Array<Object>)

    Loading keys which will be passed to #fetch (or read from the internal cache).

Returns:

  • (Object)

    The result from #fetch for keys. If keys haven’t been loaded yet, the Fiber will yield until they’re loaded.



49
50
51
52
53
54
55
56
57
# File 'lib/graphql/dataloader/source.rb', line 49

def load_all(keys)
  if keys.any? { |k| !@results.key?(k) }
    pending_keys = keys.select { |k| !@results.key?(k) }
    @pending_keys.concat(pending_keys)
    sync
  end

  keys.map { |k| result_for(k) }
end

#merge(results) ⇒ void

This method returns an undefined value.

Add these key-value pairs to this source’s cache (future loads will use these merged values).

Parameters:

  • results (Hash<Object => Object>)

    key-value pairs to cache in this source



93
94
95
96
# File 'lib/graphql/dataloader/source.rb', line 93

def merge(results)
  @results.merge!(results)
  nil
end

#pending?Boolean

Returns True if this source has any pending requests for data.

Returns:

  • (Boolean)

    True if this source has any pending requests for data.



85
86
87
# File 'lib/graphql/dataloader/source.rb', line 85

def pending?
  !@pending_keys.empty?
end

#request(key) ⇒ Dataloader::Request

Returns a pending request for a value from key. Call .load on that object to wait for the result.

Returns:

  • (Dataloader::Request)

    a pending request for a value from key. Call .load on that object to wait for the result.



21
22
23
24
25
26
# File 'lib/graphql/dataloader/source.rb', line 21

def request(key)
  if !@results.key?(key)
    @pending_keys << key
  end
  Dataloader::Request.new(self, key)
end

#request_all(keys) ⇒ Dataloader::Request

Returns a pending request for a values from keys. Call .load on that object to wait for the results.

Returns:

  • (Dataloader::Request)

    a pending request for a values from keys. Call .load on that object to wait for the results.



29
30
31
32
33
# File 'lib/graphql/dataloader/source.rb', line 29

def request_all(keys)
  pending_keys = keys.select { |k| !@results.key?(k) }
  @pending_keys.concat(pending_keys)
  Dataloader::RequestAll.new(self, keys)
end

#run_pending_keysvoid

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.

This method returns an undefined value.

Called by GraphQL::Dataloader to resolve and pending requests to this source.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/graphql/dataloader/source.rb', line 101

def run_pending_keys
  if !@fetching_keys.empty?
    @pending_keys -= @fetching_keys
  end
  return if @pending_keys.empty?
  fetch_keys = @pending_keys.uniq
  @fetching_keys.concat(fetch_keys)
  @pending_keys = []
  results = fetch(fetch_keys)
  fetch_keys.each_with_index do |key, idx|
    @results[key] = results[idx]
  end
  nil
rescue StandardError => error
  fetch_keys.each { |key| @results[key] = error }
ensure
  if fetch_keys
    @fetching_keys -= fetch_keys
  end
end

#setup(dataloader) ⇒ 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.

Called by GraphQL::Dataloader to prepare the GraphQL::Dataloader::Source’s internal state



8
9
10
11
12
13
14
15
16
# File 'lib/graphql/dataloader/source.rb', line 8

def setup(dataloader)
  # These keys have been requested but haven't been fetched yet
  @pending_keys = []
  # These keys have been passed to `fetch` but haven't been finished yet
  @fetching_keys = []
  # { key => result }
  @results = {}
  @dataloader = dataloader
end

#syncvoid

This method returns an undefined value.

Wait for a batch, if there’s anything to batch. Then run the batch and update the cache.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/graphql/dataloader/source.rb', line 70

def sync
  pending_keys = @pending_keys.dup
  @dataloader.yield
  iterations = 0
  while pending_keys.any? { |k| !@results.key?(k) }
    iterations += 1
    if iterations > 1000
      raise "#{self.class}#sync tried 1000 times to load pending keys (#{pending_keys}), but they still weren't loaded. There is likely a circular dependency."
    end
    @dataloader.yield
  end
  nil
end