mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 20:36:19 +00:00
Add GQL mutations support (#256)
This commit is contained in:
parent
40574cf3d9
commit
278fe30a2c
@ -146,7 +146,7 @@ function generateWatcher (data: string, visitor: Visitor, argv: any) {
|
||||
outStream = outputDir
|
||||
? fs.createWriteStream(path.join(outputDir, 'src/indexer.ts'))
|
||||
: process.stdout;
|
||||
visitor.exportIndexer(outStream, inputFileName);
|
||||
visitor.exportIndexer(outStream, inputFileName, argv['contract-name']);
|
||||
|
||||
outStream = outputDir
|
||||
? fs.createWriteStream(path.join(outputDir, 'src/server.ts'))
|
||||
|
@ -86,11 +86,12 @@ export class Indexer {
|
||||
* @param outStream A writable output stream to write the indexer file to.
|
||||
* @param inputFileName Input contract file name to be passed to the template.
|
||||
*/
|
||||
exportIndexer (outStream: Writable, inputFileName: string): void {
|
||||
exportIndexer (outStream: Writable, inputFileName: string, contractName: string): void {
|
||||
const template = Handlebars.compile(this._templateString);
|
||||
|
||||
const obj = {
|
||||
inputFileName,
|
||||
contractName,
|
||||
queries: this._queries,
|
||||
constants: {
|
||||
MODE_ETH_CALL,
|
||||
|
@ -94,6 +94,9 @@ export class Schema {
|
||||
* @returns GraphQLSchema object.
|
||||
*/
|
||||
buildSchema (): GraphQLSchema {
|
||||
// Add a mutation for watching a contract.
|
||||
this._addWatchContractMutation();
|
||||
|
||||
return this._composer.buildSchema();
|
||||
}
|
||||
|
||||
@ -239,6 +242,22 @@ export class Schema {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a watchContract mutation to the schema.
|
||||
*/
|
||||
_addWatchContractMutation (): void {
|
||||
// Add a mutation to the schema composer.
|
||||
this._composer.Mutation.addFields({
|
||||
watchContract: {
|
||||
type: 'Boolean!',
|
||||
args: {
|
||||
contractAddress: 'String!',
|
||||
startingBlock: 'Int'
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an 'Event' union (if doesn't exist) to the schema. Adds the specified event to the 'Event' union.
|
||||
* @param event Event type name to add to the union.
|
||||
|
@ -203,6 +203,13 @@ export class Indexer {
|
||||
return { eventName, eventInfo };
|
||||
}
|
||||
|
||||
async watchContract (address: string, startingBlock: number): Promise<boolean> {
|
||||
// Always use the checksum address (https://docs.ethers.io/v5/api/utils/address/#utils-getAddress).
|
||||
await this._db.saveContract(ethers.utils.getAddress(address), '{{contractName}}', startingBlock);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async getEventsByFilter (blockHash: string, contract: string, name: string | null): Promise<Array<Event>> {
|
||||
return this._baseIndexer.getEventsByFilter(blockHash, contract, name);
|
||||
}
|
||||
|
@ -33,6 +33,13 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch
|
||||
}
|
||||
},
|
||||
|
||||
Mutation: {
|
||||
watchContract: (_: any, { contractAddress, startingBlock = 1 }: { contractAddress: string, startingBlock: number }): Promise<boolean> => {
|
||||
log('watchContract', contractAddress, startingBlock);
|
||||
return indexer.watchContract(contractAddress, startingBlock);
|
||||
}
|
||||
},
|
||||
|
||||
Query: {
|
||||
{{#each queries}}
|
||||
{{this.name}}: (_: any, { blockHash, contractAddress
|
||||
|
@ -115,8 +115,8 @@ export class Visitor {
|
||||
* @param outStream A writable output stream to write the indexer file to.
|
||||
* @param inputFileName Input contract file name to be passed to the template.
|
||||
*/
|
||||
exportIndexer (outStream: Writable, inputFileName: string): void {
|
||||
this._indexer.exportIndexer(outStream, inputFileName);
|
||||
exportIndexer (outStream: Writable, inputFileName: string, contractName: string): void {
|
||||
this._indexer.exportIndexer(outStream, inputFileName, contractName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user