-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathrequest-cancellation.ts
58 lines (47 loc) · 1.05 KB
/
request-cancellation.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* It is possible to cancel a request using an `AbortController` signal.
*/
import { gql, GraphQLClient } from '../src/entrypoints/main.js'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`
/**
* You can define the `signal` in the `GraphQLClient` constructor:
*/
{
const abortController = new AbortController()
const client = new GraphQLClient(endpoint, {
signal: abortController.signal,
})
void client.request(gql`
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
`)
abortController.abort()
}
/**
* You can also set the signal per request (this will override an existing GraphQLClient signal):
*/
{
const abortController = new AbortController()
const client = new GraphQLClient(endpoint)
const document = gql`
{
Movie(title: "Inception") {
releaseDate
actors {
name
}
}
}
`
void client.request({
document,
signal: abortController.signal,
})
abortController.abort()
}