Skip to content

Commit

Permalink
Generate resolvers for plural subgraph GQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Nov 9, 2023
1 parent 6860f1a commit 416c12f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/codegen/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
Expand Down
24 changes: 24 additions & 0 deletions packages/codegen/src/templates/resolvers-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GraphQLBigDecimal,
{{#if (subgraphPath)}}
BlockHeight,
OrderDirection,
jsonBigIntStringReplacer,
{{/if}}
EventWatcher,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 416c12f

Please sign in to comment.