Skip to content

Commit 9d71ded

Browse files
authored
Respect other arguments while creating underlying pagination selections and use serial queries if skip limit exceeds (#76)
1 parent 3b6344e commit 9d71ded

File tree

9 files changed

+371
-85
lines changed

9 files changed

+371
-85
lines changed

.changeset/eleven-pianos-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/client-auto-pagination': patch
3+
---
4+
5+
Respect other arguments while creating underlying pagination selections

.changeset/smooth-rice-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/client-auto-pagination': patch
3+
---
4+
5+
Use lastID if skip exceeds the limit

.github/workflows/canary.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Canary Release
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '.changeset/**/*.md'
7+
8+
jobs:
9+
publish-canary:
10+
name: Publish Canary
11+
runs-on: ubuntu-latest
12+
if: github.event.pull_request.head.repo.full_name == github.repository
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v3
16+
with:
17+
fetch-depth: 0
18+
- name: Use Node
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: 18
22+
cache: 'yarn'
23+
24+
- name: Install Dependencies using Yarn
25+
run: yarn
26+
27+
- name: Setup NPM credentials
28+
run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
29+
env:
30+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
31+
32+
- name: Release Canary
33+
id: canary
34+
uses: 'kamilkisiela/release-canary@master'
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
38+
with:
39+
npm-token: ${{ secrets.NPM_TOKEN }}
40+
npm-script: 'yarn release:canary'
41+
changesets: true
42+
43+
- name: Publish a message
44+
if: steps.canary.outputs.released == 'true'
45+
uses: 'kamilkisiela/pr-comment@master'
46+
with:
47+
commentKey: canary
48+
message: |
49+
The latest changes of this PR are available as canary in npm (based on the declared `changesets`):
50+
51+
```
52+
${{ steps.canary.outputs.changesetsPublishedPackages}}
53+
```
54+
bot-token: ${{ secrets.GITHUB_TOKEN }}
55+
bot: 'github-actions[bot]'
56+
github-token: ${{ secrets.GITHUB_TOKEN }}
57+
58+
- name: Publish a empty message
59+
if: steps.canary.outputs.released == 'false'
60+
uses: 'kamilkisiela/pr-comment@master'
61+
with:
62+
commentKey: canary
63+
message: |
64+
The latest changes of this PR are not available as canary, since there are no linked `changesets` for this PR.
65+
bot-token: ${{ secrets.GITHUB_TOKEN }}
66+
bot: 'github-actions[bot]'
67+
github-token: ${{ secrets.GITHUB_TOKEN }}

examples/transforms/.graphclientrc.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ sources:
55
endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2
66
transforms:
77
# Enable Automatic Block Tracking
8-
- autoPagination:
9-
validateSchema: true
108
- blockTracking:
119
validateSchema: true
10+
- autoPagination:
11+
validateSchema: true
12+
13+
serve:
14+
browser: false

packages/auto-pagination/__tests__/auto-pagination.test.ts

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { execute, ExecutionResult, parse } from 'graphql'
44
import AutoPaginationTransform from '../src'
55

66
describe('Auto Pagination', () => {
7-
const users = new Array(20).fill({}).map((_, i) => ({ id: (i + 1).toString(), name: `User ${i + 1}` }))
8-
const LIMIT = 3
7+
const users = new Array(20000).fill({}).map((_, i) => ({ id: (i + 1).toString(), name: `User ${i + 1}` }))
8+
const usersOdd = users.filter((_, i) => i % 2 === 1)
99
const schema = makeExecutableSchema({
1010
typeDefs: /* GraphQL */ `
1111
type Query {
1212
_meta: Meta
13-
users(first: Int = ${LIMIT}, skip: Int = 0): [User!]!
13+
users(first: Int = ${1000}, skip: Int = 0, odd: Boolean, where: WhereInput): [User!]!
1414
}
1515
type User {
1616
id: ID!
@@ -22,14 +22,27 @@ describe('Auto Pagination', () => {
2222
type Block {
2323
number: Int
2424
}
25+
input WhereInput {
26+
id_gte: ID
27+
}
2528
`,
2629
resolvers: {
2730
Query: {
28-
users: (_, { first = LIMIT, skip = 0 }) => {
29-
if (first > LIMIT) {
30-
throw new Error(`You cannot request more than ${LIMIT} users; you requested ${first}`)
31+
users: (_, { first = 1000, skip = 0, odd, where }) => {
32+
if (first > 1000) {
33+
throw new Error(`You cannot request more than 1000 users; you requested ${first}`)
34+
}
35+
if (skip > 5000) {
36+
throw new Error(`You cannot skip more than 5000 users; you requested ${skip}`)
37+
}
38+
let usersSlice = users
39+
if (odd) {
40+
usersSlice = usersOdd
41+
}
42+
if (where?.id_gte) {
43+
usersSlice = users.slice(where.id_gte)
3144
}
32-
return users.slice(skip, skip + first)
45+
return usersSlice.slice(skip, skip + first)
3346
},
3447
_meta: () => ({
3548
block: {
@@ -41,18 +54,12 @@ describe('Auto Pagination', () => {
4154
})
4255
const wrappedSchema = wrapSchema({
4356
schema,
44-
transforms: [
45-
new AutoPaginationTransform({
46-
config: {
47-
limitOfRecords: LIMIT,
48-
},
49-
}),
50-
],
57+
transforms: [new AutoPaginationTransform()],
5158
})
5259
it('should give correct numbers of results if first arg are higher than given limit', async () => {
5360
const query = /* GraphQL */ `
5461
query {
55-
users(first: 10) {
62+
users(first: 2000) {
5663
id
5764
name
5865
}
@@ -62,13 +69,13 @@ describe('Auto Pagination', () => {
6269
schema: wrappedSchema,
6370
document: parse(query),
6471
})
65-
expect(result.data?.users).toHaveLength(10)
66-
expect(result.data?.users).toEqual(users.slice(0, 10))
72+
expect(result.data?.users).toHaveLength(2000)
73+
expect(result.data?.users).toEqual(users.slice(0, 2000))
6774
})
6875
it('should respect skip argument', async () => {
6976
const query = /* GraphQL */ `
7077
query {
71-
users(first: 10, skip: 1) {
78+
users(first: 2000, skip: 1) {
7279
id
7380
name
7481
}
@@ -78,8 +85,8 @@ describe('Auto Pagination', () => {
7885
schema: wrappedSchema,
7986
document: parse(query),
8087
})
81-
expect(result.data?.users).toHaveLength(10)
82-
expect(result.data?.users).toEqual(users.slice(1, 11))
88+
expect(result.data?.users).toHaveLength(2000)
89+
expect(result.data?.users).toEqual(users.slice(1, 2001))
8390
})
8491
it('should work with the values under the limit', async () => {
8592
const query = /* GraphQL */ `
@@ -105,7 +112,7 @@ describe('Auto Pagination', () => {
105112
number
106113
}
107114
}
108-
users(first: 10) {
115+
users(first: 2000) {
109116
id
110117
name
111118
}
@@ -116,7 +123,39 @@ describe('Auto Pagination', () => {
116123
document: parse(query),
117124
})
118125
expect(result.data?._meta?.block?.number).toBeDefined()
119-
expect(result.data?.users).toHaveLength(10)
120-
expect(result.data?.users).toEqual(users.slice(0, 10))
126+
expect(result.data?.users).toHaveLength(2000)
127+
expect(result.data?.users).toEqual(users.slice(0, 2000))
128+
})
129+
it('should respect other arguments', async () => {
130+
const query = /* GraphQL */ `
131+
query {
132+
users(first: 2000, odd: true) {
133+
id
134+
name
135+
}
136+
}
137+
`
138+
const result: ExecutionResult<any> = await execute({
139+
schema: wrappedSchema,
140+
document: parse(query),
141+
})
142+
expect(result.data?.users).toHaveLength(2000)
143+
expect(result.data?.users).toEqual(usersOdd.slice(0, 2000))
144+
})
145+
it('should make queries serially if skip limit reaches the limit', async () => {
146+
const query = /* GraphQL */ `
147+
query {
148+
users(first: 15000) {
149+
id
150+
name
151+
}
152+
}
153+
`
154+
const result: ExecutionResult<any> = await execute({
155+
schema: wrappedSchema,
156+
document: parse(query),
157+
})
158+
expect(result.data?.users).toHaveLength(15000)
159+
expect(result.data?.users).toEqual(users.slice(0, 15000))
121160
})
122161
})

packages/auto-pagination/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@
3939
"access": "public"
4040
},
4141
"dependencies": {
42+
"@graphql-tools/delegate": "8.7.7",
43+
"@graphql-tools/wrap": "8.4.16",
4244
"@graphql-tools/utils": "8.6.10",
45+
"lodash": "4.17.21",
4346
"tslib": "2.4.0"
4447
},
48+
"devDependencies": {
49+
"@types/lodash": "4.14.182"
50+
},
4551
"peerDependencies": {
4652
"graphql": "^15.2.0 || ^16.0.0",
4753
"@graphql-mesh/types": "^0.72.0"

0 commit comments

Comments
 (0)