forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockLink.js
More file actions
27 lines (25 loc) · 840 Bytes
/
mockLink.js
File metadata and controls
27 lines (25 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ApolloLink } from '@apollo/client'
import { MockLink } from '@apollo/client/testing'
const errorLink = new ApolloLink((operation, forward) => {
console.log(`starting request for ${operation.operationName}`)
const observer = forward(operation)
// errors will be sent to the errorCallback
observer.subscribe({
error: e => console.log('caught error with errorLink:', e),
})
return observer.map(data => {
console.log(`ending request for ${operation.operationName}`)
return data
})
})
export function mockLink(mocks, addTypename = false) {
const mockLink = new MockLink(mocks, addTypename)
mockLink.setOnError(error => {
if (error.message.match(/No more mocked responses for the query/)) {
// Do nothing
return
}
throw error
})
return ApolloLink.from([errorLink, mockLink])
}