mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
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:
parent
695723955f
commit
32d5cd10d0
@ -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
|
||||
|
@ -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 };
|
||||
}
|
||||
|
@ -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 = {
|
||||
|
Loading…
Reference in New Issue
Block a user