UNPKG

@neo4j/graphql

Version:

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

58 lines 2.98 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.validateCypherDirective = validateCypherDirective; 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_root_type_1 = require("../utils/location-helpers/is-in-root-type"); const is_in_subscription_type_1 = require("../utils/location-helpers/is-in-subscription-type"); const path_parser_1 = require("../utils/path-parser"); function validateCypherDirective(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?.length || !fieldDefinitionNode.directives.find((directive) => directive.name.value === directives_1.cypherDirective.name)) { return; } const isValidLocation = ((0, is_in_node_type_1.fieldIsInNodeType)({ path, ancestors, typeMapWithExtensions }) || (0, is_in_root_type_1.fieldIsInRootType)({ path, ancestors, typeMapWithExtensions })) && !(0, is_in_subscription_type_1.fieldIsInSubscriptionType)({ path, ancestors, typeMapWithExtensions }); const { isValid, errorMsg } = (0, document_validation_error_1.assertValid)(() => { if (!isValidLocation) { throw new document_validation_error_1.DocumentValidationError(`Directive "${directives_1.cypherDirective.name}" requires in a type with "@node" or on root types: Query, and Mutation`, []); } }); 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.cypherDirective.name}`], errorMsg, })); } }, }; } //# sourceMappingURL=cypher.js.map