UNPKG

@neo4j/graphql

Version:

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

143 lines 6.46 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.ReadFactory = void 0; const RelationshipAdapter_1 = require("../../../../schema-model/relationship/model-adapters/RelationshipAdapter"); const check_authentication_1 = require("../../../authorization/check-authentication"); const ReadOperation_1 = require("../../ast/operations/ReadOperation"); const CompositeReadOperation_1 = require("../../ast/operations/composite/CompositeReadOperation"); const CompositeReadPartial_1 = require("../../ast/operations/composite/CompositeReadPartial"); const NodeSelection_1 = require("../../ast/selection/NodeSelection"); const RelationshipSelection_1 = require("../../ast/selection/RelationshipSelection"); const get_concrete_entities_1 = require("../../utils/get-concrete-entities"); const get_concrete_where_1 = require("../../utils/get-concrete-where"); const is_concrete_entity_1 = require("../../utils/is-concrete-entity"); class ReadFactory { constructor(queryASTFactory) { this.queryASTFactory = queryASTFactory; } createReadOperation({ entityOrRel, resolveTree, context, varName, reference, }) { const entity = entityOrRel instanceof RelationshipAdapter_1.RelationshipAdapter ? entityOrRel.target : entityOrRel; const relationship = entityOrRel instanceof RelationshipAdapter_1.RelationshipAdapter ? entityOrRel : undefined; const resolveTreeWhere = this.queryASTFactory.operationsFactory.getWhereArgs(resolveTree, reference); if ((0, is_concrete_entity_1.isConcreteEntity)(entity)) { (0, check_authentication_1.checkEntityAuthentication)({ entity: entity.entity, targetOperations: ["READ"], context, }); let selection; if (relationship) { selection = new RelationshipSelection_1.RelationshipSelection({ relationship, }); } else { selection = new NodeSelection_1.NodeSelection({ target: entity, alias: varName, }); } const operation = new ReadOperation_1.ReadOperation({ target: entity, relationship, selection, }); return this.hydrateReadOperation({ operation, entity, resolveTree, context, whereArgs: resolveTreeWhere, }); } else { const concreteEntities = (0, get_concrete_entities_1.getConcreteEntities)(entity, resolveTreeWhere); const concreteReadOperations = concreteEntities.map((concreteEntity) => { // Duplicate from normal read let selection; if (relationship) { selection = new RelationshipSelection_1.RelationshipSelection({ relationship, targetOverride: concreteEntity, }); } else { selection = new NodeSelection_1.NodeSelection({ target: concreteEntity, alias: varName, }); } const readPartial = new CompositeReadPartial_1.CompositeReadPartial({ target: concreteEntity, relationship, selection, }); const whereArgs = (0, get_concrete_where_1.getConcreteWhere)(entity, concreteEntity, resolveTreeWhere); return this.hydrateReadOperation({ operation: readPartial, entity: concreteEntity, resolveTree, context, whereArgs: whereArgs, partialOf: entity, }); }); const compositeReadOp = new CompositeReadOperation_1.CompositeReadOperation({ compositeEntity: entity, children: concreteReadOperations, relationship, }); this.hydrateCompositeReadOperationWithPagination(entity, compositeReadOp, resolveTree, context); return compositeReadOp; } } hydrateReadOperation({ entity, operation, resolveTree, context, whereArgs, partialOf, }) { const sortArg = resolveTree.args.sort; const limitArg = resolveTree.args.limit; const offsetArg = resolveTree.args.offset; const paginationArgs = { limit: limitArg, offset: offsetArg, sort: sortArg }; return this.queryASTFactory.operationsFactory.hydrateOperation({ entity, operation, context, whereArgs, fieldsByTypeName: resolveTree.fieldsByTypeName, paginationArgs, partialOf, }); } hydrateCompositeReadOperationWithPagination(entity, operation, resolveTree, context) { const sortArg = resolveTree.args.sort; const limitArg = resolveTree.args.limit; const offsetArg = resolveTree.args.offset; const options = this.queryASTFactory.operationsFactory.getOptions({ entity, sortArg, limitArg, offsetArg }); if (options) { const sort = this.queryASTFactory.sortAndPaginationFactory.createSortFields(options, entity, context); operation.addSort(...sort); const pagination = this.queryASTFactory.sortAndPaginationFactory.createPagination(options); if (pagination) { operation.addPagination(pagination); } } } } exports.ReadFactory = ReadFactory; //# sourceMappingURL=ReadFactory.js.map