UNPKG

@neo4j/graphql

Version:

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

135 lines 5.55 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.SortAndPaginationFactory = void 0; const constants_1 = require("../../../constants"); const RelationshipAdapter_1 = require("../../../schema-model/relationship/model-adapters/RelationshipAdapter"); const utils_1 = require("../../../utils/utils"); const CypherAttributeOperation_1 = require("../ast/operations/CypherAttributeOperation"); const Pagination_1 = require("../ast/pagination/Pagination"); const CypherPropertySort_1 = require("../ast/sort/CypherPropertySort"); const PropertySort_1 = require("../ast/sort/PropertySort"); const ScoreSort_1 = require("../ast/sort/ScoreSort"); const is_concrete_entity_1 = require("../utils/is-concrete-entity"); const is_relationship_entity_1 = require("../utils/is-relationship-entity"); const is_union_entity_1 = require("../utils/is-union-entity"); class SortAndPaginationFactory { constructor(queryASTFactory) { this.queryASTFactory = queryASTFactory; } createSortFields(options, entity, context) { // SOFT_DEPRECATION: OPTIONS-ARGUMENT return (0, utils_1.asArray)(options.sort).flatMap((s) => { return this.createPropertySort({ optionArg: s, entity, context }); }); } createConnectionSortFields(options, entityOrRel, context) { if ((0, is_relationship_entity_1.isRelationshipEntity)(entityOrRel)) { const nodeSortFields = this.createPropertySort({ optionArg: options.node ?? {}, entity: entityOrRel.target, context, }); const edgeSortFields = this.createPropertySort({ optionArg: options.edge || {}, entity: entityOrRel, context, }); return { edge: edgeSortFields, node: nodeSortFields, }; } const nodeSortFields = this.createPropertySort({ optionArg: options.node ?? {}, entity: entityOrRel, context, }); if (options[constants_1.SCORE_FIELD]) { if (context.vector) { nodeSortFields.push(new ScoreSort_1.ScoreSort({ scoreVariable: context.vector.scoreVariable, direction: options[constants_1.SCORE_FIELD], })); } else if (context.fulltext) { nodeSortFields.push(new ScoreSort_1.ScoreSort({ scoreVariable: context.fulltext.scoreVariable, direction: options[constants_1.SCORE_FIELD], })); } } return { edge: [], node: nodeSortFields, }; } createPagination(args) { if (args.limit || args.offset) { return new Pagination_1.Pagination({ skip: args.offset, limit: args.limit, }); } } createPropertySort({ optionArg, entity, context, }) { if ((0, is_union_entity_1.isUnionEntity)(entity)) { return []; } if (entity instanceof RelationshipAdapter_1.RelationshipAdapter && entity.propertiesTypeName && Object.keys(optionArg).some((k) => (entity.siblings || []).includes(k))) { if (!optionArg[entity.propertiesTypeName]) { return []; } return this.createPropertySort({ optionArg: optionArg[entity.propertiesTypeName], entity, context, }); } return Object.entries(optionArg).map(([fieldName, sortDir]) => { const attribute = entity.findAttribute(fieldName); if (!attribute) { throw new Error(`no filter attribute ${fieldName}`); } if (attribute.annotations.cypher && (0, is_concrete_entity_1.isConcreteEntity)(entity)) { const cypherOperation = this.queryASTFactory.operationsFactory.createCustomCypherOperation({ context, cypherAttributeField: attribute, }); if (!(cypherOperation instanceof CypherAttributeOperation_1.CypherAttributeOperation)) { throw new Error("Transpile error: sorting is supported only for @cypher scalar properties"); } return new CypherPropertySort_1.CypherPropertySort({ direction: sortDir, attribute, cypherOperation, }); } return new PropertySort_1.PropertySort({ direction: sortDir, attribute, }); }); } } exports.SortAndPaginationFactory = SortAndPaginationFactory; //# sourceMappingURL=SortAndPaginationFactory.js.map