-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support subqery _metadata query #17
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e836efe
Support subqery _metadata query
yoozo d574efd
remove index flag
yoozo 00f1412
unit test
yoozo bdab6d2
GraphiQL control flag
yoozo 1ea57d9
set graphiqlPath
yoozo da09cf0
health check api
yoozo 29300f7
change log
yoozo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import { URL } from 'url'; | ||
import { | ||
getMetadataTableName, | ||
MetaData, | ||
METADATA_REGEX, | ||
MULTI_METADATA_REGEX, | ||
TableEstimate, | ||
} from '@subql/utils'; | ||
import { makeExtendSchemaPlugin, gql, ExtensionDefinition } from 'graphile-utils'; | ||
import { PgClient, withPgClientTransaction } from 'postgraphile/@dataplan/pg'; | ||
import { constant } from 'postgraphile/grafast'; | ||
import { ArgsInterface } from '../config/yargs'; | ||
|
||
const extensionsTypeDefs: ExtensionDefinition['typeDefs'] = gql` | ||
type TableEstimate { | ||
table: String | ||
estimate: Int | ||
} | ||
type _Metadata { | ||
lastProcessedHeight: Int | ||
lastProcessedTimestamp: Date | ||
targetHeight: Int | ||
chain: String | ||
specName: String | ||
genesisHash: String | ||
startHeight: Int | ||
indexerHealthy: Boolean | ||
indexerNodeVersion: String | ||
queryNodeVersion: String | ||
queryNodeStyle: String | ||
rowCountEstimate: [TableEstimate] | ||
dynamicDatasources: [JSON] | ||
evmChainId: String | ||
deployments: JSON | ||
lastFinalizedVerifiedHeight: Int | ||
unfinalizedBlocks: String | ||
lastCreatedPoiHeight: Int | ||
latestSyncedPoiHeight: Int | ||
dbSize: BigInt | ||
} | ||
type _Metadatas { | ||
totalCount: Int! | ||
nodes: [_Metadata]! | ||
} | ||
extend type Query { | ||
_metadata(chainId: String): _Metadata | ||
_metadatas(after: Cursor, before: Cursor): _Metadatas | ||
} | ||
`; | ||
type MetaType = number | string | boolean; | ||
type MetaEntry = { key: string; value: MetaType }; | ||
type MetadatasConnection = { | ||
totalCount?: number; | ||
nodes?: Partial<MetaData>[]; | ||
}; | ||
|
||
const { version: packageVersion } = require('../../package.json'); | ||
const META_JSON_FIELDS = ['deployments']; | ||
|
||
function matchMetadataTableName(name: string): boolean { | ||
return METADATA_REGEX.test(name) || MULTI_METADATA_REGEX.test(name); | ||
} | ||
|
||
async function getTableEstimate(schemaName: string, pgClient: PgClient) { | ||
const { rows } = await pgClient.query<TableEstimate>({ | ||
text: `select relname as table , reltuples::bigint as estimate from pg_class | ||
where | ||
relnamespace in (select oid from pg_namespace where nspname = $1) | ||
and | ||
relname in (select table_name from information_schema.tables where table_schema = $1)`, | ||
values: [schemaName], | ||
}); | ||
return rows; | ||
} | ||
|
||
export function CreateSubqueryMetadataPlugin(schemaName: string, args: ArgsInterface) { | ||
Check warning on line 79 in src/plugins/GetSubqueryMetadataPlugin.ts GitHub Actions / code-style
|
||
return makeExtendSchemaPlugin((build) => { | ||
// Find all metadata table | ||
const pgResources = build.input.pgRegistry.pgResources; | ||
const metadataTables = Object.keys(build.input.pgRegistry.pgResources).filter((tableName) => | ||
matchMetadataTableName(tableName) | ||
); | ||
const metadataPgResource = metadataTables.reduce( | ||
(result, key) => { | ||
result[key] = pgResources[key]; | ||
return result; | ||
}, | ||
{} as { [key: string]: (typeof pgResources)[keyof typeof pgResources] } | ||
); | ||
|
||
return { | ||
typeDefs: extensionsTypeDefs, | ||
|
||
plans: { | ||
Query: { | ||
_metadata($parent, { $chainId }, ...args) { | ||
const totalCountInput = $parent.get('totalCount'); | ||
if ($chainId === undefined) { | ||
return; | ||
} | ||
|
||
const chainId = $chainId.eval(); | ||
const metadataTableName = chainId ? getMetadataTableName(chainId) : '_metadata'; | ||
const $metadata = metadataPgResource[metadataTableName]; | ||
if (!$metadata) throw new Error(`Not Found Metadata, chainId: ${chainId}`); | ||
const $metadataResult = withPgClientTransaction( | ||
$metadata.executor, | ||
$chainId, | ||
async (pgClient, input): Promise<Partial<MetaData>> => { | ||
const rowCountEstimate = await getTableEstimate(schemaName, pgClient); | ||
const { rows } = await pgClient.query<MetaEntry>({ | ||
text: `select value, key from "${schemaName}"."${metadataTableName}"`, | ||
}); | ||
const result: Record<string, unknown> = {}; | ||
rows.forEach((item) => { | ||
if (META_JSON_FIELDS.includes(item.key)) { | ||
result[item.key] = JSON.parse(item.value as string); | ||
} else { | ||
result[item.key] = item.value; | ||
} | ||
}); | ||
|
||
result.rowCountEstimate = rowCountEstimate; | ||
result.queryNodeVersion = packageVersion; | ||
result.queryNodeStyle = 'subgraph'; | ||
return result; | ||
} | ||
); | ||
|
||
return $metadataResult; | ||
}, | ||
_metadatas(_, $input) { | ||
const totalCount = Object.keys(metadataPgResource).length; | ||
const pgTable = metadataPgResource[metadataTables[0]]; | ||
if (!totalCount || !pgTable) { | ||
return constant({ totalCount: 0, nodes: [] }); | ||
} | ||
|
||
const $metadataResult = withPgClientTransaction( | ||
pgTable.executor, | ||
$input.getRaw(''), | ||
async (pgClient, input): Promise<MetadatasConnection> => { | ||
const rowCountEstimate = await getTableEstimate(schemaName, pgClient); | ||
const nodes = await Promise.all( | ||
metadataTables.map(async (tableName): Promise<Partial<MetaData>> => { | ||
const { rows } = await pgClient.query({ | ||
text: `select value, key from "${schemaName}"."${tableName}"`, | ||
}); | ||
const result: Record<string, unknown> = {}; | ||
rows.forEach((item: any) => { | ||
if (META_JSON_FIELDS.includes(item.key)) { | ||
result[item.key] = JSON.parse(item.value); | ||
} else { | ||
result[item.key] = item.value; | ||
} | ||
}); | ||
|
||
result.rowCountEstimate = rowCountEstimate; | ||
result.queryNodeVersion = packageVersion; | ||
result.queryNodeStyle = 'subgraph'; | ||
return result; | ||
}) | ||
); | ||
|
||
return { totalCount, nodes }; | ||
} | ||
); | ||
|
||
return $metadataResult; | ||
}, | ||
}, | ||
}, | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the current behaviour? I think its probably fine we dont support it but also we should provide an appropriate error if we don't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, only single-chain projects are supported, and if it is a multi-chain project, an error will be returned.