UNPKG

@neo4j/graphql

Version:

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations

187 lines 8.19 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AggregationOperation = void 0; const cypher_builder_1 = __importDefault(require("@neo4j/cypher-builder")); const RelationshipAdapter_1 = require("../../../../schema-model/relationship/model-adapters/RelationshipAdapter"); const utils_1 = require("../../../../utils/utils"); const wrap_subquery_in_calls_1 = require("../../utils/wrap-subquery-in-calls"); const QueryASTContext_1 = require("../QueryASTContext"); const CountField_1 = require("../fields/aggregation-fields/CountField"); const operations_1 = require("./operations"); // TODO: somewhat dupe of readOperation class AggregationOperation extends operations_1.Operation { constructor({ entity, directed = true, selection, }) { super(); this.fields = []; // Aggregation fields this.nodeFields = []; // Aggregation node fields this.edgeFields = []; // Aggregation node fields this.authFilters = []; this.aggregationProjectionMap = new cypher_builder_1.default.Map(); this.filters = []; this.entity = entity; this.directed = directed; this.selection = selection; } setFields(fields) { this.fields = fields; } addFilters(...filters) { this.filters.push(...filters); } addAuthFilters(...filter) { this.authFilters.push(...filter); } getChildren() { return (0, utils_1.filterTruthy)([ ...this.fields, ...this.nodeFields, ...this.edgeFields, ...this.filters, ...this.authFilters, this.selection, ]); } setNodeFields(fields) { this.nodeFields = fields; } setEdgeFields(fields) { this.edgeFields = fields; } transpile(context) { if (!context.hasTarget()) { throw new Error("No parent node found!"); } const clauses = this.transpileAggregation(context); return { clauses, projectionExpr: this.aggregationProjectionMap, }; } getAuthFilterPredicate(context) { return (0, utils_1.filterTruthy)(this.authFilters.map((f) => f.getPredicate(context))); } getFieldProjectionClause(target, returnVariable, field) { return field.getAggregationProjection(target, returnVariable); } getPattern(context) { if (!context.target) { throw new Error("Not Target"); } if (context.relationship) { if (!context.direction || !context.source) { throw new Error("No valid relationship in aggregation pattern"); } return new cypher_builder_1.default.Pattern(context.source) .related(context.relationship, { direction: context.direction }) .to(context.target); } else { return new cypher_builder_1.default.Pattern(context.target); } } createContext(parentContext) { if (this.entity instanceof RelationshipAdapter_1.RelationshipAdapter) { const relVar = new cypher_builder_1.default.Relationship(); const targetNode = new cypher_builder_1.default.Node(); const relDirection = this.entity.getCypherDirection(); return parentContext.push({ relationship: relVar, target: targetNode, direction: relDirection }); } else { const targetNode = new cypher_builder_1.default.Node(); return new QueryASTContext_1.QueryASTContext({ target: targetNode, neo4jGraphQLContext: parentContext.neo4jGraphQLContext, }); } } transpileAggregation(context) { const operationContext = this.createContext(context); const pattern = this.getPattern(operationContext); const nodeMap = new cypher_builder_1.default.Map(); const fieldSubqueries = this.fields.map((f) => { const returnVariable = new cypher_builder_1.default.Variable(); this.aggregationProjectionMap.set(f.getProjectionField(returnVariable)); return this.createSubquery(f, pattern, returnVariable, context); }); const nodeFieldSubqueries = this.nodeFields.map((f) => { const returnVariable = new cypher_builder_1.default.Variable(); nodeMap.set(f.getProjectionField(returnVariable)); return this.createSubquery(f, pattern, returnVariable, context); }); if (nodeMap.size > 0) { this.aggregationProjectionMap.set("node", nodeMap); } let edgeFieldSubqueries = []; if (operationContext.relationship) { const edgeMap = new cypher_builder_1.default.Map(); edgeFieldSubqueries = this.edgeFields.map((f) => { const returnVariable = new cypher_builder_1.default.Variable(); edgeMap.set(f.getProjectionField(returnVariable)); return this.createSubquery(f, pattern, returnVariable, context, "edge"); }); if (edgeMap.size > 0) { this.aggregationProjectionMap.set("edge", edgeMap); } } return [...fieldSubqueries, ...nodeFieldSubqueries, ...edgeFieldSubqueries]; } getPredicates(queryASTContext) { const authPredicates = this.getAuthFilterPredicate(queryASTContext); return cypher_builder_1.default.and(...this.filters.map((f) => f.getPredicate(queryASTContext)), ...authPredicates); } getValidations(queryASTContext) { return (0, utils_1.filterTruthy)(this.authFilters.flatMap((f) => f.getValidation(queryASTContext))); } createSubquery(field, pattern, returnVariable, context, target = "node") { const { selection: matchClause, nestedContext } = this.selection.apply(context); let extraSelectionWith = undefined; const nestedSubqueries = (0, wrap_subquery_in_calls_1.wrapSubqueriesInCypherCalls)(nestedContext, this.getChildren(), [nestedContext.target]); const targetVar = target === "edge" ? nestedContext.relationship : nestedContext.target; if (!targetVar) throw new Error("Edge not define in aggregations"); const filterPredicates = this.getPredicates(nestedContext); const validations = this.getValidations(nestedContext); const selectionClauses = this.getChildren().flatMap((c) => { return c.getSelection(nestedContext); }); if (selectionClauses.length > 0 || nestedSubqueries.length > 0) { extraSelectionWith = new cypher_builder_1.default.With("*"); } if (filterPredicates) { if (extraSelectionWith) { extraSelectionWith.where(filterPredicates); } else { matchClause.where(filterPredicates); } } if (field instanceof CountField_1.CountField) { field.edgeVar = nestedContext.relationship; } const ret = this.getFieldProjectionClause(targetVar, returnVariable, field); return cypher_builder_1.default.utils.concat(matchClause, ...validations, ...selectionClauses, ...nestedSubqueries, extraSelectionWith, ret); } } exports.AggregationOperation = AggregationOperation; //# sourceMappingURL=AggregationOperation.js.map