From bc1c2678134e228941b74b708935f2683b34ac99 Mon Sep 17 00:00:00 2001 From: nikugogoi Date: Tue, 9 Aug 2022 13:25:46 +0530 Subject: [PATCH] Add GQL query for sync status (#159) * Add sync status GQL query * Add sync status GQL query in codegen --- packages/codegen/src/schema.ts | 23 +++++++++++++++++++ .../templates/resolvers-template.handlebars | 8 ++++++- packages/eden-watcher/src/resolvers.ts | 8 ++++++- packages/eden-watcher/src/schema.gql | 8 +++++++ packages/erc20-watcher/src/resolvers.ts | 6 +++++ packages/erc20-watcher/src/schema.ts | 9 ++++++++ packages/erc721-watcher/src/resolvers.ts | 8 ++++++- packages/erc721-watcher/src/schema.gql | 8 +++++++ packages/graph-test-watcher/src/resolvers.ts | 8 ++++++- packages/graph-test-watcher/src/schema.gql | 8 +++++++ packages/mobymask-watcher/src/resolvers.ts | 8 ++++++- packages/mobymask-watcher/src/schema.gql | 8 +++++++ 12 files changed, 105 insertions(+), 5 deletions(-) diff --git a/packages/codegen/src/schema.ts b/packages/codegen/src/schema.ts index d2963947..fba31c08 100644 --- a/packages/codegen/src/schema.ts +++ b/packages/codegen/src/schema.ts @@ -95,6 +95,9 @@ export class Schema { // Add a mutation for watching a contract. this._addWatchContractMutation(); + // Add type and query for SyncStatus. + this._addSyncStatus(); + // Add IPLDBlock type and queries. this._addIPLDType(); this._addIPLDQuery(); @@ -331,6 +334,26 @@ export class Schema { }); } + _addSyncStatus (): void { + const typeComposer = this._composer.createObjectTC({ + name: 'SyncStatus', + fields: { + latestIndexedBlockHash: 'String!', + latestIndexedBlockNumber: 'Int!', + latestCanonicalBlockHash: 'String!', + latestCanonicalBlockNumber: 'Int!' + } + }); + + this._composer.addSchemaMustHaveType(typeComposer); + + this._composer.Query.addFields({ + getSyncStatus: { + type: this._composer.getOTC('SyncStatus') + } + }); + } + _addIPLDType (): void { const typeComposer = this._composer.createObjectTC({ name: 'ResultIPLDBlock', diff --git a/packages/codegen/src/templates/resolvers-template.handlebars b/packages/codegen/src/templates/resolvers-template.handlebars index 11c1e49c..871b55cd 100644 --- a/packages/codegen/src/templates/resolvers-template.handlebars +++ b/packages/codegen/src/templates/resolvers-template.handlebars @@ -114,12 +114,18 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; }, - getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Diff }: { blockHash: string, contractAddress: string, kind: string }) => { + getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Checkpoint }: { blockHash: string, contractAddress: string, kind: string }) => { log('getState', blockHash, contractAddress, kind); const ipldBlock = await indexer.getPrevIPLDBlock(blockHash, contractAddress, kind); return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; + }, + + getSyncStatus: async () => { + log('getSyncStatus'); + + return indexer.getSyncStatus(); } } }; diff --git a/packages/eden-watcher/src/resolvers.ts b/packages/eden-watcher/src/resolvers.ts index 18e259f9..707fb86b 100644 --- a/packages/eden-watcher/src/resolvers.ts +++ b/packages/eden-watcher/src/resolvers.ts @@ -217,12 +217,18 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; }, - getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Diff }: { blockHash: string, contractAddress: string, kind: string }) => { + getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Checkpoint }: { blockHash: string, contractAddress: string, kind: string }) => { log('getState', blockHash, contractAddress, kind); const ipldBlock = await indexer.getPrevIPLDBlock(blockHash, contractAddress, kind); return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; + }, + + getSyncStatus: async () => { + log('getSyncStatus'); + + return indexer.getSyncStatus(); } } }; diff --git a/packages/eden-watcher/src/schema.gql b/packages/eden-watcher/src/schema.gql index 5975aa67..90f03f5a 100644 --- a/packages/eden-watcher/src/schema.gql +++ b/packages/eden-watcher/src/schema.gql @@ -211,6 +211,13 @@ type ResultIPLDBlock { data: String! } +type SyncStatus { + latestIndexedBlockHash: String! + latestIndexedBlockNumber: Int! + latestCanonicalBlockHash: String! + latestCanonicalBlockNumber: Int! +} + type Query { events(blockHash: String!, contractAddress: String!, name: String): [ResultEvent!] eventsInRange(fromBlockNumber: Int!, toBlockNumber: Int!): [ResultEvent!] @@ -234,6 +241,7 @@ type Query { account(id: String!, block: Block_height): Account! getStateByCID(cid: String!): ResultIPLDBlock getState(blockHash: String!, contractAddress: String!, kind: String): ResultIPLDBlock + getSyncStatus: SyncStatus } type Producer { diff --git a/packages/erc20-watcher/src/resolvers.ts b/packages/erc20-watcher/src/resolvers.ts index 846f918a..a70fede2 100644 --- a/packages/erc20-watcher/src/resolvers.ts +++ b/packages/erc20-watcher/src/resolvers.ts @@ -97,6 +97,12 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch const events = await indexer.getEventsInRange(fromBlockNumber, toBlockNumber); return events.map(event => indexer.getResultEvent(event)); + }, + + getSyncStatus: async () => { + log('getSyncStatus'); + + return indexer.getSyncStatus(); } } }; diff --git a/packages/erc20-watcher/src/schema.ts b/packages/erc20-watcher/src/schema.ts index 6ff3a4a6..a75ba539 100644 --- a/packages/erc20-watcher/src/schema.ts +++ b/packages/erc20-watcher/src/schema.ts @@ -72,6 +72,13 @@ type WatchedEvent { event: ResultEvent! } +type SyncStatus { + latestIndexedBlockHash: String! + latestIndexedBlockNumber: Int! + latestCanonicalBlockHash: String! + latestCanonicalBlockNumber: Int! +} + # # Queries # @@ -139,6 +146,8 @@ type Query { fromBlockNumber: Int! toBlockNumber: Int! ): [ResultEvent!] + + getSyncStatus: SyncStatus } # diff --git a/packages/erc721-watcher/src/resolvers.ts b/packages/erc721-watcher/src/resolvers.ts index f912e3e7..e01bc6ed 100644 --- a/packages/erc721-watcher/src/resolvers.ts +++ b/packages/erc721-watcher/src/resolvers.ts @@ -166,12 +166,18 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; }, - getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Diff }: { blockHash: string, contractAddress: string, kind: string }) => { + getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Checkpoint }: { blockHash: string, contractAddress: string, kind: string }) => { log('getState', blockHash, contractAddress, kind); const ipldBlock = await indexer.getPrevIPLDBlock(blockHash, contractAddress, kind); return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; + }, + + getSyncStatus: async () => { + log('getSyncStatus'); + + return indexer.getSyncStatus(); } } }; diff --git a/packages/erc721-watcher/src/schema.gql b/packages/erc721-watcher/src/schema.gql index 6be95a11..dd358945 100644 --- a/packages/erc721-watcher/src/schema.gql +++ b/packages/erc721-watcher/src/schema.gql @@ -85,6 +85,13 @@ type ResultIPLDBlock { data: String! } +type SyncStatus { + latestIndexedBlockHash: String! + latestIndexedBlockNumber: Int! + latestCanonicalBlockHash: String! + latestCanonicalBlockNumber: Int! +} + type Query { events(blockHash: String!, contractAddress: String!, name: String): [ResultEvent!] eventsInRange(fromBlockNumber: Int!, toBlockNumber: Int!): [ResultEvent!] @@ -104,6 +111,7 @@ type Query { _operatorApprovals(blockHash: String!, contractAddress: String!, key0: String!, key1: String!): ResultBoolean! getStateByCID(cid: String!): ResultIPLDBlock getState(blockHash: String!, contractAddress: String!, kind: String): ResultIPLDBlock + getSyncStatus: SyncStatus transferCount(id: String!, block: Block_height): TransferCount! } diff --git a/packages/graph-test-watcher/src/resolvers.ts b/packages/graph-test-watcher/src/resolvers.ts index 83961978..917e0751 100644 --- a/packages/graph-test-watcher/src/resolvers.ts +++ b/packages/graph-test-watcher/src/resolvers.ts @@ -122,12 +122,18 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; }, - getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Diff }: { blockHash: string, contractAddress: string, kind: string }) => { + getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Checkpoint }: { blockHash: string, contractAddress: string, kind: string }) => { log('getState', blockHash, contractAddress, kind); const ipldBlock = await indexer.getPrevIPLDBlock(blockHash, contractAddress, kind); return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; + }, + + getSyncStatus: async () => { + log('getSyncStatus'); + + return indexer.getSyncStatus(); } } }; diff --git a/packages/graph-test-watcher/src/schema.gql b/packages/graph-test-watcher/src/schema.gql index b25d1c51..ea0441bd 100644 --- a/packages/graph-test-watcher/src/schema.gql +++ b/packages/graph-test-watcher/src/schema.gql @@ -86,6 +86,13 @@ type ResultIPLDBlock { data: String! } +type SyncStatus { + latestIndexedBlockHash: String! + latestIndexedBlockNumber: Int! + latestCanonicalBlockHash: String! + latestCanonicalBlockNumber: Int! +} + type Query { events(blockHash: String!, contractAddress: String!, name: String): [ResultEvent!] eventsInRange(fromBlockNumber: Int!, toBlockNumber: Int!): [ResultEvent!] @@ -96,6 +103,7 @@ type Query { category(id: String!, block: Block_height): Category! getStateByCID(cid: String!): ResultIPLDBlock getState(blockHash: String!, contractAddress: String!, kind: String): ResultIPLDBlock + getSyncStatus: SyncStatus } enum BlogKind { diff --git a/packages/mobymask-watcher/src/resolvers.ts b/packages/mobymask-watcher/src/resolvers.ts index 102f6775..0ca9b64c 100644 --- a/packages/mobymask-watcher/src/resolvers.ts +++ b/packages/mobymask-watcher/src/resolvers.ts @@ -110,7 +110,7 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; }, - getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Diff }: { blockHash: string, contractAddress: string, kind: string }) => { + getState: async (_: any, { blockHash, contractAddress, kind = StateKind.Checkpoint }: { blockHash: string, contractAddress: string, kind: string }) => { log('getState', blockHash, contractAddress, kind); const ipldBlock = await indexer.getPrevIPLDBlock(blockHash, contractAddress, kind); @@ -118,6 +118,12 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch return ipldBlock && ipldBlock.block.isComplete ? indexer.getResultIPLDBlock(ipldBlock) : undefined; }, + getSyncStatus: async () => { + log('getSyncStatus'); + + return indexer.getSyncStatus(); + }, + latestBlock: async () => { log('latestBlock'); diff --git a/packages/mobymask-watcher/src/schema.gql b/packages/mobymask-watcher/src/schema.gql index 3afa168a..b58f64ab 100644 --- a/packages/mobymask-watcher/src/schema.gql +++ b/packages/mobymask-watcher/src/schema.gql @@ -87,6 +87,13 @@ type ResultIPLDBlock { data: String! } +type SyncStatus { + latestIndexedBlockHash: String! + latestIndexedBlockNumber: Int! + latestCanonicalBlockHash: String! + latestCanonicalBlockNumber: Int! +} + type Query { events(blockHash: String!, contractAddress: String!, name: String): [ResultEvent!] eventsInRange(fromBlockNumber: Int!, toBlockNumber: Int!): [ResultEvent!] @@ -97,6 +104,7 @@ type Query { isMember(blockHash: String!, contractAddress: String!, key0: String!): ResultBoolean! getStateByCID(cid: String!): ResultIPLDBlock getState(blockHash: String!, contractAddress: String!, kind: String): ResultIPLDBlock + getSyncStatus: SyncStatus latestBlock: Block_height }