mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-08 12:28:05 +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; } = {};
|
const queryObject: { [key: string]: any; } = {};
|
||||||
queryObject[queryName] = {
|
queryObject[queryName] = {
|
||||||
// Get type composer object for return type from the schema composer.
|
// Get type composer object for return type from the schema composer.
|
||||||
type: this._composer.getAnyTC(subgraphType).NonNull,
|
type: this._composer.getAnyTC(subgraphType),
|
||||||
args: {
|
args: {
|
||||||
id: 'ID!',
|
id: 'ID!',
|
||||||
block: BlockHeight
|
block: BlockHeight
|
||||||
|
@ -515,7 +515,15 @@ export class Database {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
// Fetching blockHash for previous entity in frothy region.
|
// 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 };
|
return { blockHash, blockNumber, id };
|
||||||
}
|
}
|
||||||
|
@ -222,18 +222,14 @@ export class GraphDatabase {
|
|||||||
block: CanonicalBlockHeight = {},
|
block: CanonicalBlockHeight = {},
|
||||||
selections: ReadonlyArray<SelectionNode> = []
|
selections: ReadonlyArray<SelectionNode> = []
|
||||||
): Promise<Entity | undefined> {
|
): Promise<Entity | undefined> {
|
||||||
let { hash: blockHash, number: blockNumber } = block;
|
const { hash: blockHash, number: blockNumber } = block;
|
||||||
const repo = queryRunner.manager.getRepository<Entity>(entityType);
|
const repo = queryRunner.manager.getRepository<Entity>(entityType);
|
||||||
const whereOptions: any = { id };
|
const whereOptions: any = { id };
|
||||||
|
|
||||||
if (blockNumber) {
|
|
||||||
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (blockHash) {
|
if (blockHash) {
|
||||||
whereOptions.blockHash = blockHash;
|
whereOptions.blockHash = blockHash;
|
||||||
const block = await this._baseDatabase.getBlockProgress(queryRunner.manager.getRepository('block_progress'), blockHash);
|
} else if (blockNumber) {
|
||||||
blockNumber = block?.blockNumber;
|
whereOptions.blockNumber = LessThanOrEqual(blockNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
const findOptions = {
|
const findOptions = {
|
||||||
|
Loading…
Reference in New Issue
Block a user