Add GQL mutations support (#256)

This commit is contained in:
prathamesh0 2021-09-29 13:49:01 +05:30 committed by GitHub
parent 40574cf3d9
commit 278fe30a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 4 deletions

View File

@ -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'))

View File

@ -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,

View File

@ -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.

View File

@ -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);
}

View File

@ -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

View File

@ -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);
}
/**