Fix generated watcher GQL query name and add _change_block filter (#471)

* Add lower case method for camel case entity names

* Add BlockChangedFilter required in where filter

* Use lower camel case in resolvers and fix lower case transformation
This commit is contained in:
Nabarun Gogoi 2023-11-15 10:14:14 +05:30 committed by GitHub
parent 7c4f9fb797
commit 8e9d5092fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 17 deletions

View File

@ -12,6 +12,7 @@ import pluralize from 'pluralize';
import { getGqlForSol, getTsForGql } from './utils/type-mappings'; import { getGqlForSol, getTsForGql } from './utils/type-mappings';
import { Param } from './utils/types'; import { Param } from './utils/types';
import { lowerCamelCase } from './utils/helpers';
const TEMPLATE_FILE = './templates/resolvers-template.handlebars'; const TEMPLATE_FILE = './templates/resolvers-template.handlebars';
@ -62,7 +63,7 @@ export class Resolvers {
} }
const entityName = subgraphTypeDef.name.value; const entityName = subgraphTypeDef.name.value;
const queryName = `${entityName.charAt(0).toLowerCase()}${entityName.slice(1)}`; const queryName = lowerCamelCase(entityName);
let pluralQueryName = pluralize(queryName); let pluralQueryName = pluralize(queryName);
pluralQueryName = (pluralQueryName === queryName) ? `${pluralQueryName}s` : pluralQueryName; pluralQueryName = (pluralQueryName === queryName) ? `${pluralQueryName}s` : pluralQueryName;

View File

@ -12,10 +12,11 @@ import pluralize from 'pluralize';
import { getGqlForSol } from './utils/type-mappings'; import { getGqlForSol } from './utils/type-mappings';
import { Param } from './utils/types'; import { Param } from './utils/types';
import { getBaseType, isArrayType } from './utils/helpers'; import { getBaseType, isArrayType, lowerCamelCase } from './utils/helpers';
const OrderDirection = 'OrderDirection'; const ORDER_DIRECTION = 'OrderDirection';
const BlockHeight = 'Block_height'; const BLOCK_HEIGHT = 'Block_height';
const BLOCK_CHANGED_FILTER = 'BlockChangedFilter';
export class Schema { export class Schema {
_composer: SchemaComposer; _composer: SchemaComposer;
@ -154,25 +155,34 @@ export class Schema {
this._composer.addTypeDefs(subgraphTypeDefsString); this._composer.addTypeDefs(subgraphTypeDefsString);
// Create the Block_height input needed in subgraph queries. // Create the Block_height input needed in subgraph queries.
let typeComposer: any = this._composer.createInputTC({ let inputTypeComposer = this._composer.createInputTC({
name: BlockHeight, name: BLOCK_HEIGHT,
fields: { fields: {
hash: 'Bytes', hash: 'Bytes',
number: 'Int' number: 'Int'
} }
}); });
this._composer.addSchemaMustHaveType(typeComposer); this._composer.addSchemaMustHaveType(inputTypeComposer);
// Add the OrderDirection enum needed in subgraph plural queries. // Add the OrderDirection enum needed in subgraph plural queries.
const orderDirectionEnum = new GraphQLEnumType({ const orderDirectionEnum = new GraphQLEnumType({
name: OrderDirection, name: ORDER_DIRECTION,
values: { values: {
asc: {}, asc: {},
desc: {} desc: {}
} }
}); });
typeComposer = this._composer.createEnumTC(orderDirectionEnum); const enumTypeComposer = this._composer.createEnumTC(orderDirectionEnum);
this._composer.addSchemaMustHaveType(typeComposer); this._composer.addSchemaMustHaveType(enumTypeComposer);
// Add the BlockChangedFilter input needed in subgraph queries.
inputTypeComposer = this._composer.createInputTC({
name: BLOCK_CHANGED_FILTER,
fields: {
number_gte: 'Int!'
}
});
this._composer.addSchemaMustHaveType(inputTypeComposer);
// Add subgraph-schema entity queries to the schema composer. // Add subgraph-schema entity queries to the schema composer.
this._addSubgraphSchemaQueries(subgraphTypeDefs); this._addSubgraphSchemaQueries(subgraphTypeDefs);
@ -188,7 +198,7 @@ export class Schema {
const subgraphType = subgraphTypeDef.name.value; const subgraphType = subgraphTypeDef.name.value;
// Lowercase first letter for query name. // Lowercase first letter for query name.
const queryName = `${subgraphType.charAt(0).toLowerCase()}${subgraphType.slice(1)}`; const queryName = lowerCamelCase(subgraphType);
const queryObject: { [key: string]: any; } = {}; const queryObject: { [key: string]: any; } = {};
queryObject[queryName] = { queryObject[queryName] = {
@ -196,7 +206,7 @@ export class Schema {
type: this._composer.getAnyTC(subgraphType), type: this._composer.getAnyTC(subgraphType),
args: { args: {
id: 'ID!', id: 'ID!',
block: BlockHeight block: BLOCK_HEIGHT
} }
}; };
@ -212,6 +222,15 @@ export class Schema {
}); });
this._composer.addSchemaMustHaveType(subgraphTypeOrderByEnum); this._composer.addSchemaMustHaveType(subgraphTypeOrderByEnum);
// Create the subgraphType_filter input type
const subgraphTypeFilterComposer = this._composer.createInputTC({
name: `${subgraphType}_filter`,
// TODO: Add fields to filter input based on entity properties
fields: {}
});
subgraphTypeFilterComposer.setField('_change_block', BLOCK_CHANGED_FILTER);
this._composer.addSchemaMustHaveType(subgraphTypeFilterComposer);
// Create plural query name // Create plural query name
// Append suffix 's' if pluralized name is the same as singular name (eg. PoolDayData) // Append suffix 's' if pluralized name is the same as singular name (eg. PoolDayData)
let pluralQueryName = pluralize(queryName); let pluralQueryName = pluralize(queryName);
@ -221,11 +240,10 @@ export class Schema {
// Get type composer object for return type from the schema composer. // Get type composer object for return type from the schema composer.
type: this._composer.getAnyTC(subgraphType).NonNull.List.NonNull, type: this._composer.getAnyTC(subgraphType).NonNull.List.NonNull,
args: { args: {
block: BlockHeight, block: BLOCK_HEIGHT,
// TODO: Create input type for where clause where: `${subgraphType}_filter`,
// where: subgraphType_filter,
orderBy: subgraphTypeOrderByEnum, orderBy: subgraphTypeOrderByEnum,
orderDirection: OrderDirection, orderDirection: ORDER_DIRECTION,
first: { type: GraphQLInt, defaultValue: 100 }, first: { type: GraphQLInt, defaultValue: 100 },
skip: { type: GraphQLInt, defaultValue: 0 } skip: { type: GraphQLInt, defaultValue: 0 }
} }
@ -487,7 +505,7 @@ export class Schema {
_meta: { _meta: {
type: this._composer.getOTC('_Meta_'), type: this._composer.getOTC('_Meta_'),
args: { args: {
block: BlockHeight block: BLOCK_HEIGHT
} }
} }
}); });

View File

@ -58,3 +58,13 @@ export function filterInheritedContractNodes (ast: SourceUnit, contractNodes: AS
return resultSet; return resultSet;
} }
/**
* Convert initial uppercase letters of camel case to lowercase
* @param value
*/
export function lowerCamelCase (value: string): string {
const lowerCaseIndex = value.split('').findIndex(char => char.toLowerCase() === char);
return `${value.slice(0, lowerCaseIndex).toLowerCase()}${value.slice(lowerCaseIndex)}`;
}