From c7bcd4c2761d04ce3907caee741371bc08371334 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Thu, 9 Nov 2023 16:33:35 +0530 Subject: [PATCH] Generate resolvers for plural subgraph GQL queries (#461) --- packages/codegen/src/resolvers.ts | 9 +++++-- .../templates/resolvers-template.handlebars | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/codegen/src/resolvers.ts b/packages/codegen/src/resolvers.ts index a4a9ea16..9f728cd7 100644 --- a/packages/codegen/src/resolvers.ts +++ b/packages/codegen/src/resolvers.ts @@ -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); } } diff --git a/packages/codegen/src/templates/resolvers-template.handlebars b/packages/codegen/src/templates/resolvers-template.handlebars index cb6b1fff..b32c4870 100644 --- a/packages/codegen/src/templates/resolvers-template.handlebars +++ b/packages/codegen/src/templates/resolvers-template.handlebars @@ -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);