Skip to content

Commit 99aaf40

Browse files
committed
Add the fetch response to the cache event payload.
1 parent e11e886 commit 99aaf40

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Next
44

5+
### Minor
6+
7+
- Added a `response` property to the `GraphQL` instance `cache` event payload, containing the original `fetch` [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance the `cacheValue` was derived from.
8+
59
### Patch
610

711
- Updated dependencies.

src/test/GraphQL.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ t.test('GraphQL.operate()', async t => {
4848
}
4949
}),
5050
expectedResolvedCacheValue,
51+
expectedResponseType = Response,
5152
callback
5253
}) => async t => {
5354
const fetchEvent = promisifyEvent(graphql, 'fetch')
@@ -125,6 +126,12 @@ t.test('GraphQL.operate()', async t => {
125126
'GraphQL `cache` event data property `cacheValue`'
126127
)
127128

129+
t.type(
130+
cacheEventData.response,
131+
expectedResponseType,
132+
'GraphQL `cache` event data property `response`'
133+
)
134+
128135
if (resetEvent) {
129136
const resetEventData = await resetEvent
130137
t.equals(
@@ -193,7 +200,8 @@ t.test('GraphQL.operate()', async t => {
193200
port,
194201
expectedResolvedCacheValue: {
195202
fetchError: 'Global fetch API or polyfill unavailable.'
196-
}
203+
},
204+
expectedResponseType: 'undefined'
197205
})
198206
)
199207

src/universal/GraphQL.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export class GraphQL {
131131
* @ignore
132132
*/
133133
fetch = ({ url, ...options }, cacheKey) => {
134+
let fetchResponse
135+
134136
const fetcher =
135137
typeof fetch === 'function'
136138
? fetch
@@ -142,6 +144,8 @@ export class GraphQL {
142144
const cacheValuePromise = fetcher(url, options)
143145
.then(
144146
response => {
147+
fetchResponse = response
148+
145149
if (!response.ok)
146150
cacheValue.httpError = {
147151
status: response.status,
@@ -172,7 +176,13 @@ export class GraphQL {
172176
// Clear the loaded operation.
173177
delete this.operations[cacheKey]
174178

175-
this.emit('cache', { cacheKey, cacheValue })
179+
this.emit('cache', {
180+
cacheKey,
181+
cacheValue,
182+
183+
// May be undefined if there was a fetch error.
184+
response: fetchResponse
185+
})
176186

177187
return cacheValue
178188
})

0 commit comments

Comments
 (0)