mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-08 00:44:06 +00:00
Handle event name conflicts in schema and update hooks in codegen (#97)
* Change return type for subgraph resolvers and update hooks generation * Handle event name conflicts in schema generation
This commit is contained in:
@@ -71,6 +71,8 @@ export class Schema {
|
||||
|
||||
// Check if the type is already added.
|
||||
if (this._composer.has(name)) {
|
||||
this._resolveEventConflict(name, params);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -422,4 +424,38 @@ export class Schema {
|
||||
// Add a new type to the union.
|
||||
eventUnion.addType(this._composer.getOTC(event));
|
||||
}
|
||||
|
||||
_resolveEventConflict (name: string, params: Array<Param>): void {
|
||||
const eventTC = this._composer.getOTC(name);
|
||||
const currentFields = eventTC.getFieldNames();
|
||||
|
||||
// Get the common fields.
|
||||
let commonFields: string[] = [];
|
||||
commonFields = params.reduce((acc, curr) => {
|
||||
if (currentFields.includes(curr.name)) {
|
||||
acc.push(curr.name);
|
||||
}
|
||||
return acc;
|
||||
}, commonFields);
|
||||
|
||||
// Make the current fields that are uncommon nullable.
|
||||
currentFields.forEach((field: string) => {
|
||||
if (!commonFields.includes(field)) {
|
||||
eventTC.makeFieldNullable(field);
|
||||
}
|
||||
});
|
||||
|
||||
// Get the new fields.
|
||||
const newFields: any = {};
|
||||
params.forEach((param: Param) => {
|
||||
if (!commonFields.includes(param.name)) {
|
||||
const tsCurrType = getTsForSol(param.type);
|
||||
assert(tsCurrType, `ts type for sol type ${param.type} for ${param.name} not found`);
|
||||
newFields[param.name] = `${getGqlForTs(tsCurrType)}`;
|
||||
}
|
||||
});
|
||||
|
||||
// Add the new fields to the current type.
|
||||
eventTC.addFields(newFields);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
import assert from 'assert';
|
||||
|
||||
// import { updateStateForMappingType, updateStateForElementaryType } from '@vulcanize/util';
|
||||
|
||||
import { Indexer, ResultEvent } from './indexer';
|
||||
|
||||
/**
|
||||
@@ -18,12 +20,16 @@ export async function createInitialState (indexer: Indexer, contractAddress: str
|
||||
assert(blockHash);
|
||||
assert(contractAddress);
|
||||
|
||||
// Store an empty state in an IPLDBlock.
|
||||
// Store the desired initial state in an IPLDBlock.
|
||||
const ipldBlockData: any = {
|
||||
state: {}
|
||||
};
|
||||
|
||||
// Use updateStateForMappingType to update initial state.
|
||||
// Use updateStateForElementaryType to update initial state with an elementary property.
|
||||
// Eg. const ipldBlockData = updateStateForElementaryType(ipldBlockData, '_totalBalance', result.value.toString());
|
||||
|
||||
// Use updateStateForMappingType to update initial state with a nested property.
|
||||
// Eg. const ipldBlockData = updateStateForMappingType(ipldBlockData, '_allowances', [owner, spender], allowance.value.toString());
|
||||
|
||||
// Return initial state data to be saved.
|
||||
return ipldBlockData;
|
||||
@@ -38,7 +44,7 @@ export async function createStateDiff (indexer: Indexer, blockHash: string): Pro
|
||||
assert(indexer);
|
||||
assert(blockHash);
|
||||
|
||||
// Use indexer.createStateDiff() method to create a custom diff.
|
||||
// Use indexer.createStateDiff() method to save custom state diff(s).
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,6 +61,8 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
||||
|
||||
// Use indexer.createStateCheckpoint() method to create a custom checkpoint.
|
||||
|
||||
// Return false to update the state created by this hook by auto-generated checkpoint state.
|
||||
// Return true to disable update of the state created by this hook by auto-generated checkpoint state.
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -66,4 +74,7 @@ export async function createStateCheckpoint (indexer: Indexer, contractAddress:
|
||||
export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Promise<void> {
|
||||
assert(indexer);
|
||||
assert(eventData);
|
||||
|
||||
// Use indexer methods to index data.
|
||||
// Pass `diff` parameter to indexer methods as true to save an auto-generated state from the indexed data.
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ export const createResolvers = async (indexer: Indexer, eventWatcher: EventWatch
|
||||
{{/each}}
|
||||
|
||||
{{~#each subgraphQueries}}
|
||||
{{this.queryName}}: async (_: any, { id, block = {} }: { id: string, block: BlockHeight }): Promise<{{this.entityName}} | undefined> => {
|
||||
{{this.queryName}}: async (_: any, { id, block = {} }: { id: string, block: BlockHeight }) => {
|
||||
log('{{this.queryName}}', id, block);
|
||||
|
||||
return indexer.getSubgraphEntity({{this.entityName}}, id, block);
|
||||
|
||||
Reference in New Issue
Block a user