UNPKG

@neo4j/graphql

Version:

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

94 lines 4.6 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.CompositeAggregationPartial = 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 create_node_from_entity_1 = require("../../../utils/create-node-from-entity"); const wrap_subquery_in_calls_1 = require("../../../utils/wrap-subquery-in-calls"); const QueryASTNode_1 = require("../../QueryASTNode"); class CompositeAggregationPartial extends QueryASTNode_1.QueryASTNode { constructor({ target, entity, directed = true, attachedTo, }) { super(); this.authFilters = []; this.entity = entity; this.target = target; this.directed = directed; this.attachedTo = attachedTo ?? "node"; } getChildren() { return this.authFilters; } addAuthFilters(...filter) { this.authFilters.push(...filter); } getSubqueries(context) { if (!context.target) { throw new Error("No parent node found!"); } let pattern; const targetNode = new cypher_builder_1.default.Node(); const targetNodeLabels = (0, create_node_from_entity_1.getEntityLabels)(this.target, context.neo4jGraphQLContext); let target = targetNode; const authFilterPredicates = this.getAuthFilterPredicate(context); const authFilterValidations = this.getValidations(context); if (this.entity instanceof RelationshipAdapter_1.RelationshipAdapter) { const relVar = new cypher_builder_1.default.Relationship(); const relDirection = this.entity.getCypherDirection(); if (this.attachedTo === "relationship") { target = relVar; } pattern = new cypher_builder_1.default.Pattern(context.target) .related(relVar, { direction: relDirection, type: this.entity.type }) .to(targetNode, { labels: targetNodeLabels }); const matchClause = new cypher_builder_1.default.Match(pattern).where(cypher_builder_1.default.and(...authFilterPredicates)); const nestedSubqueries = (0, wrap_subquery_in_calls_1.wrapSubqueriesInCypherCalls)(context, this.getChildren(), [target]); return [ cypher_builder_1.default.utils.concat(matchClause, ...authFilterValidations, ...nestedSubqueries, new cypher_builder_1.default.Return([targetNode, "node"], [relVar, "edge"])), ]; } else { pattern = new cypher_builder_1.default.Pattern(targetNode, { labels: targetNodeLabels }); const matchClause = new cypher_builder_1.default.Match(pattern).where(cypher_builder_1.default.and(...authFilterPredicates)); const nestedSubqueries = (0, wrap_subquery_in_calls_1.wrapSubqueriesInCypherCalls)(context, this.getChildren(), [target]); return [ cypher_builder_1.default.utils.concat(matchClause, ...authFilterValidations, ...nestedSubqueries, new cypher_builder_1.default.Return([targetNode, "node"])), ]; } } print() { return `${super.print()} <${this.target.name}>`; } setAttachedTo(attachedTo) { this.attachedTo = attachedTo; } getAuthFilterPredicate(context) { return (0, utils_1.filterTruthy)(this.authFilters.map((f) => f.getPredicate(context))); } getValidations(context) { return (0, utils_1.filterTruthy)(this.authFilters.map((f) => f.getValidation(context))); } } exports.CompositeAggregationPartial = CompositeAggregationPartial; //# sourceMappingURL=CompositeAggregationPartial.js.map