mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-06 19:38:05 +00:00
Handle where clause on nested GQL query selections (#531)
* Handle where clause on nested GQL query selections * Handle variables for arguments on nested selections * Handle args on nested GQL query selections for plural queries * Update package versions
This commit is contained in:
parent
d53fbcf731
commit
05fdf85af8
@ -2,7 +2,7 @@
|
|||||||
"packages": [
|
"packages": [
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"npmClient": "yarn",
|
"npmClient": "yarn",
|
||||||
"useWorkspaces": true,
|
"useWorkspaces": true,
|
||||||
"command": {
|
"command": {
|
||||||
|
2
packages/cache/package.json
vendored
2
packages/cache/package.json
vendored
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/cache",
|
"name": "@cerc-io/cache",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"description": "Generic object cache",
|
"description": "Generic object cache",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/cli",
|
"name": "@cerc-io/cli",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -15,13 +15,13 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.7.1",
|
"@apollo/client": "^3.7.1",
|
||||||
"@cerc-io/cache": "^0.2.105",
|
"@cerc-io/cache": "^0.2.106",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.105",
|
"@cerc-io/ipld-eth-client": "^0.2.106",
|
||||||
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
|
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
|
||||||
"@cerc-io/nitro-node": "^0.1.15",
|
"@cerc-io/nitro-node": "^0.1.15",
|
||||||
"@cerc-io/peer": "^0.2.105",
|
"@cerc-io/peer": "^0.2.106",
|
||||||
"@cerc-io/rpc-eth-client": "^0.2.105",
|
"@cerc-io/rpc-eth-client": "^0.2.106",
|
||||||
"@cerc-io/util": "^0.2.105",
|
"@cerc-io/util": "^0.2.106",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"@graphql-tools/utils": "^9.1.1",
|
"@graphql-tools/utils": "^9.1.1",
|
||||||
"@ipld/dag-cbor": "^8.0.0",
|
"@ipld/dag-cbor": "^8.0.0",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/codegen",
|
"name": "@cerc-io/codegen",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"description": "Code generator",
|
"description": "Code generator",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
@ -20,7 +20,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/util": "^0.2.105",
|
"@cerc-io/util": "^0.2.106",
|
||||||
"@graphql-tools/load-files": "^6.5.2",
|
"@graphql-tools/load-files": "^6.5.2",
|
||||||
"@npmcli/package-json": "^5.0.0",
|
"@npmcli/package-json": "^5.0.0",
|
||||||
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
|
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
|
||||||
|
@ -41,12 +41,12 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/cli": "^0.2.105",
|
"@cerc-io/cli": "^0.2.106",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.105",
|
"@cerc-io/ipld-eth-client": "^0.2.106",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.105",
|
"@cerc-io/solidity-mapper": "^0.2.106",
|
||||||
"@cerc-io/util": "^0.2.105",
|
"@cerc-io/util": "^0.2.106",
|
||||||
{{#if (subgraphPath)}}
|
{{#if (subgraphPath)}}
|
||||||
"@cerc-io/graph-node": "^0.2.105",
|
"@cerc-io/graph-node": "^0.2.106",
|
||||||
{{/if}}
|
{{/if}}
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/graph-node",
|
"name": "@cerc-io/graph-node",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cerc-io/solidity-mapper": "^0.2.105",
|
"@cerc-io/solidity-mapper": "^0.2.106",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
"@graphprotocol/graph-ts": "^0.22.0",
|
"@graphprotocol/graph-ts": "^0.22.0",
|
||||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||||
@ -51,9 +51,9 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.3.19",
|
"@apollo/client": "^3.3.19",
|
||||||
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
|
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
|
||||||
"@cerc-io/cache": "^0.2.105",
|
"@cerc-io/cache": "^0.2.106",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.105",
|
"@cerc-io/ipld-eth-client": "^0.2.106",
|
||||||
"@cerc-io/util": "^0.2.105",
|
"@cerc-io/util": "^0.2.106",
|
||||||
"@types/json-diff": "^0.5.2",
|
"@types/json-diff": "^0.5.2",
|
||||||
"@types/yargs": "^17.0.0",
|
"@types/yargs": "^17.0.0",
|
||||||
"bn.js": "^4.11.9",
|
"bn.js": "^4.11.9",
|
||||||
|
@ -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;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/ipld-eth-client",
|
"name": "@cerc-io/ipld-eth-client",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"description": "IPLD ETH Client",
|
"description": "IPLD ETH Client",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -20,8 +20,8 @@
|
|||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/client": "^3.7.1",
|
"@apollo/client": "^3.7.1",
|
||||||
"@cerc-io/cache": "^0.2.105",
|
"@cerc-io/cache": "^0.2.106",
|
||||||
"@cerc-io/util": "^0.2.105",
|
"@cerc-io/util": "^0.2.106",
|
||||||
"cross-fetch": "^3.1.4",
|
"cross-fetch": "^3.1.4",
|
||||||
"debug": "^4.3.1",
|
"debug": "^4.3.1",
|
||||||
"ethers": "^5.4.4",
|
"ethers": "^5.4.4",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/peer",
|
"name": "@cerc-io/peer",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"description": "libp2p module",
|
"description": "libp2p module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"exports": "./dist/index.js",
|
"exports": "./dist/index.js",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/rpc-eth-client",
|
"name": "@cerc-io/rpc-eth-client",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"description": "RPC ETH Client",
|
"description": "RPC ETH Client",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -19,9 +19,9 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/cache": "^0.2.105",
|
"@cerc-io/cache": "^0.2.106",
|
||||||
"@cerc-io/ipld-eth-client": "^0.2.105",
|
"@cerc-io/ipld-eth-client": "^0.2.106",
|
||||||
"@cerc-io/util": "^0.2.105",
|
"@cerc-io/util": "^0.2.106",
|
||||||
"chai": "^4.3.4",
|
"chai": "^4.3.4",
|
||||||
"ethers": "^5.4.4",
|
"ethers": "^5.4.4",
|
||||||
"left-pad": "^1.3.0",
|
"left-pad": "^1.3.0",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/solidity-mapper",
|
"name": "@cerc-io/solidity-mapper",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/test",
|
"name": "@cerc-io/test",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/tracing-client",
|
"name": "@cerc-io/tracing-client",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"description": "ETH VM tracing client",
|
"description": "ETH VM tracing client",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@cerc-io/util",
|
"name": "@cerc-io/util",
|
||||||
"version": "0.2.105",
|
"version": "0.2.106",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apollo/utils.keyvaluecache": "^1.0.1",
|
"@apollo/utils.keyvaluecache": "^1.0.1",
|
||||||
"@cerc-io/nitro-node": "^0.1.15",
|
"@cerc-io/nitro-node": "^0.1.15",
|
||||||
"@cerc-io/peer": "^0.2.105",
|
"@cerc-io/peer": "^0.2.106",
|
||||||
"@cerc-io/solidity-mapper": "^0.2.105",
|
"@cerc-io/solidity-mapper": "^0.2.106",
|
||||||
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
|
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
|
||||||
"@ethersproject/properties": "^5.7.0",
|
"@ethersproject/properties": "^5.7.0",
|
||||||
"@ethersproject/providers": "^5.4.4",
|
"@ethersproject/providers": "^5.4.4",
|
||||||
@ -54,7 +54,7 @@
|
|||||||
"yargs": "^17.0.1"
|
"yargs": "^17.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cerc-io/cache": "^0.2.105",
|
"@cerc-io/cache": "^0.2.106",
|
||||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||||
"@types/bunyan": "^1.8.8",
|
"@types/bunyan": "^1.8.8",
|
||||||
"@types/express": "^4.17.14",
|
"@types/express": "^4.17.14",
|
||||||
|
@ -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';
|
||||||
@ -280,16 +280,10 @@ export class GraphDatabase {
|
|||||||
// Filter out __typename field in GQL for loading relations.
|
// Filter out __typename field in GQL for loading relations.
|
||||||
childSelections = childSelections.filter(selection => !(selection.kind === 'Field' && selection.name.value === '__typename'));
|
childSelections = childSelections.filter(selection => !(selection.kind === 'Field' && selection.name.value === '__typename'));
|
||||||
|
|
||||||
// Parse arguments on a plural selection field
|
// Parse selection's arguments
|
||||||
let relationWhere: Where = {};
|
let { where: relationWhere, queryOptions: relationQueryOptions } = this._getSelectionFieldArguments(selection, queryInfo);
|
||||||
let relationQueryOptions: QueryOptions = {};
|
|
||||||
if (isDerived || isArray) {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
({ where: relationWhere, queryOptions: relationQueryOptions } = this._getSelectionFieldArguments(selection));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDerived) {
|
if (isDerived) {
|
||||||
// TODO: Merge with relationWhere
|
|
||||||
const where: Where = {
|
const where: Where = {
|
||||||
[foreignKey]: [{
|
[foreignKey]: [{
|
||||||
value: entityData.id,
|
value: entityData.id,
|
||||||
@ -297,13 +291,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
|
||||||
@ -315,7 +315,6 @@ export class GraphDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isArray) {
|
if (isArray) {
|
||||||
// TODO: Merge with relationWhere
|
|
||||||
const where: Where = {
|
const where: Where = {
|
||||||
id: [{
|
id: [{
|
||||||
value: entityData[field],
|
value: entityData[field],
|
||||||
@ -323,13 +322,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
|
||||||
@ -799,6 +804,7 @@ export class GraphDatabase {
|
|||||||
queryInfo: GraphQLResolveInfo
|
queryInfo: GraphQLResolveInfo
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
assert(selection.kind === 'Field');
|
assert(selection.kind === 'Field');
|
||||||
|
|
||||||
const field = selection.name.value;
|
const field = selection.name.value;
|
||||||
const { entity: relationEntity, isArray, isDerived, field: foreignKey } = relations[field];
|
const { entity: relationEntity, isArray, isDerived, field: foreignKey } = relations[field];
|
||||||
let childSelections = selection.selectionSet?.selections || [];
|
let childSelections = selection.selectionSet?.selections || [];
|
||||||
@ -806,6 +812,9 @@ export class GraphDatabase {
|
|||||||
// Filter out __typename field in GQL for loading relations.
|
// Filter out __typename field in GQL for loading relations.
|
||||||
childSelections = childSelections.filter(selection => !(selection.kind === 'Field' && selection.name.value === '__typename'));
|
childSelections = childSelections.filter(selection => !(selection.kind === 'Field' && selection.name.value === '__typename'));
|
||||||
|
|
||||||
|
// Parse selection's arguments
|
||||||
|
let { where: relationWhere, queryOptions: relationQueryOptions } = this._getSelectionFieldArguments(selection, queryInfo);
|
||||||
|
|
||||||
if (isDerived) {
|
if (isDerived) {
|
||||||
const where: Where = {
|
const where: Where = {
|
||||||
[foreignKey]: [{
|
[foreignKey]: [{
|
||||||
@ -814,14 +823,20 @@ 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,
|
||||||
childSelections,
|
childSelections,
|
||||||
queryInfo
|
queryInfo
|
||||||
);
|
);
|
||||||
@ -866,14 +881,20 @@ 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,
|
||||||
childSelections,
|
childSelections,
|
||||||
queryInfo
|
queryInfo
|
||||||
);
|
);
|
||||||
@ -918,14 +939,20 @@ 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,
|
||||||
childSelections,
|
childSelections,
|
||||||
queryInfo
|
queryInfo
|
||||||
);
|
);
|
||||||
@ -1339,6 +1366,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);
|
||||||
@ -1359,32 +1456,39 @@ export class GraphDatabase {
|
|||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
_getSelectionFieldArguments (fieldNode: FieldNode): { where: Where, queryOptions: QueryOptions } {
|
_getSelectionFieldArguments (fieldNode: FieldNode, queryInfo: GraphQLResolveInfo): { 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, queryInfo));
|
||||||
// where = this.buildFilter(arg.value);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'first':
|
case 'first': {
|
||||||
queryOptions.limit = Number((arg.value as IntValueNode).value);
|
const argValue = (arg.value.kind === 'Variable') ? queryInfo.variableValues[arg.value.name.value] : (arg.value as IntValueNode).value;
|
||||||
|
queryOptions.limit = Number(argValue);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 'skip':
|
case 'skip': {
|
||||||
queryOptions.skip = Number((arg.value as IntValueNode).value);
|
const argValue = (arg.value.kind === 'Variable') ? queryInfo.variableValues[arg.value.name.value] : (arg.value as IntValueNode).value;
|
||||||
|
queryOptions.skip = Number(argValue);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 'orderBy':
|
case 'orderBy': {
|
||||||
queryOptions.orderBy = (arg.value as EnumValueNode).value;
|
const argValue = (arg.value.kind === 'Variable') ? queryInfo.variableValues[arg.value.name.value] : (arg.value as EnumValueNode).value;
|
||||||
|
queryOptions.orderBy = String(argValue);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 'orderDirection':
|
case 'orderDirection': {
|
||||||
queryOptions.orderDirection = (arg.value as EnumValueNode).value as OrderDirection;
|
const argValue = (arg.value.kind === 'Variable') ? queryInfo.variableValues[arg.value.name.value] : (arg.value as EnumValueNode).value;
|
||||||
|
queryOptions.orderDirection = argValue as OrderDirection;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error('Unrecognized query argument');
|
throw new Error('Unrecognized query argument');
|
||||||
@ -1395,4 +1499,34 @@ export class GraphDatabase {
|
|||||||
|
|
||||||
return { where, queryOptions };
|
return { where, queryOptions };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_buildWhereFromArgumentNode (arg: ArgumentNode, queryInfo: GraphQLResolveInfo): { [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':
|
||||||
|
acc[fieldNode.name.value] = queryInfo.variableValues[fieldNode.value.name.value];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'ListValue':
|
||||||
|
case 'ObjectValue':
|
||||||
|
throw new Error(`Nested filter type ${fieldNode.value.kind} not supported`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user