Skip to content

Commit 35086ac

Browse files
authored
Merge pull request #535 from daostack/fix-hitting-limit-from-graph
Loop with skips of 1000 to avoid hitting graph limit
2 parents f9ac758 + b9200d5 commit 35086ac

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@daostack/arc.js",
3-
"version": "2.0.0-experimental.54",
3+
"version": "2.0.0-experimental.55",
44
"description": "",
55
"keywords": [],
66
"main": "dist/lib/index.js",

src/arc.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
} from './index'
4545

4646
const abis = require('./abis/abis.json')
47+
const MAX_BATCH_QUERY = 1000
4748

4849
export type IArcOptions = IApolloClientOptions & {
4950
/** Information about the contracts. Cf. [[setContractInfos]] and [[fetchContractInfos]] */
@@ -203,14 +204,20 @@ export class Arc extends GraphNodeObserver {
203204

204205
/**
205206
* fetch contractInfos from the subgraph
207+
* we skip the first max batch query each iteration to avoid hitting subgraph limit
206208
* @return a list of IContractInfo instances
207209
*/
208210
public async fetchContractInfos(
209211
apolloQueryOptions: IApolloQueryOptions = {}
210212
): Promise<IContractInfo[]> {
211-
const query = gql`
213+
const allContractInfos = []
214+
let contractInfos = []
215+
let skip = 0
216+
217+
do {
218+
const query = gql`
212219
query AllContractInfos {
213-
contractInfos(first: 1000) {
220+
contractInfos(first: ${MAX_BATCH_QUERY} skip: ${skip * MAX_BATCH_QUERY}) {
214221
id
215222
name
216223
version
@@ -219,25 +226,36 @@ export class Arc extends GraphNodeObserver {
219226
}
220227
}
221228
`
222-
// const result = await this.getObservableList(query, itemMap, apolloQueryOptions).pipe(first()).toPromise()
223-
const response = await this.sendQuery(query, apolloQueryOptions)
224-
const contractInfos = response.data.contractInfos as IContractInfo[]
229+
const response = await this.sendQuery(query, apolloQueryOptions)
230+
contractInfos = response.data.contractInfos as IContractInfo[]
231+
if (contractInfos.length > 0) {
232+
allContractInfos.push(...contractInfos)
233+
}
234+
skip++
235+
} while (contractInfos.length === MAX_BATCH_QUERY)
236+
225237
const universalContractInfos = await this.fetchUniversalContractInfos()
226-
contractInfos.push(...universalContractInfos)
227-
this.setContractInfos(contractInfos)
228-
return contractInfos
238+
allContractInfos.push(...universalContractInfos)
239+
this.setContractInfos(allContractInfos)
240+
return allContractInfos
229241
}
230242

231243
/**
232244
* fetch universalContractInfos from the subgraph
245+
* we skip the first max batch query each iteration to avoid hitting subgraph limit
233246
* @return a list of IContractInfo instances
234247
*/
235248
public async fetchUniversalContractInfos(
236249
apolloQueryOptions: IApolloQueryOptions = {}
237250
): Promise<IContractInfo[]> {
238-
const query = gql`
251+
const allUniversalContractInfos = []
252+
let universalContractInfos = []
253+
let skip = 0
254+
255+
do {
256+
const query = gql`
239257
query AllUniversalContractInfos {
240-
universalContractInfos(first: 1000) {
258+
universalContractInfos(first: ${MAX_BATCH_QUERY} skip: ${skip * MAX_BATCH_QUERY}) {
241259
id
242260
name
243261
version
@@ -246,9 +264,15 @@ export class Arc extends GraphNodeObserver {
246264
}
247265
}
248266
`
249-
const response = await this.sendQuery(query, apolloQueryOptions)
250-
const result = response.data.universalContractInfos as IContractInfo[]
251-
return result
267+
const response = await this.sendQuery(query, apolloQueryOptions)
268+
universalContractInfos = response.data.universalContractInfos as IContractInfo[]
269+
if (universalContractInfos.length > 0) {
270+
allUniversalContractInfos.push(...universalContractInfos)
271+
}
272+
skip++
273+
} while (universalContractInfos.length === MAX_BATCH_QUERY)
274+
275+
return allUniversalContractInfos
252276
}
253277

254278
/**

0 commit comments

Comments
 (0)