mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-08 12:28:05 +00:00
Generate resolvers for plural subgraph GQL queries (#461)
This commit is contained in:
parent
6860f1a26f
commit
c7bcd4c276
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user