Skip to content

Commit bb5df03

Browse files
authored
fix(block-tracking/auto-pagination): respect the changes done by other transforms (#110)
1 parent 3d418dd commit bb5df03

File tree

7 files changed

+71
-14
lines changed

7 files changed

+71
-14
lines changed

.changeset/loud-hornets-leave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphprotocol/client-auto-pagination': patch
3+
'@graphprotocol/client-block-tracking': patch
4+
---
5+
6+
Respect the changes done by other transforms

examples/transforms/.graphclientrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ sources:
44
graphql:
55
endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2
66
transforms:
7+
- prefix:
8+
value: uni_
9+
includeRootOperations: true
710
# Enable Automatic Block Tracking
811
- blockTracking:
912
validateSchema: true

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { makeExecutableSchema } from '@graphql-tools/schema'
22
import { wrapSchema } from '@graphql-tools/wrap'
33
import { execute, ExecutionResult, parse } from 'graphql'
44
import AutoPaginationTransform from '../src'
5+
import PrefixTransform from '@graphql-mesh/transform-prefix'
6+
import LocalforageCache from '@graphql-mesh/cache-localforage'
7+
import { PubSub } from '@graphql-mesh/utils'
58

69
describe('Auto Pagination', () => {
710
const users = new Array(20000).fill({}).map((_, i) => ({ id: (i + 1).toString(), name: `User ${i + 1}` }))
@@ -158,4 +161,37 @@ describe('Auto Pagination', () => {
158161
expect(result.data?.users).toHaveLength(15000)
159162
expect(result.data?.users).toEqual(users.slice(0, 15000))
160163
})
164+
it('should work with prefix transform properly', async () => {
165+
const wrappedSchema = wrapSchema({
166+
schema,
167+
transforms: [
168+
new PrefixTransform({
169+
baseDir: process.cwd(),
170+
cache: new LocalforageCache(),
171+
pubsub: new PubSub(),
172+
apiName: 'test',
173+
config: {
174+
value: 'my_',
175+
includeRootOperations: true,
176+
},
177+
importFn: (m) => import(m),
178+
}),
179+
new AutoPaginationTransform(),
180+
],
181+
})
182+
const query = /* GraphQL */ `
183+
query {
184+
my_users(first: 15000) {
185+
id
186+
name
187+
}
188+
}
189+
`
190+
const result: ExecutionResult<any> = await execute({
191+
schema: wrappedSchema,
192+
document: parse(query),
193+
})
194+
expect(result.data?.my_users).toHaveLength(15000)
195+
expect(result.data?.my_users).toEqual(users.slice(0, 15000))
196+
})
161197
})

packages/auto-pagination/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"tslib": "2.4.0"
4747
},
4848
"devDependencies": {
49-
"@types/lodash": "4.14.182"
49+
"@types/lodash": "4.14.182",
50+
"@graphql-mesh/transform-prefix": "0.11.47"
5051
},
5152
"peerDependencies": {
5253
"graphql": "^15.2.0 || ^16.0.0",

packages/auto-pagination/src/index.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,12 @@ export default class AutoPaginationTransform implements MeshTransform {
7878
}
7979
}
8080

81-
transformSchema(
82-
schema: GraphQLSchema,
83-
subschemaConfig: SubschemaConfig<any, any, any, any>,
84-
transformedSchema: GraphQLSchema | undefined,
85-
) {
81+
transformSchema(schema: GraphQLSchema, subschemaConfig: SubschemaConfig<any, any, any, any>) {
8682
if (this.config.validateSchema) {
87-
validateSchema(schema, this.config)
83+
validateSchema(subschemaConfig.schema, this.config)
8884
}
89-
if (transformedSchema != null) {
90-
const queryType = transformedSchema.getQueryType()
85+
if (schema != null) {
86+
const queryType = schema.getQueryType()
9187
if (queryType != null) {
9288
const queryFields = queryType.getFields()
9389
for (const fieldName in queryFields) {
@@ -114,7 +110,8 @@ export default class AutoPaginationTransform implements MeshTransform {
114110
const askedRecords = Math.min(remainingRecords, this.config.skipArgumentLimit)
115111
_.set(newArgs, this.config.firstArgumentName, askedRecords)
116112
const result = await delegateToSchema({
117-
schema: transformedSchema,
113+
schema,
114+
fieldName,
118115
args: newArgs,
119116
context,
120117
info,
@@ -244,7 +241,8 @@ export default class AutoPaginationTransform implements MeshTransform {
244241
const finalData = {}
245242
for (const fullAliasName in originalResult.data) {
246243
if (fullAliasName.startsWith('splitted_')) {
247-
const [, , aliasName] = fullAliasName.split('_')
244+
const [, , ...rest] = fullAliasName.split('_')
245+
const aliasName = rest.join('_')
248246
finalData[aliasName] = finalData[aliasName] || []
249247
for (const record of originalResult.data[fullAliasName]) {
250248
finalData[aliasName].push(record)

packages/block-tracking/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MeshTransform } from '@graphql-mesh/types'
2-
import type { DelegationContext } from '@graphql-tools/delegate'
2+
import type { DelegationContext, SubschemaConfig } from '@graphql-tools/delegate'
33
import type { ExecutionRequest } from '@graphql-tools/utils'
44
import { memoize1, memoize2 } from '@graphql-tools/utils'
55
import {
@@ -175,9 +175,9 @@ export default class BlockTrackingTransform implements MeshTransform {
175175
}
176176
}
177177

178-
transformSchema(schema: GraphQLSchema) {
178+
transformSchema(schema: GraphQLSchema, subschemaConfig: SubschemaConfig<any, any, any, any>): GraphQLSchema {
179179
if (this.config.validateSchema) {
180-
validateSchema(schema, this.config)
180+
validateSchema(subschemaConfig.schema, this.config)
181181
}
182182
return schema
183183
}

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,19 @@
16731673
json-pointer "0.6.2"
16741674
lodash.get "4.4.2"
16751675

1676+
"@graphql-mesh/[email protected]":
1677+
version "0.11.47"
1678+
resolved "https://registry.yarnpkg.com/@graphql-mesh/transform-prefix/-/transform-prefix-0.11.47.tgz#48a797da4fe4c4cd629431b58e0bb1f135ba4a36"
1679+
integrity sha512-gVUkAr0w0wLL5jFPxdmMYSXWYq9b96//mdESF9vrksiP9lE7JOLNzI08OSSy0lU6TEFNmB/x7CymJ5axyAz7Pg==
1680+
dependencies:
1681+
"@graphql-mesh/types" "0.73.2"
1682+
"@graphql-mesh/utils" "0.35.2"
1683+
"@graphql-tools/delegate" "8.7.10"
1684+
"@graphql-tools/utils" "8.6.12"
1685+
"@graphql-tools/wrap" "8.4.19"
1686+
graphql-scalars "1.17.0"
1687+
tslib "^2.3.1"
1688+
16761689
"@graphql-mesh/[email protected]":
16771690
version "0.12.46"
16781691
resolved "https://registry.yarnpkg.com/@graphql-mesh/transform-rename/-/transform-rename-0.12.46.tgz#06fd775770bb7b84a7ff115f830d366735811133"

0 commit comments

Comments
 (0)