UNPKG

@neo4j/graphql

Version:

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

207 lines 8.63 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RelationshipAdapter = void 0; const typescript_memoize_1 = require("typescript-memoize"); const constants_1 = require("../../../constants"); const AttributeAdapter_1 = require("../../attribute/model-adapters/AttributeAdapter"); const UnionEntityAdapter_1 = require("../../entity/model-adapters/UnionEntityAdapter"); const get_entity_adapter_1 = require("../../utils/get-entity-adapter"); const string_manipulation_1 = require("../../utils/string-manipulation"); const RelationshipOperations_1 = require("./RelationshipOperations"); class RelationshipAdapter { constructor(relationship, sourceAdapter) { this.attributes = new Map(); const { name, type, args, attributes = new Map(), source, target, direction, isList, queryDirection, nestedOperations, aggregate, isNullable, description, annotations, propertiesTypeName, firstDeclaredInTypeName, originalTarget, } = relationship; this.name = name; this.type = type; this.args = args; if (sourceAdapter) { this.source = sourceAdapter; } else { this.source = (0, get_entity_adapter_1.getEntityAdapter)(source); } this.direction = direction; this.isList = isList; this.queryDirection = queryDirection; this.nestedOperations = new Set(nestedOperations); this.aggregate = aggregate; this.isNullable = isNullable; this.rawEntity = target; this.initAttributes(attributes); this.description = description; this.annotations = annotations; this.propertiesTypeName = propertiesTypeName; this.firstDeclaredInTypeName = firstDeclaredInTypeName; this.rawOriginalTargetEntity = originalTarget; if (relationship.getSiblings()) { this.siblings = relationship.getSiblings(); } } get operations() { if (!this._operations) { return new RelationshipOperations_1.RelationshipOperations(this); } return this._operations; } get singular() { if (!this._singular) { this._singular = (0, string_manipulation_1.singular)(this.name); } return this._singular; } get plural() { if (!this._plural) { this._plural = (0, string_manipulation_1.plural)(this.name); } return this._plural; } initAttributes(attributes) { for (const [attributeName, attribute] of attributes.entries()) { const attributeAdapter = new AttributeAdapter_1.AttributeAdapter(attribute); this.attributes.set(attributeName, attributeAdapter); } } findAttribute(name) { return this.attributes.get(name); } /** * @returns the direction to use in the CypherBuilder **/ getCypherDirection() { if (this.queryDirection === "UNDIRECTED") { return "undirected"; } return this.cypherDirectionFromRelDirection(); } cypherDirectionFromRelDirection() { return this.direction === "IN" ? "left" : "right"; } // construct the target entity only when requested get target() { if (!this._target) { this._target = (0, get_entity_adapter_1.getEntityAdapter)(this.rawEntity); } return this._target; } get originalTarget() { if (!this.rawOriginalTargetEntity) { return; } return (0, get_entity_adapter_1.getEntityAdapter)(this.rawOriginalTargetEntity); } isReadable() { return this.annotations.selectable?.onRead !== false; } isFilterableByValue() { return this.annotations.filterable?.byValue !== false; } // this.aggregate refers to the @relationship.aggregate argument and does not have effect on the aggregation filters isFilterableByAggregate() { if (this.target instanceof UnionEntityAdapter_1.UnionEntityAdapter) { return false; } return this.annotations.filterable?.byAggregate !== false; } isCreatable() { return this.annotations.settable?.onCreate !== false; } isUpdatable() { return this.annotations.settable?.onUpdate !== false; } shouldGenerateFieldInputType() { return (this.nestedOperations.has(constants_1.RelationshipNestedOperationsOption.CONNECT) || this.nestedOperations.has(constants_1.RelationshipNestedOperationsOption.CREATE)); } shouldGenerateUpdateFieldInputType(ifUnionRelationshipTargetEntity) { if (this.target instanceof UnionEntityAdapter_1.UnionEntityAdapter) { if (!ifUnionRelationshipTargetEntity) { throw new Error("Expected member entity"); } } return this.nestedOperations.size > 0; } get hasNonNullCreateInputFields() { return this.createInputFields.some((property) => property.typeHelper.isRequired()); } get hasCreateInputFields() { return this.createInputFields.length > 0; } get hasUpdateInputFields() { return this.updateInputFields.length > 0; } get hasAnyProperties() { return this.propertiesTypeName !== undefined; } /** * Categories * = a grouping of attributes * used to generate different types for the Entity that contains these Attributes */ get aggregableFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isAggregableField()); } get aggregationWhereFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isAggregationWhereField()); } get createInputFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isCreateInputField()); } get updateInputFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isUpdateInputField()); } get sortableFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isSortableField()); } get whereFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isWhereField()); } get temporalFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.typeHelper.isTemporal()); } getPopulatedByFields(operation) { switch (operation) { case "CREATE": return Array.from(this.attributes.values()).filter((attribute) => attribute.populatedByCreateIsGenerated()); case "UPDATE": return Array.from(this.attributes.values()).filter((attribute) => attribute.populatedByUpdateIsGenerated()); default: throw new Error("Invalid operation"); } } get subscriptionConnectedRelationshipFields() { return Array.from(this.attributes.values()).filter((attribute) => attribute.isSubscriptionConnectedRelationshipField()); } } exports.RelationshipAdapter = RelationshipAdapter; __decorate([ (0, typescript_memoize_1.Memoize)() ], RelationshipAdapter.prototype, "target", null); __decorate([ (0, typescript_memoize_1.Memoize)() ], RelationshipAdapter.prototype, "originalTarget", null); //# sourceMappingURL=RelationshipAdapter.js.map