MCPcopy Index your code
hub / github.com/graphql/dataloader / load

Method load

src/index.js:74–113  ·  view source on GitHub ↗

* Loads a key, returning a `Promise` for the value represented by that key.

(key: K)

Source from the content-addressed store, hash-verified

72 * Loads a key, returning a `Promise` for the value represented by that key.
73 */
74 load(key: K): Promise<V> {
75 if (key === null || key === undefined) {
76 throw new TypeError(
77 'The loader.load() function must be called with a value, ' +
78 `but got: ${String(key)}.`,
79 );
80 }
81
82 const batch = getCurrentBatch(this);
83 const cacheMap = this._cacheMap;
84 let cacheKey: ?C;
85
86 // If caching and there is a cache-hit, return cached Promise.
87 if (cacheMap) {
88 cacheKey = this._cacheKeyFn(key);
89 const cachedPromise = cacheMap.get(cacheKey);
90 if (cachedPromise) {
91 const cacheHits = batch.cacheHits || (batch.cacheHits = []);
92 return new Promise(resolve => {
93 cacheHits.push(() => {
94 resolve(cachedPromise);
95 });
96 });
97 }
98 }
99
100 // Otherwise, produce a new Promise for this key, and enqueue it to be
101 // dispatched along with the current batch.
102 batch.keys.push(key);
103 const promise = new Promise((resolve, reject) => {
104 batch.callbacks.push({ resolve, reject });
105 });
106
107 // If caching, cache this promise.
108 if (cacheMap) {
109 cacheMap.set((cacheKey: any), promise);
110 }
111
112 return promise;
113 }
114
115 /**
116 * Loads multiple keys, promising an array of values:

Callers 6

loadManyMethod · 0.95
abuse.test.jsFile · 0.80
oldbrowser.test.jsFile · 0.80
unhandled.test.jsFile · 0.80
browser.test.jsFile · 0.80
dataloader.test.jsFile · 0.80

Calls 3

getCurrentBatchFunction · 0.85
setMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected