forked from graffle-js/graffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batching-requests.ts
57 lines (49 loc) · 1.1 KB
/
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
import { batchRequests } from '../src'
;(async function () {
const endpoint = 'https://api.spacex.land/graphql/'
const query1 = /* GraphQL */ `
query ($id: ID!) {
capsule(id: $id) {
id
landings
}
}
`
const variables1 = {
id: 'C105',
}
interface TData1 {
data: { capsule: { id: string; landings: number } }
}
const query2 = /* GraphQL */ `
{
rockets(limit: 10) {
active
}
}
`
interface TData2 {
data: { rockets: { active: boolean }[] }
}
const query3 = /* GraphQL */ `
query ($id: ID!) {
core(id: $id) {
id
block
original_launch
}
}
`
const variables3 = {
id: 'B1015',
}
interface TData3 {
data: { core: { id: string; block: number; original_launch: string } }
}
const data = await batchRequests<[TData1, TData2, TData3]>(endpoint, [
{ document: query1, variables: variables1 },
{ document: query2 },
{ document: query3, variables: variables3 },
])
console.log(JSON.stringify(data, undefined, 2))
})().catch((error) => console.error(error))