Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GQL mutations #256

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/codegen/src/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
3 changes: 2 additions & 1 deletion packages/codegen/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions packages/codegen/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/codegen/src/templates/resolvers-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down