Skip to content

Commit 9dacdc5

Browse files
authored
New Automatic Type Merging transform (#108)
* New Automatic Type Merging transform * Fix * fix * Use type merging transform for the reference * Update example
1 parent c637c13 commit 9dacdc5

File tree

8 files changed

+118
-75
lines changed

8 files changed

+118
-75
lines changed

.changeset/moody-ducks-lie.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphprotocol/client-cli': minor
3+
'@graphprotocol/client-auto-type-merging': major
4+
---
5+
6+
New Automatic Type Merging transform

examples/composition/.graphclientrc.yml

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,18 @@ sources:
33
handler:
44
graphql:
55
endpoint: https://api.thegraph.com/subgraphs/name/amxx/eip721-subgraph
6-
76
transforms:
8-
- prefix:
9-
value: NFT_
10-
includeRootOperations: true
11-
ignore:
12-
- _SubgraphErrorPolicy_
137
# Rename Account to User to apply Type Merging
148
- rename:
159
- from:
16-
type: NFT_Account
10+
type: Account
1711
to:
1812
type: User
19-
# Teach Type Merging how to resolve this entity
20-
- typeMerging:
21-
queryFields:
22-
- queryFieldName: NFT_accounts
23-
keyField: id
24-
keyArg: 'where.id_in'
13+
- autoTypeMerging: true
2514

2615
- name: uniswap
2716
handler:
2817
graphql:
2918
endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2
3019
transforms:
31-
- prefix:
32-
value: UNI_
33-
includeRootOperations: true
34-
ignore:
35-
- _SubgraphErrorPolicy_
36-
- User # Keep User as-is because we need it for Type Merging
37-
- typeMerging:
38-
queryFields:
39-
# Without batching
40-
- queryFieldName: UNI_user
41-
keyField: id
42-
keyArg: id
20+
- autoTypeMerging: true

examples/composition/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@graphprotocol/client-cli": "1.0.1",
13-
"@graphql-mesh/transform-prefix": "0.11.45",
1413
"@graphql-mesh/transform-rename": "0.12.46",
15-
"@graphql-mesh/transform-type-merging": "0.4.0",
1614
"concurrently": "7.1.0",
1715
"graphql": "16.3.0",
1816
"nodemon": "2.0.15",

examples/composition/src/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ import { execute } from '../.graphclient'
33
async function main() {
44
const response = await execute(
55
/* GraphQL */ `
6-
query ExampleQuery {
7-
# Root field is from nft
8-
NFT_account(id: "0x72ba1965320ab5352fd6d68235cc3c5306a6ffa2") {
9-
__typename
6+
fragment UserFields on User {
7+
# Shared unique identifier field
8+
id
9+
# Field from uniswap source
10+
tokens(first: 1) {
1011
id
11-
# This field is from uniswap
12-
liquidityPositions {
13-
__typename
12+
uri
13+
registry {
1414
id
15-
liquidityTokenBalance
1615
}
1716
}
18-
# Root field is from uniswap
19-
UNI_user(id: "0x72ba1965320ab5352fd6d68235cc3c5306a6ffa2") {
20-
__typename
17+
# Field from nft source
18+
liquidityPositions(first: 1) {
2119
id
22-
# This field is from nft
23-
tokens {
24-
__typename
25-
id
26-
uri
27-
registry {
28-
id
29-
}
30-
}
20+
}
21+
}
22+
23+
query ExampleQuery {
24+
# Get base entity from nft source
25+
account(id: "0x72ba1965320ab5352fd6d68235cc3c5306a6ffa2") {
26+
...UserFields
27+
}
28+
# Get base entity from uniswap source
29+
user(id: "0x72ba1965320ab5352fd6d68235cc3c5306a6ffa2") {
30+
...UserFields
3131
}
3232
}
3333
`,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "@graphprotocol/client-auto-type-merging",
3+
"version": "0.0.0",
4+
"description": "",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/graphprotocol/graph-client.git",
8+
"directory": "packages/block-auto-type-merging"
9+
},
10+
"scripts": {
11+
"prepack": "bob prepack",
12+
"check": "tsc --pretty --noEmit"
13+
},
14+
"keywords": [
15+
"thegraph",
16+
"graphql",
17+
"client"
18+
],
19+
"license": "MIT",
20+
"sideEffects": false,
21+
"main": "dist/index.js",
22+
"module": "dist/index.mjs",
23+
"typings": "dist/index.d.ts",
24+
"typescript": {
25+
"definition": "dist/index.d.ts"
26+
},
27+
"exports": {
28+
".": {
29+
"require": "./dist/index.js",
30+
"import": "./dist/index.mjs"
31+
},
32+
"./*": {
33+
"require": "./dist/*.js",
34+
"import": "./dist/*.mjs"
35+
}
36+
},
37+
"publishConfig": {
38+
"directory": "dist",
39+
"access": "public"
40+
},
41+
"dependencies": {
42+
"@graphql-tools/delegate": "8.7.10",
43+
"@graphql-mesh/transform-type-merging": "0.4.2",
44+
"tslib": "2.4.0"
45+
},
46+
"peerDependencies": {
47+
"graphql": "^15.2.0 || ^16.0.0",
48+
"@graphql-mesh/types": "^0.73.0"
49+
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types'
2+
import { SubschemaConfig } from '@graphql-tools/delegate'
3+
import { GraphQLSchema } from 'graphql'
4+
import TypeMergingTransform from '@graphql-mesh/transform-type-merging'
5+
6+
export default class AutoTypeMergingTransform implements MeshTransform {
7+
constructor(private options: MeshTransformOptions) {}
8+
transformSchema(schema: GraphQLSchema, subschemaConfig: SubschemaConfig) {
9+
const queryType = schema.getQueryType()
10+
const queryFields = queryType?.getFields()
11+
const typeMergingQueryFieldConfig: YamlConfig.MergedRootFieldConfig[] = []
12+
if (queryFields != null) {
13+
for (const queryFieldName in queryFields) {
14+
const queryField = queryFields[queryFieldName]
15+
if (queryField.args.some((arg) => arg.name === 'where')) {
16+
typeMergingQueryFieldConfig.push({
17+
queryFieldName,
18+
keyField: 'id',
19+
keyArg: 'where.id_in',
20+
})
21+
}
22+
}
23+
}
24+
const typeMergingTransform = new TypeMergingTransform({
25+
...this.options,
26+
config: {
27+
queryFields: typeMergingQueryFieldConfig,
28+
},
29+
})
30+
return typeMergingTransform.transformSchema(schema, subschemaConfig)
31+
}
32+
}

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@graphql-mesh/graphql": "0.27.1",
5454
"tslib": "2.4.0",
5555
"@graphprotocol/client-auto-pagination": "1.0.1",
56+
"@graphprotocol/client-auto-type-merging": "0.0.0",
5657
"@graphprotocol/client-block-tracking": "1.0.1",
5758
"@graphprotocol/client-add-source-name": "1.0.1"
5859
},

yarn.lock

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,19 +1641,6 @@
16411641
json-pointer "0.6.2"
16421642
lodash.get "4.4.2"
16431643

1644-
"@graphql-mesh/[email protected]":
1645-
version "0.11.45"
1646-
resolved "https://registry.yarnpkg.com/@graphql-mesh/transform-prefix/-/transform-prefix-0.11.45.tgz#55d8d58e925a2bf0d041c699957ac6f8681e9bda"
1647-
integrity sha512-RRivxjcXTZdVvz4HyGDUKuTICkqT0pb/oQTzPEmmRn+ihukCNBPKgPP8zz7cP4uqHzbjHD/TxUHnSP0DNjvk5Q==
1648-
dependencies:
1649-
"@graphql-mesh/types" "0.73.0"
1650-
"@graphql-mesh/utils" "0.35.0"
1651-
"@graphql-tools/delegate" "8.7.10"
1652-
"@graphql-tools/utils" "8.6.12"
1653-
"@graphql-tools/wrap" "8.4.19"
1654-
graphql-scalars "1.17.0"
1655-
tslib "^2.3.1"
1656-
16571644
"@graphql-mesh/[email protected]":
16581645
version "0.12.46"
16591646
resolved "https://registry.yarnpkg.com/@graphql-mesh/transform-rename/-/transform-rename-0.12.46.tgz#06fd775770bb7b84a7ff115f830d366735811133"
@@ -1667,13 +1654,13 @@
16671654
graphql-scalars "1.17.0"
16681655
tslib "^2.3.1"
16691656

1670-
"@graphql-mesh/[email protected].0":
1671-
version "0.4.0"
1672-
resolved "https://registry.yarnpkg.com/@graphql-mesh/transform-type-merging/-/transform-type-merging-0.4.0.tgz#fbb69d0b00d6d2032803036604aa6c629b50bca9"
1673-
integrity sha512-cMr2T2QLJ8sej4j5djVLTqLrgebGHBYFaqH0uhLSMQUH0Iy6GoXs8eo4WnrDQDn/EDoWvwa0pXkPcQBbFptZsg==
1657+
"@graphql-mesh/[email protected].2":
1658+
version "0.4.2"
1659+
resolved "https://registry.yarnpkg.com/@graphql-mesh/transform-type-merging/-/transform-type-merging-0.4.2.tgz#72f74663a3a62e759ebc47d008441a05959cc6a0"
1660+
integrity sha512-UJuakq4hepwfrUZkx+u+Pf2WiYhbMlOgONzDMDn3j/VhZaJ0Wh9N4pMDccNX46lYkGppmCWq+bi55AJD7/fxkQ==
16741661
dependencies:
1675-
"@graphql-mesh/types" "0.73.0"
1676-
"@graphql-mesh/utils" "0.35.0"
1662+
"@graphql-mesh/types" "0.73.2"
1663+
"@graphql-mesh/utils" "0.35.2"
16771664
"@graphql-tools/delegate" "8.7.10"
16781665
"@graphql-tools/stitching-directives" "2.2.17"
16791666
tslib "^2.3.1"
@@ -1932,15 +1919,6 @@
19321919
tslib "^2.4.0"
19331920
value-or-promise "^1.0.11"
19341921

1935-
"@graphql-tools/[email protected]":
1936-
version "2.2.17"
1937-
resolved "https://registry.yarnpkg.com/@graphql-tools/stitching-directives/-/stitching-directives-2.2.17.tgz#3d5c67098cb56ad61982524518a5a113b739abc4"
1938-
integrity sha512-bR6ByL7XP3ofp7h7y5/pBbIT3jSiQFHsdtwLU+YQy5esTd1JDh2hS+GWqm3pyCKveWDyya+eyTABIydSe+p4fg==
1939-
dependencies:
1940-
"@graphql-tools/delegate" "8.7.10"
1941-
"@graphql-tools/utils" "8.6.12"
1942-
tslib "~2.4.0"
1943-
19441922
"@graphql-tools/[email protected]":
19451923
version "2.2.18"
19461924
resolved "https://registry.yarnpkg.com/@graphql-tools/stitching-directives/-/stitching-directives-2.2.18.tgz#138ce217eee6a46f107d022ffb3b36fa023ef4f3"

0 commit comments

Comments
 (0)