Skip to content

Commit 1082229

Browse files
authored
Update to the latest postgraphile beta (#23)
* Update to the latest postgraphile beta * Stop using deprecated methods, improve types * Update changelog
1 parent 3b929cf commit 1082229

17 files changed

+2358
-2012
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Changed
9+
- Update postgraphile dependencies to beta.48 (#23)
810

911
## [0.2.2] - 2025-05-21
1012
### Fixed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
"prepare": "husky"
2121
},
2222
"dependencies": {
23-
"@graphile/simplify-inflection": "^8.0.0-beta.5",
23+
"@graphile/simplify-inflection": "^8.0.0-beta.8",
2424
"@subql/utils": "^2.14.0",
2525
"@types/yargs": "^17.0.33",
2626
"dotenv": "^16.4.5",
2727
"eslint": "^8.8.0",
2828
"express": "^4.21.1",
29-
"postgraphile": "^5.0.0-beta.28",
29+
"postgraphile": "^5.0.0-beta.48",
3030
"yargs": "latest"
3131
},
3232
"devDependencies": {

src/config/graphile.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function genPreset(args: ArgsInterface): GraphileConfig.Preset {
3838

3939
const SchemaSmartTagsPlugin = CreateSchemaSmartTagsPlugin(pgSchema);
4040
const metadataPlugin = CreateMetadataPlugin(pgSchema);
41-
const subqueryMetadataPlugin = CreateSubqueryMetadataPlugin(pgSchema, args);
41+
const subqueryMetadataPlugin = CreateSubqueryMetadataPlugin(pgSchema);
4242
const preset: GraphileConfig.Preset = {
4343
extends: [PostGraphileAmberPreset, PgSimplifyInflectionPreset],
4444
grafserv: {

src/plugins/GetMetadataPlugin.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
22
// SPDX-License-Identifier: GPL-3.0
33

4-
import { makeExtendSchemaPlugin, gql } from 'graphile-utils';
5-
import { withPgClientTransaction } from 'postgraphile/@dataplan/pg';
4+
import { gql, extendSchema } from 'graphile-utils';
5+
import { loadOneWithPgClient } from 'postgraphile/@dataplan/pg';
66

77
const METADATA_TYPES = {
88
deployments: 'string',
@@ -27,8 +27,8 @@ type MetaType = number | string | boolean | object;
2727

2828
type MetaEntry = { key: string; value: MetaType };
2929

30-
export function CreateMetadataPlugin(schemaName: string) {
31-
return makeExtendSchemaPlugin((build) => {
30+
export function CreateMetadataPlugin(schemaName: string): GraphileConfig.Plugin {
31+
return extendSchema((build) => {
3232
// TODO Only handled the single-chain scenario, multi-chains may have unexpected results.
3333
const metadata = build.input.pgRegistry.pgResources._metadata;
3434

@@ -49,14 +49,12 @@ export function CreateMetadataPlugin(schemaName: string) {
4949
_meta: MetadataPayload
5050
}
5151
`,
52-
plans: {
52+
objects: {
5353
Query: {
54-
_meta() {
55-
const $executorContext = metadata.executor.context();
56-
const $metadataResult = withPgClientTransaction(
57-
metadata.executor,
58-
$executorContext,
59-
async (client, data) => {
54+
plans: {
55+
_meta() {
56+
const $executorContext = metadata.executor.context();
57+
return loadOneWithPgClient(metadata.executor, $executorContext, async (client) => {
6058
const { rows } = await client.query<MetaEntry>({
6159
text: `select * from "${schemaName}"."_metadata" WHERE key = ANY ($1)`,
6260
values: [METADATA_KEYS],
@@ -85,10 +83,9 @@ export function CreateMetadataPlugin(schemaName: string) {
8583
result.block.number = Number(row.value);
8684
}
8785
}
88-
return result;
89-
}
90-
);
91-
return $metadataResult;
86+
return [result];
87+
});
88+
},
9289
},
9390
},
9491
},

src/plugins/GetSubqueryMetadataPlugin.ts

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import {
88
MULTI_METADATA_REGEX,
99
TableEstimate,
1010
} from '@subql/utils';
11-
import { makeExtendSchemaPlugin, gql, ExtensionDefinition } from 'graphile-utils';
12-
import { PgClient, withPgClientTransaction } from 'postgraphile/@dataplan/pg';
13-
import { constant } from 'postgraphile/grafast';
14-
import { ArgsInterface } from '../config/yargs';
11+
import { gql, ExtensionDefinition, extendSchema } from 'graphile-utils';
12+
import { loadOneWithPgClient, PgClient } from 'postgraphile/@dataplan/pg';
13+
import { constant, FieldArgs, Step } from 'postgraphile/grafast';
1514

1615
const extensionsTypeDefs: ExtensionDefinition['typeDefs'] = gql`
1716
type TableEstimate {
@@ -66,17 +65,17 @@ function matchMetadataTableName(name: string): boolean {
6665
async function getTableEstimate(schemaName: string, pgClient: PgClient) {
6766
const { rows } = await pgClient.query<TableEstimate>({
6867
text: `select relname as table , reltuples::bigint as estimate from pg_class
69-
where
68+
where
7069
relnamespace in (select oid from pg_namespace where nspname = $1)
71-
and
70+
and
7271
relname in (select table_name from information_schema.tables where table_schema = $1)`,
7372
values: [schemaName],
7473
});
7574
return rows;
7675
}
7776

78-
export function CreateSubqueryMetadataPlugin(schemaName: string, args: ArgsInterface): GraphileConfig.Plugin {
79-
return makeExtendSchemaPlugin((build) => {
77+
export function CreateSubqueryMetadataPlugin(schemaName: string): GraphileConfig.Plugin {
78+
return extendSchema((build) => {
8079
// Find all metadata table
8180
const pgResources = build.input.pgRegistry.pgResources;
8281
const metadataTables = Object.keys(build.input.pgRegistry.pgResources).filter((tableName) =>
@@ -92,83 +91,83 @@ export function CreateSubqueryMetadataPlugin(schemaName: string, args: ArgsInter
9291

9392
return {
9493
typeDefs: extensionsTypeDefs,
95-
96-
plans: {
94+
objects: {
9795
Query: {
98-
_metadata($parent, { $chainId }, ...args) {
99-
const totalCountInput = $parent.get('totalCount');
100-
if ($chainId === undefined) {
101-
return;
102-
}
103-
104-
const chainId = $chainId.eval();
105-
const metadataTableName = chainId ? getMetadataTableName(chainId) : '_metadata';
106-
const $metadata = metadataPgResource[metadataTableName];
107-
if (!$metadata) throw new Error(`Not Found Metadata, chainId: ${chainId}`);
108-
const $metadataResult = withPgClientTransaction(
109-
$metadata.executor,
110-
$chainId,
111-
async (pgClient, input): Promise<Partial<MetaData>> => {
112-
const rowCountEstimate = await getTableEstimate(schemaName, pgClient);
113-
const { rows } = await pgClient.query<MetaEntry>({
114-
text: `select value, key from "${schemaName}"."${metadataTableName}"`,
115-
});
116-
const result: Record<string, unknown> = {};
117-
rows.forEach((item) => {
118-
if (META_JSON_FIELDS.includes(item.key)) {
119-
result[item.key] = JSON.parse(item.value as string);
120-
} else {
121-
result[item.key] = item.value;
122-
}
123-
});
124-
125-
result.rowCountEstimate = rowCountEstimate;
126-
result.queryNodeVersion = packageVersion;
127-
result.queryNodeStyle = 'subgraph';
128-
return result;
96+
plans: {
97+
_metadata($parent: Step, { $chainId }: FieldArgs) {
98+
if ($chainId === undefined) {
99+
return;
129100
}
130-
);
131101

132-
return $metadataResult;
133-
},
134-
_metadatas(_, $input) {
135-
const totalCount = Object.keys(metadataPgResource).length;
136-
const pgTable = metadataPgResource[metadataTables[0]];
137-
if (!totalCount || !pgTable) {
138-
return constant({ totalCount: 0, nodes: [] });
139-
}
102+
const chainId = ($chainId as any).eval();
103+
const metadataTableName = chainId ? getMetadataTableName(chainId) : '_metadata';
104+
const $metadata = metadataPgResource[metadataTableName];
105+
if (!$metadata) throw new Error(`Not Found Metadata, chainId: ${chainId}`);
140106

141-
const $metadataResult = withPgClientTransaction(
142-
pgTable.executor,
143-
$input.getRaw(''),
144-
async (pgClient, input): Promise<MetadatasConnection> => {
145-
const rowCountEstimate = await getTableEstimate(schemaName, pgClient);
146-
const nodes = await Promise.all(
147-
metadataTables.map(async (tableName): Promise<Partial<MetaData>> => {
148-
const { rows } = await pgClient.query({
149-
text: `select value, key from "${schemaName}"."${tableName}"`,
150-
});
151-
const result: Record<string, unknown> = {};
152-
rows.forEach((item: any) => {
153-
if (META_JSON_FIELDS.includes(item.key)) {
154-
result[item.key] = JSON.parse(item.value);
155-
} else {
156-
result[item.key] = item.value;
157-
}
158-
});
107+
return loadOneWithPgClient(
108+
$metadata.executor,
109+
$chainId,
110+
async (
111+
pgClient /*, [chainId]: readonly string[] */
112+
): Promise<[MetadatasConnection]> => {
113+
const rowCountEstimate = await getTableEstimate(schemaName, pgClient);
114+
const { rows } = await pgClient.query<MetaEntry>({
115+
text: `select value, key from "${schemaName}"."${metadataTableName}"`,
116+
});
117+
const result: Record<string, unknown> = {};
118+
rows.forEach((item) => {
119+
if (META_JSON_FIELDS.includes(item.key)) {
120+
result[item.key] = JSON.parse(item.value as string);
121+
} else {
122+
result[item.key] = item.value;
123+
}
124+
});
159125

160-
result.rowCountEstimate = rowCountEstimate;
161-
result.queryNodeVersion = packageVersion;
162-
result.queryNodeStyle = 'subgraph';
163-
return result;
164-
})
165-
);
166-
167-
return { totalCount, nodes };
126+
result.rowCountEstimate = rowCountEstimate;
127+
result.queryNodeVersion = packageVersion;
128+
result.queryNodeStyle = 'subgraph';
129+
return [result];
130+
}
131+
);
132+
},
133+
// NOTE there are no tests for this
134+
_metadatas(_: Step, $input: FieldArgs) {
135+
const totalCount = Object.keys(metadataPgResource).length;
136+
const pgTable = metadataPgResource[metadataTables[0]];
137+
if (!totalCount || !pgTable) {
138+
return constant({ totalCount: 0, nodes: [] });
168139
}
169-
);
170140

171-
return $metadataResult;
141+
return loadOneWithPgClient(
142+
pgTable.executor,
143+
$input.getRaw(''),
144+
async (pgClient): Promise<[MetadatasConnection]> => {
145+
const rowCountEstimate = await getTableEstimate(schemaName, pgClient);
146+
const nodes = await Promise.all(
147+
metadataTables.map(async (tableName): Promise<Partial<MetaData>> => {
148+
const { rows } = await pgClient.query<{ key: string; value: string }>({
149+
text: `select value, key from "${schemaName}"."${tableName}"`,
150+
});
151+
const result: Record<string, unknown> = {};
152+
rows.forEach((item) => {
153+
if (META_JSON_FIELDS.includes(item.key)) {
154+
result[item.key] = JSON.parse(item.value);
155+
} else {
156+
result[item.key] = item.value;
157+
}
158+
});
159+
160+
result.rowCountEstimate = rowCountEstimate;
161+
result.queryNodeVersion = packageVersion;
162+
result.queryNodeStyle = 'subgraph';
163+
return result;
164+
})
165+
);
166+
167+
return [{ totalCount, nodes }];
168+
}
169+
);
170+
},
172171
},
173172
},
174173
},

src/plugins/PgRowByVirtualIdPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ return function (resource) {
163163
function plan(_$root: any, args: FieldArgs) {
164164
const spec = Object.create(null);
165165
for (const attributeName in detailsByAttributeName) {
166-
spec[attributeName] = args.get(
166+
spec[attributeName] = args.getRaw(
167167
detailsByAttributeName[attributeName].graphqlName
168168
);
169169
}

src/plugins/filter/ArgFilterAttributesPlugin.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: GPL-3.0
33

44
// refer https://github.com/graphile-contrib/postgraphile-plugin-connection-filter/blob/375f125/src/PgConnectionArgFilterAttributesPlugin.ts
5-
import type { PgConditionStep } from '@dataplan/pg';
6-
import { getFieldDefine, getSupportOperators, Operators } from './utils';
5+
import type { PgCondition } from '@dataplan/pg';
6+
import { getFieldDefine, getSupportOperators, isEmpty, Operators } from './utils';
77

88
export const ArgFilterAttributesPlugin: GraphileConfig.Plugin = {
99
name: 'ArgFilterAttributesPlugin',
@@ -56,23 +56,21 @@ export const ArgFilterAttributesPlugin: GraphileConfig.Plugin = {
5656
{
5757
type: fieldDefine.type,
5858
description: 'filter condition field',
59-
inputPlan: EXPORTABLE(
60-
(escapeLikeWildcards) => (input) => {
61-
return `%${escapeLikeWildcards(input)}%` as any;
62-
},
63-
[escapeLikeWildcards]
64-
),
59+
// TODO unable to find migration for this
60+
// inputPlan: EXPORTABLE(
61+
// (escapeLikeWildcards) => (input: unknown) => {
62+
// return `%${escapeLikeWildcards(input)}%` as any;
63+
// },
64+
// [escapeLikeWildcards]
65+
// ),
6566
// eslint-disable-next-line complexity
66-
applyPlan: EXPORTABLE(
67+
apply: EXPORTABLE(
6768
() =>
6869
/* eslint-disable complexity */
69-
function ($where: PgConditionStep<any>, fieldArgs) {
70-
const $input = fieldArgs.getRaw();
71-
if ($input.evalIs(undefined)) {
70+
function ($where: PgCondition<any>, inputValue: any) {
71+
if (isEmpty(inputValue)) {
7272
return;
7373
}
74-
75-
let inputValue = $input.eval();
7674
switch (operator) {
7775
case Operators.CONTAINS:
7876
case Operators.NOT_CONTAINS:

0 commit comments

Comments
 (0)