Prioritize block hash over number in singular time travel queries (#462)

* Keep return value for singular subgraph entity queries nullable

* Prioritize block hash over number in time travel queries

* Throw error when block with given hash doesn't exist in db
This commit is contained in:
prathamesh0 2023-11-10 09:39:58 +05:30 committed by GitHub
parent 695723955f
commit 32d5cd10d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View File

@ -193,7 +193,7 @@ export class Schema {
const queryObject: { [key: string]: any; } = {};
queryObject[queryName] = {
// Get type composer object for return type from the schema composer.
type: this._composer.getAnyTC(subgraphType).NonNull,
type: this._composer.getAnyTC(subgraphType),
args: {
id: 'ID!',
block: BlockHeight

View File

@ -515,7 +515,15 @@ export class Database {
`;
// Fetching blockHash for previous entity in frothy region.
const [{ block_hash: blockHash, block_number: blockNumber, id }] = await queryRunner.query(heirerchicalQuery, [data.blockHash, data.id, MAX_REORG_DEPTH]);
const result = await queryRunner.query(heirerchicalQuery, [data.blockHash, data.id, MAX_REORG_DEPTH]);
// Check if empty array returned
// (occurs when block at given hash doesn't exist in the db)
if (!result?.length) {
throw new Error('no block with that hash found');
}
const [{ block_hash: blockHash, block_number: blockNumber, id }] = result;
return { blockHash, blockNumber, id };
}

View File

@ -222,18 +222,14 @@ export class GraphDatabase {
block: CanonicalBlockHeight = {},
selections: ReadonlyArray<SelectionNode> = []
): Promise<Entity | undefined> {
let { hash: blockHash, number: blockNumber } = block;
const { hash: blockHash, number: blockNumber } = block;
const repo = queryRunner.manager.getRepository<Entity>(entityType);
const whereOptions: any = { id };
if (blockNumber) {
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
}
if (blockHash) {
whereOptions.blockHash = blockHash;
const block = await this._baseDatabase.getBlockProgress(queryRunner.manager.getRepository('block_progress'), blockHash);
blockNumber = block?.blockNumber;
} else if (blockNumber) {
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
}
const findOptions = {