UNPKG

@neo4j/graphql

Version:

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

87 lines 5.08 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.validateAuthenticationDirective = validateAuthenticationDirective; const authentication_1 = require("../../../../graphql/directives/type-dependant-directives/authentication"); const is_root_type_1 = require("../../../../utils/is-root-type"); const utils_1 = require("../../../../utils/utils"); 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 is_node_type_1 = require("../utils/location-helpers/is-node-type"); const path_parser_1 = require("../utils/path-parser"); function validateAuthenticationDirective(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 === authentication_1.authenticationDirectiveScaffold.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 "${authentication_1.authenticationDirectiveScaffold.name}" requires in a type with "@node" or in 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], `@${authentication_1.authenticationDirectiveScaffold.name}`], errorMsg, })); } }, ObjectTypeDefinition(objectTypeDefinitionNode, _key, _parent, path, ancestors) { const { directives } = objectTypeDefinitionNode; const objectTypeExtensionNodes = typeMapWithExtensions[objectTypeDefinitionNode.name.value]?.extensions; const extensionsDirectives = (0, utils_1.asArray)(objectTypeExtensionNodes).flatMap((extensionNode) => { return extensionNode.directives ?? []; }); const allDirectives = [...(directives ?? []), ...extensionsDirectives]; if (!allDirectives.find((directive) => directive.name.value === authentication_1.authenticationDirectiveScaffold.name)) { return; } const isValidLocation = ((0, is_node_type_1.typeIsANodeType)({ objectTypeDefinitionNode, typeMapWithExtensions }) || (0, is_root_type_1.isRootType)(objectTypeDefinitionNode)) && objectTypeDefinitionNode.name.value !== "Subscription"; const { isValid, errorMsg } = (0, document_validation_error_1.assertValid)(() => { if (!isValidLocation) { throw new document_validation_error_1.DocumentValidationError(`Directive "${authentication_1.authenticationDirectiveScaffold.name}" requires in a type with "@node" or in 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: [objectTypeDefinitionNode], path: [...pathToNode[0], `@${authentication_1.authenticationDirectiveScaffold.name}`], errorMsg, })); } }, }; } //# sourceMappingURL=authentication.js.map