UNPKG

@neo4j/graphql

Version:

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

68 lines 3.41 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.validateIdDirective = validateIdDirective; const graphql_1 = require("graphql"); const directives_1 = require("../../../../graphql/directives"); const document_validation_error_1 = require("../utils/document-validation-error"); const is_in_node_type_1 = require("../utils/location-helpers/is-in-node-type"); const is_in_relationship_properties_type_1 = require("../utils/location-helpers/is-in-relationship-properties-type"); const path_parser_1 = require("../utils/path-parser"); function validateIdDirective(context) { const typeMapWithExtensions = context.typeMapWithExtensions; if (!typeMapWithExtensions) { throw new Error("No typeMapWithExtensions found in the context"); } return { FieldDefinition(fieldDefinitionNode, _key, _parent, path, ancestors) { if (!fieldDefinitionNode.directives?.find((directive) => directive.name.value === directives_1.idDirective.name)) { return; } const isValidLocation = (0, is_in_node_type_1.fieldIsInNodeType)({ path, ancestors, typeMapWithExtensions }) || (0, is_in_relationship_properties_type_1.fieldIsInRelationshipPropertiesType)({ path, ancestors, typeMapWithExtensions }); const { isValid, errorMsg } = (0, document_validation_error_1.assertValid)(() => { if (!isValidLocation) { throw new document_validation_error_1.DocumentValidationError(`Directive "${directives_1.idDirective.name}" requires in a type with "@node" or within the "@${directives_1.relationshipPropertiesDirective.name}" directive`, []); } assertTypeIsSupportedByID(fieldDefinitionNode.type); }); const pathToNode = (0, path_parser_1.getPathToNode)(path, ancestors); if (!isValid) { context.reportError((0, document_validation_error_1.createGraphQLError)({ nodes: [fieldDefinitionNode], path: [...pathToNode[0], `@${directives_1.idDirective.name}`], errorMsg, })); } }, }; } function assertTypeIsSupportedByID(type) { if (type.kind === graphql_1.Kind.LIST_TYPE) { throw new document_validation_error_1.DocumentValidationError("Cannot autogenerate an array.", ["@id"]); } if (type.kind === graphql_1.Kind.NON_NULL_TYPE) { return assertTypeIsSupportedByID(type.type); } if (graphql_1.GraphQLID.name !== type.name.value) { throw new document_validation_error_1.DocumentValidationError("Cannot autogenerate a non ID field.", ["@id"]); } } //# sourceMappingURL=id.js.map