Handle where clause on nested GQL query selections

This commit is contained in:
Prathamesh Musale 2024-08-02 10:49:46 +05:30
parent d53fbcf731
commit 70b0ac8076
2 changed files with 116 additions and 83 deletions

View File

@ -28,10 +28,6 @@ import {
Transaction, Transaction,
EthClient, EthClient,
DEFAULT_LIMIT, DEFAULT_LIMIT,
FILTER_CHANGE_BLOCK,
Where,
Filter,
OPERATOR_MAP,
ExtraEventData, ExtraEventData,
EthFullTransaction EthFullTransaction
} from '@cerc-io/util'; } from '@cerc-io/util';
@ -379,7 +375,7 @@ export class GraphWatcher {
const dbTx = await this._database.createTransactionRunner(); const dbTx = await this._database.createTransactionRunner();
try { try {
where = this._buildFilter(where); where = this._database.buildFilter(where);
if (!queryOptions.limit) { if (!queryOptions.limit) {
queryOptions.limit = DEFAULT_LIMIT; queryOptions.limit = DEFAULT_LIMIT;
@ -514,76 +510,6 @@ export class GraphWatcher {
return transaction; return transaction;
} }
_buildFilter (where: { [key: string]: any } = {}): Where {
return Object.entries(where).reduce((acc: Where, [fieldWithSuffix, value]) => {
if (fieldWithSuffix === FILTER_CHANGE_BLOCK) {
assert(value.number_gte && typeof value.number_gte === 'number');
acc[FILTER_CHANGE_BLOCK] = [{
value: value.number_gte,
not: false
}];
return acc;
}
if (['and', 'or'].includes(fieldWithSuffix)) {
assert(Array.isArray(value));
// Parse all the comibations given in the array
acc[fieldWithSuffix] = value.map(w => {
return this._buildFilter(w);
});
return acc;
}
const [field, ...suffix] = fieldWithSuffix.split('_');
if (!acc[field]) {
acc[field] = [];
}
let op = suffix.shift();
// If op is "" (different from undefined), it means it's a nested filter on a relation field
if (op === '') {
(acc[field] as Filter[]).push({
// Parse nested filter value
value: this._buildFilter(value),
not: false,
operator: 'nested'
});
return acc;
}
const filter: Filter = {
value,
not: false,
operator: 'equals'
};
if (op === 'not') {
filter.not = true;
op = suffix.shift();
}
if (op) {
filter.operator = op as keyof typeof OPERATOR_MAP;
}
// If filter field ends with "nocase", use case insensitive version of the operator
if (suffix[suffix.length - 1] === 'nocase') {
filter.operator = `${op}_nocase` as keyof typeof OPERATOR_MAP;
}
(acc[field] as Filter[]).push(filter);
return acc;
}, {});
}
_getSelectionsFromGQLInfo (queryInfo: GraphQLResolveInfo): readonly SelectionNode[] { _getSelectionsFromGQLInfo (queryInfo: GraphQLResolveInfo): readonly SelectionNode[] {
const [fieldNode] = queryInfo.fieldNodes; const [fieldNode] = queryInfo.fieldNodes;
const selectionSet = fieldNode.selectionSet; const selectionSet = fieldNode.selectionSet;

View File

@ -17,11 +17,11 @@ import {
} from 'typeorm'; } from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata'; import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer'; import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer';
import { ArgumentNode, FieldNode, GraphQLResolveInfo, SelectionNode, IntValueNode, EnumValueNode } from 'graphql'; import { ArgumentNode, FieldNode, GraphQLResolveInfo, SelectionNode, IntValueNode, EnumValueNode, ObjectValueNode, ObjectFieldNode } from 'graphql';
import _ from 'lodash'; import _ from 'lodash';
import debug from 'debug'; import debug from 'debug';
import { Database as BaseDatabase, QueryOptions, Where, CanonicalBlockHeight, OrderDirection } from '../database'; import { Database as BaseDatabase, QueryOptions, Where, CanonicalBlockHeight, Filter, OPERATOR_MAP, OrderDirection } from '../database';
import { BlockProgressInterface } from '../types'; import { BlockProgressInterface } from '../types';
import { cachePrunedEntitiesCount, eventProcessingLoadEntityCacheHitCount, eventProcessingLoadEntityCount, eventProcessingLoadEntityDBQueryDuration } from '../metrics'; import { cachePrunedEntitiesCount, eventProcessingLoadEntityCacheHitCount, eventProcessingLoadEntityCount, eventProcessingLoadEntityDBQueryDuration } from '../metrics';
import { ServerConfig } from '../config'; import { ServerConfig } from '../config';
@ -284,7 +284,6 @@ export class GraphDatabase {
let relationWhere: Where = {}; let relationWhere: Where = {};
let relationQueryOptions: QueryOptions = {}; let relationQueryOptions: QueryOptions = {};
if (isDerived || isArray) { if (isDerived || isArray) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ where: relationWhere, queryOptions: relationQueryOptions } = this._getSelectionFieldArguments(selection)); ({ where: relationWhere, queryOptions: relationQueryOptions } = this._getSelectionFieldArguments(selection));
} }
@ -297,13 +296,19 @@ export class GraphDatabase {
operator: 'equals' operator: 'equals'
}] }]
}; };
relationWhere = _.mergeWith(relationWhere, where, (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
// Overwrite the array in the target object with the source array
return srcValue;
}
});
const relatedEntities = await this.getEntities( const relatedEntities = await this.getEntities(
queryRunner, queryRunner,
relationEntity, relationEntity,
relationsMap, relationsMap,
block, block,
where, relationWhere,
relationQueryOptions, relationQueryOptions,
childSelections, childSelections,
queryInfo queryInfo
@ -323,13 +328,19 @@ export class GraphDatabase {
operator: 'in' operator: 'in'
}] }]
}; };
relationWhere = _.mergeWith(relationWhere, where, (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
// Overwrite the array in the target object with the source array
return srcValue;
}
});
const relatedEntities = await this.getEntities( const relatedEntities = await this.getEntities(
queryRunner, queryRunner,
relationEntity, relationEntity,
relationsMap, relationsMap,
block, block,
where, relationWhere,
relationQueryOptions, relationQueryOptions,
childSelections, childSelections,
queryInfo queryInfo
@ -1339,6 +1350,76 @@ export class GraphDatabase {
); );
} }
buildFilter (where: { [key: string]: any } = {}): Where {
return Object.entries(where).reduce((acc: Where, [fieldWithSuffix, value]) => {
if (fieldWithSuffix === FILTER_CHANGE_BLOCK) {
assert(value.number_gte && typeof value.number_gte === 'number');
acc[FILTER_CHANGE_BLOCK] = [{
value: value.number_gte,
not: false
}];
return acc;
}
if (['and', 'or'].includes(fieldWithSuffix)) {
assert(Array.isArray(value));
// Parse all the comibations given in the array
acc[fieldWithSuffix] = value.map(w => {
return this.buildFilter(w);
});
return acc;
}
const [field, ...suffix] = fieldWithSuffix.split('_');
if (!acc[field]) {
acc[field] = [];
}
let op = suffix.shift();
// If op is "" (different from undefined), it means it's a nested filter on a relation field
if (op === '') {
(acc[field] as Filter[]).push({
// Parse nested filter value
value: this.buildFilter(value),
not: false,
operator: 'nested'
});
return acc;
}
const filter: Filter = {
value,
not: false,
operator: 'equals'
};
if (op === 'not') {
filter.not = true;
op = suffix.shift();
}
if (op) {
filter.operator = op as keyof typeof OPERATOR_MAP;
}
// If filter field ends with "nocase", use case insensitive version of the operator
if (suffix[suffix.length - 1] === 'nocase') {
filter.operator = `${op}_nocase` as keyof typeof OPERATOR_MAP;
}
(acc[field] as Filter[]).push(filter);
return acc;
}, {});
}
_measureCachedPrunedEntities (): void { _measureCachedPrunedEntities (): void {
const totalEntities = Array.from(this.cachedEntities.latestPrunedEntities.values()) const totalEntities = Array.from(this.cachedEntities.latestPrunedEntities.values())
.reduce((acc, idEntitiesMap) => acc + idEntitiesMap.size, 0); .reduce((acc, idEntitiesMap) => acc + idEntitiesMap.size, 0);
@ -1360,14 +1441,13 @@ export class GraphDatabase {
} }
_getSelectionFieldArguments (fieldNode: FieldNode): { where: Where, queryOptions: QueryOptions } { _getSelectionFieldArguments (fieldNode: FieldNode): { where: Where, queryOptions: QueryOptions } {
const where: Where = {}; let where: Where = {};
const queryOptions: QueryOptions = {}; const queryOptions: QueryOptions = {};
fieldNode.arguments?.forEach((arg: ArgumentNode) => { fieldNode.arguments?.forEach((arg: ArgumentNode) => {
switch (arg.name.value) { switch (arg.name.value) {
case 'where': case 'where':
// TODO: Parse ArgumentNode to build where filter where = this.buildFilter(this._buildWhereFromArgumentNode(arg));
// where = this.buildFilter(arg.value);
break; break;
case 'first': case 'first':
@ -1395,4 +1475,31 @@ export class GraphDatabase {
return { where, queryOptions }; return { where, queryOptions };
} }
_buildWhereFromArgumentNode (arg: ArgumentNode): { [key: string]: any } {
// TODO: Handle all types of filters on nested fields
return (arg.value as ObjectValueNode).fields.reduce((acc: { [key: string]: any }, fieldNode: ObjectFieldNode) => {
switch (fieldNode.value.kind) {
case 'BooleanValue' :
case 'EnumValue' :
case 'FloatValue' :
case 'IntValue' :
case 'StringValue' :
acc[fieldNode.name.value] = fieldNode.value.value;
break;
case 'NullValue':
acc[fieldNode.name.value] = null;
break;
case 'Variable':
case 'ListValue':
case 'ObjectValue':
throw new Error(`Nested filter type ${fieldNode.value.kind} not supported`);
}
return acc;
}, {});
}
} }