@@ -8,10 +8,9 @@ import {
8
8
MULTI_METADATA_REGEX ,
9
9
TableEstimate ,
10
10
} 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' ;
15
14
16
15
const extensionsTypeDefs : ExtensionDefinition [ 'typeDefs' ] = gql `
17
16
type TableEstimate {
@@ -66,17 +65,17 @@ function matchMetadataTableName(name: string): boolean {
66
65
async function getTableEstimate ( schemaName : string , pgClient : PgClient ) {
67
66
const { rows } = await pgClient . query < TableEstimate > ( {
68
67
text : `select relname as table , reltuples::bigint as estimate from pg_class
69
- where
68
+ where
70
69
relnamespace in (select oid from pg_namespace where nspname = $1)
71
- and
70
+ and
72
71
relname in (select table_name from information_schema.tables where table_schema = $1)` ,
73
72
values : [ schemaName ] ,
74
73
} ) ;
75
74
return rows ;
76
75
}
77
76
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 ) => {
80
79
// Find all metadata table
81
80
const pgResources = build . input . pgRegistry . pgResources ;
82
81
const metadataTables = Object . keys ( build . input . pgRegistry . pgResources ) . filter ( ( tableName ) =>
@@ -92,83 +91,83 @@ export function CreateSubqueryMetadataPlugin(schemaName: string, args: ArgsInter
92
91
93
92
return {
94
93
typeDefs : extensionsTypeDefs ,
95
-
96
- plans : {
94
+ objects : {
97
95
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 ;
129
100
}
130
- ) ;
131
101
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 } ` ) ;
140
106
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
+ } ) ;
159
125
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 : [ ] } ) ;
168
139
}
169
- ) ;
170
140
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
+ } ,
172
171
} ,
173
172
} ,
174
173
} ,
0 commit comments