Skip to content

Commit 1687089

Browse files
author
dustin deus
committed
fix request memoization
1 parent e3b08bf commit 1687089

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/http-data-source.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
7575
private logger?: Logger
7676
private cache!: KeyValueCache<string>
7777
private globalRequestOptions?: RequestOptions
78-
private readonly memoizedResults: QuickLRU<string, Response<any>>
78+
private readonly memoizedResults: QuickLRU<string, Promise<Response<any>>>
7979

8080
constructor(public readonly baseURL: string, private readonly options?: HTTPDataSourceOptions) {
8181
super()
@@ -297,9 +297,10 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
297297
// a single instance of the data sources is scoped to one graphql request
298298
const cachedResponse = this.memoizedResults.get(cacheKey)
299299
if (cachedResponse) {
300-
cachedResponse.memoized = true
301-
cachedResponse.isFromCache = false
302-
return cachedResponse
300+
const response = await cachedResponse
301+
response.memoized = true
302+
response.isFromCache = false
303+
return response
303304
}
304305
}
305306

@@ -320,11 +321,8 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
320321
}
321322
}
322323

323-
const response = await this.performRequest<TResult>(options, cacheKey)
324-
325-
if (this.isResponseCacheable<TResult>(options, response)) {
326-
this.memoizedResults.set(cacheKey, response)
327-
}
324+
const response = this.performRequest<TResult>(options, cacheKey)
325+
this.memoizedResults.set(cacheKey, response)
328326

329327
return response
330328
}

test/http-data-source.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ test('Should memoize subsequent GET calls to the same endpoint', async (t) => {
242242
const server = http.createServer((req, res) => {
243243
t.is(req.method, 'GET')
244244
res.write(JSON.stringify(wanted))
245-
res.end()
245+
setTimeout(() => res.end(), 200).unref()
246246
res.socket?.unref()
247247
})
248248

0 commit comments

Comments
 (0)