Skip to content

Commit

Permalink
Update query service to support historical by timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Nov 11, 2024
1 parent 2367210 commit 163b922
Showing 1 changed file with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ function addQueryContext(queryBuilder: QueryBuilder, sql: any, blockHeight: any)
}
}

export const PgBlockHeightPlugin: Plugin = (builder) => {
export const PgBlockHeightPlugin: Plugin = async (builder, options) => {
const [schemaName] = options.pgSchemas;
const {rows} = await options.pgConfig.query(
`SELECT * FROM "${schemaName}"._metadata WHERE key = 'historicalStateEnabled'`
);

// Note this varies from node where true is allowed because of legacy support
const historicalMode = rows[0].value as boolean | 'block' | 'timestamp';
// Adds blockHeight condition to join clause when joining a table that has _block_range column
builder.hook(
'GraphQLObjectType:fields:field',
Expand All @@ -41,9 +48,10 @@ export const PgBlockHeightPlugin: Plugin = (builder) => {
return field;
}

addArgDataGenerator(({blockHeight}) => ({
addArgDataGenerator(({blockHeight, timestamp}) => ({
pgQuery: (queryBuilder: QueryBuilder) => {
addQueryContext(queryBuilder, sql, blockHeight);
// If timestamp provided use that as the value
addQueryContext(queryBuilder, sql, blockHeight ?? timestamp);
addRangeQuery(queryBuilder, sql);
},
}));
Expand All @@ -56,7 +64,10 @@ export const PgBlockHeightPlugin: Plugin = (builder) => {
(
args,
{extend, pgSql: sql},
{addArgDataGenerator, scope: {isPgFieldConnection, isPgRowByUniqueConstraintField, pgFieldIntrospection}}
{
addArgDataGenerator,
scope: {isPgFieldConnection, isPgRowByUniqueConstraintField, pgFieldIntrospection},
}: Context<any>
) => {
if (!isPgRowByUniqueConstraintField && !isPgFieldConnection) {
return args;
Expand All @@ -65,16 +76,27 @@ export const PgBlockHeightPlugin: Plugin = (builder) => {
return args;
}

addArgDataGenerator(({blockHeight}) => ({
addArgDataGenerator(({blockHeight, timestamp}) => ({
pgQuery: (queryBuilder: QueryBuilder) => {
addQueryContext(queryBuilder, sql, blockHeight);
// If timestamp provided use that as the value
addQueryContext(queryBuilder, sql, blockHeight ?? timestamp);
addRangeQuery(queryBuilder, sql);
},
}));

if (historicalMode === 'timestamp') {
return extend(args, {
timestamp: {
description: 'When specified, the query will return results as of this timestamp. Unix timestamp in MS',
defaultValue: '9223372036854775807',
type: GraphQLString, // String because of int overflow
},
});
}

return extend(args, {
blockHeight: {
description: 'Block height',
description: 'When specified, the query will return results as of this block height',
defaultValue: '9223372036854775807',
type: GraphQLString, // String because of int overflow
},
Expand Down

0 comments on commit 163b922

Please sign in to comment.