UNPKG

@neo4j/graphql

Version:

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

99 lines 5.39 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.validateCoalesceDirective = validateCoalesceDirective; const graphql_1 = require("graphql"); const constants_1 = require("../../../../constants"); 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"); const same_type_argument_as_field_1 = require("../utils/same-type-argument-as-field"); function validateCoalesceDirective(context) { const typeMapWithExtensions = context.typeMapWithExtensions; if (!typeMapWithExtensions) { throw new Error("No typeMapWithExtensions found in the context"); } const enumsTypes = Object.values(typeMapWithExtensions) .map((type) => type.definition) .filter((definition) => definition.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION); return { FieldDefinition(fieldDefinitionNode, _key, _parent, path, ancestors) { const coalesce = fieldDefinitionNode.directives?.find((directive) => directive.name.value === directives_1.coalesceDirective.name); if (!coalesce) { return; } const valueArg = coalesce.arguments?.find((arg) => arg.name.value === "value"); if (!valueArg) { 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, errorPath } = (0, document_validation_error_1.assertValid)(() => { if (!isValidLocation) { throw new document_validation_error_1.DocumentValidationError(`Directive @"${directives_1.coalesceDirective.name}" requires in a type with "@node" or within the "@relationshipProperties" directive`, []); } assertTypeIsSupportedByCoalesce(fieldDefinitionNode.type, typeMapWithExtensions); // for compatibility with previous helper we generate the enumTypes here, but it can be passed the typeMap instead. (0, same_type_argument_as_field_1.assertArgumentHasSameTypeAsField)({ directiveName: directives_1.coalesceDirective.name, traversedDef: fieldDefinitionNode, argument: valueArg, enums: enumsTypes, }); }); if (!isValid) { const pathToNode = (0, path_parser_1.getPathToNode)(path, ancestors); context.reportError((0, document_validation_error_1.createGraphQLError)({ nodes: [fieldDefinitionNode], path: [...pathToNode[0], `@${directives_1.coalesceDirective.name}`, ...errorPath], errorMsg, })); } }, }; } function assertTypeIsSupportedByCoalesce(typeNode, typeMapWithExtensions) { if (typeNode.kind === graphql_1.Kind.LIST_TYPE) { assertTypeIsSupportedByCoalesce(typeNode.type, typeMapWithExtensions); } if (typeNode.kind === graphql_1.Kind.NON_NULL_TYPE) { assertTypeIsSupportedByCoalesce(typeNode.type, typeMapWithExtensions); } if (typeNode.kind === graphql_1.Kind.NAMED_TYPE) { if (constants_1.GRAPHQL_BUILTIN_SCALAR_TYPES.includes(typeNode.name.value)) { return; } if (constants_1.SPATIAL_TYPES.includes(typeNode.name.value)) { throw new document_validation_error_1.DocumentValidationError(`@${directives_1.coalesceDirective.name} is not supported by Spatial types.`, []); } if (constants_1.TEMPORAL_SCALAR_TYPES.includes(typeNode.name.value)) { throw new document_validation_error_1.DocumentValidationError(`@${directives_1.coalesceDirective.name} is not supported by Temporal types.`, []); } // check if the type is an enum const typeFromMap = typeMapWithExtensions[typeNode.name.value]; if (typeFromMap?.definition.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) { return; } throw new document_validation_error_1.DocumentValidationError(`@${directives_1.coalesceDirective.name} directive can only be used on types: Int | Float | String | Boolean | ID | Enum`, []); } } //# sourceMappingURL=coalesce.js.map