Generate resolvers for plural subgraph GQL queries (#461)

This commit is contained in:
prathamesh0 2023-11-09 16:33:35 +05:30 committed by GitHub
parent 6860f1a26f
commit c7bcd4c276
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import { Writable } from 'stream';
import Handlebars from 'handlebars';
import assert from 'assert';
import _ from 'lodash';
import pluralize from 'pluralize';
import { getGqlForSol, getTsForGql } from './utils/type-mappings';
import { Param } from './utils/types';
@ -63,12 +64,16 @@ export class Resolvers {
const entityName = subgraphTypeDef.name.value;
const queryName = `${entityName.charAt(0).toLowerCase()}${entityName.slice(1)}`;
let pluralQueryName = pluralize(queryName);
pluralQueryName = (pluralQueryName === queryName) ? `${pluralQueryName}s` : pluralQueryName;
// Add singular and plural query names
const queryObject = {
entityName,
queryName
queryName,
pluralQueryName
};
// TODO: Generate plural query resolvers
this._subgraphQueries.push(queryObject);
}
}

View File

@ -18,6 +18,7 @@ import {
GraphQLBigDecimal,
{{#if (subgraphPath)}}
BlockHeight,
OrderDirection,
jsonBigIntStringReplacer,
{{/if}}
EventWatcher,
@ -113,6 +114,29 @@ export const createResolvers = async (indexerArg: IndexerInterface, eventWatcher
return indexer.getSubgraphEntity({{this.entityName}}, id, block, info.fieldNodes[0].selectionSet.selections);
},
{{this.pluralQueryName}}: async (
_: any,
{ block = {}, where, first, skip, orderBy, orderDirection }: { block: BlockHeight, where: { [key: string]: any }, first: number, skip: number, orderBy: string, orderDirection: OrderDirection },
__: any,
info: GraphQLResolveInfo
) => {
log('{{this.pluralQueryName}}', JSON.stringify(block, jsonBigIntStringReplacer), JSON.stringify(where, jsonBigIntStringReplacer), first, skip, orderBy, orderDirection);
gqlTotalQueryCount.inc(1);
gqlQueryCount.labels('{{this.pluralQueryName}}').inc(1);
assert(info.fieldNodes[0].selectionSet);
// Set cache-control hints
// setGQLCacheHints(info, block, gqlCacheConfig);
return indexer.getSubgraphEntities(
{{this.entityName}},
block,
where,
{ limit: first, skip, orderBy, orderDirection },
info.fieldNodes[0].selectionSet.selections
);
},
{{/each}}
events: async (_: any, { blockHash, contractAddress, name }: { blockHash: string, contractAddress: string, name?: string }) => {
log('events', blockHash, contractAddress, name);