@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
127 lines • 5.44 kB
JavaScript
"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.RelationshipDeclarationAdapter = void 0;
const constants_1 = require("../../../constants");
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 RelationshipAdapter_1 = require("./RelationshipAdapter");
const RelationshipDeclarationOperations_1 = require("./RelationshipDeclarationOperations");
class RelationshipDeclarationAdapter {
constructor(relationshipDeclaration, sourceAdapter) {
const { name, args, source, target, isList, nestedOperations, aggregate, isNullable, description, annotations, firstDeclaredInTypeName, } = relationshipDeclaration;
this.name = name;
this.args = args;
if (sourceAdapter) {
this.source = sourceAdapter;
}
else {
this.source = (0, get_entity_adapter_1.getEntityAdapter)(source);
}
this.isList = isList;
this.nestedOperations = new Set(nestedOperations);
this.aggregate = aggregate;
this.isNullable = isNullable;
this.rawEntity = target;
this.description = description;
this.annotations = annotations;
this.relationshipImplementations = relationshipDeclaration.relationshipImplementations.map((r) => new RelationshipAdapter_1.RelationshipAdapter(r));
this.firstDeclaredInTypeName = firstDeclaredInTypeName;
}
get operations() {
if (!this._operations) {
return new RelationshipDeclarationOperations_1.RelationshipDeclarationOperations(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;
}
// 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 nonGeneratedProperties() {
return this.relationshipImplementations.flatMap((impl) => Array.from(impl.attributes.values()).filter((attribute) => attribute.isNonGeneratedField()));
}
get hasNonNullNonGeneratedProperties() {
return this.nonGeneratedProperties.some((property) => property.typeHelper.isRequired());
}
get hasAnyProperties() {
return !!this.relationshipImplementations.find((relationshipImpl) => relationshipImpl.hasAnyProperties);
}
get hasCreateInputFields() {
return !!this.relationshipImplementations.find((impl) => impl.hasCreateInputFields);
}
get hasUpdateInputFields() {
return !!this.relationshipImplementations.find((impl) => impl.hasUpdateInputFields);
}
get hasNonNullCreateInputFields() {
return !!this.relationshipImplementations.find((impl) => impl.hasNonNullCreateInputFields);
}
isReadable() {
return this.annotations.selectable?.onRead !== false;
}
isFilterableByValue() {
return this.annotations.filterable?.byValue !== false;
}
isFilterableByAggregate() {
if (this.source instanceof UnionEntityAdapter_1.UnionEntityAdapter || 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;
}
findRelationshipImplementation(relationshipName) {
return this.relationshipImplementations.find((impl) => impl.name === relationshipName);
}
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;
}
}
exports.RelationshipDeclarationAdapter = RelationshipDeclarationAdapter;
//# sourceMappingURL=RelationshipDeclarationAdapter.js.map