UNPKG

@neo4j/graphql

Version:

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

135 lines 6.82 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomCypherFactory = void 0; const AttributeAdapter_1 = require("../../../../schema-model/attribute/model-adapters/AttributeAdapter"); const TypenameFilter_1 = require("../../ast/filters/property-filters/TypenameFilter"); const CypherAttributeOperation_1 = require("../../ast/operations/CypherAttributeOperation"); const CypherEntityOperation_1 = require("../../ast/operations/CypherEntityOperation"); const CompositeCypherOperation_1 = require("../../ast/operations/composite/CompositeCypherOperation"); const CompositeReadPartial_1 = require("../../ast/operations/composite/CompositeReadPartial"); const CustomCypherSelection_1 = require("../../ast/selection/CustomCypherSelection"); const NodeSelection_1 = require("../../ast/selection/NodeSelection"); const is_concrete_entity_1 = require("../../utils/is-concrete-entity"); class CustomCypherFactory { constructor(queryASTFactory) { this.queryASTFactory = queryASTFactory; } createCustomCypherOperation({ resolveTree, context, entity, cypherAttributeField, cypherArguments = {}, }) { const selection = new CustomCypherSelection_1.CustomCypherSelection({ operationField: cypherAttributeField, rawArguments: cypherArguments, isNested: true, }); if (!entity) { return new CypherAttributeOperation_1.CypherAttributeOperation(selection, cypherAttributeField, true); } if ((0, is_concrete_entity_1.isConcreteEntity)(entity)) { const customCypher = new CypherEntityOperation_1.CypherEntityOperation({ cypherAttributeField: cypherAttributeField, target: entity, selection, }); if (!resolveTree) { return customCypher; } return this.queryASTFactory.operationsFactory.hydrateReadOperation({ entity, operation: customCypher, resolveTree, context, whereArgs: {}, }); } const CypherReadPartials = entity.concreteEntities.map((concreteEntity) => { const partialSelection = new NodeSelection_1.NodeSelection({ target: concreteEntity, useContextTarget: true }); const partial = new CompositeReadPartial_1.CompositeReadPartial({ target: concreteEntity, selection: partialSelection }); // The Typename filter here is required to access concrete entities from a Cypher Union selection. // It would be probably more ergonomic to pass the label filter with the selection, // although is currently not possible to do so with Cypher.Builder // https://github.com/neo4j/cypher-builder/issues/300 partial.addFilters(new TypenameFilter_1.TypenameFilter([concreteEntity])); if (!resolveTree) { return partial; } return this.queryASTFactory.operationsFactory.hydrateReadOperation({ entity: concreteEntity, operation: partial, resolveTree, context, whereArgs: {}, }); }); return new CompositeCypherOperation_1.CompositeCypherOperation({ selection, partials: CypherReadPartials, cypherAttributeField }); } createTopLevelCustomCypherOperation({ resolveTree, context, entity, }) { const operationAttribute = context.schemaModel.operations.Query?.findAttribute(resolveTree.name) ?? context.schemaModel.operations.Mutation?.findAttribute(resolveTree.name); if (!operationAttribute) { throw new Error(`Failed to collect information about the operation field with name: ${resolveTree.name}`); } const operationField = new AttributeAdapter_1.AttributeAdapter(operationAttribute); const selection = new CustomCypherSelection_1.CustomCypherSelection({ operationField, rawArguments: resolveTree.args, isNested: false, }); if (!entity) { return new CypherAttributeOperation_1.CypherAttributeOperation(selection, operationField, false); } if ((0, is_concrete_entity_1.isConcreteEntity)(entity)) { const customCypher = new CypherEntityOperation_1.CypherEntityOperation({ cypherAttributeField: operationField, target: entity, selection, }); return this.queryASTFactory.operationsFactory.hydrateReadOperation({ entity, operation: customCypher, resolveTree, context, whereArgs: {}, }); } const CypherReadPartials = entity.concreteEntities.map((concreteEntity) => { const partialSelection = new NodeSelection_1.NodeSelection({ target: concreteEntity, useContextTarget: true }); const partial = new CompositeReadPartial_1.CompositeReadPartial({ target: concreteEntity, selection: partialSelection }); // The Typename filter here is required to access concrete entities from a Cypher Union selection. // It would be probably more ergonomic to pass the label filter with the selection, // although is currently not possible to do so with Cypher.Builder // https://github.com/neo4j/cypher-builder/issues/300 partial.addFilters(new TypenameFilter_1.TypenameFilter([concreteEntity])); return this.queryASTFactory.operationsFactory.hydrateReadOperation({ entity: concreteEntity, operation: partial, resolveTree, context, whereArgs: {}, }); }); return new CompositeCypherOperation_1.CompositeCypherOperation({ selection, partials: CypherReadPartials, cypherAttributeField: operationField, }); } } exports.CustomCypherFactory = CustomCypherFactory; //# sourceMappingURL=CustomCypherFactory.js.map