forked from pancakeswap/pancake-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infoQueryHelpers.ts
45 lines (44 loc) · 1.44 KB
/
infoQueryHelpers.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
import { GraphQLClient } from 'graphql-request'
import { getGQLHeaders } from 'utils/graphql'
import requestWithTimeout from 'utils/requestWithTimeout'
/**
* Helper function to get large amount GraphQL subqueries
* @param queryConstructor constructor function that combines subqueries
* @param subqueries individual queries
* @param endpoint GraphQL endpoint
* @param skipCount how many subqueries to fire at a time
* @returns
*/
export const multiQuery = async (
queryConstructor: (subqueries: string[]) => string,
subqueries: string[],
endpoint: string | GraphQLClient,
skipCount = 1000,
) => {
let fetchedData = {}
let allFound = false
let skip = 0
const client =
typeof endpoint === 'string' ? new GraphQLClient(endpoint, { headers: getGQLHeaders(endpoint) }) : endpoint
try {
while (!allFound) {
let end = subqueries.length
if (skip + skipCount < subqueries.length) {
end = skip + skipCount
}
const subqueriesSlice = subqueries.slice(skip, end)
// eslint-disable-next-line no-await-in-loop
const result: any = await requestWithTimeout(client, queryConstructor(subqueriesSlice))
fetchedData = {
...fetchedData,
...result,
}
allFound = Object.keys(result).length < skipCount || skip + skipCount > subqueries.length
skip += skipCount
}
return fetchedData
} catch (error) {
console.error('Failed to fetch info data', error)
return null
}
}