forked from graffle-js/graffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql-batching-requests.ts
60 lines (51 loc) · 1.07 KB
/
graphql-batching-requests.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
59
60
/**
* It is possible with `graphql-request` to use batching via the `batchRequests()` function.
* @see https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md
*/
import { batchRequests, gql } from '../src/index.js'
const endpoint = `https://api.spacex.land/graphql/`
const query1 = gql`
query ($id: ID!) {
capsule(id: $id) {
id
landings
}
}
`
const variables1 = {
id: `C105`,
}
interface Data1 {
data: { capsule: { id: string; landings: number } }
}
const query2 = gql`
{
rockets(limit: 10) {
active
}
}
`
interface Data2 {
data: { rockets: { active: boolean }[] }
}
const query3 = gql`
query ($id: ID!) {
core(id: $id) {
id
block
original_launch
}
}
`
const variables3 = {
id: `B1015`,
}
interface Data3 {
data: { core: { id: string; block: number; original_launch: string } }
}
const data = await batchRequests<[Data1, Data2, Data3]>(endpoint, [
{ document: query1, variables: variables1 },
{ document: query2 },
{ document: query3, variables: variables3 },
])
console.log(data)