UNPKG

@neo4j/graphql

Version:

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

89 lines 5.05 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.validatePopulatedByDirective = validatePopulatedByDirective; const graphql_1 = require("graphql"); const directives_1 = require("../../../../graphql/directives"); const scalars_1 = require("../../../../graphql/scalars"); const parse_value_node_1 = require("../../../../schema-model/parser/parse-value-node"); 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 utils_1 = require("../utils/utils"); function validatePopulatedByDirective(context) { const typeMapWithExtensions = context.typeMapWithExtensions; if (!typeMapWithExtensions) { throw new Error("No typeMapWithExtensions found in the context"); } const callbacks = context.callbacks; return { FieldDefinition(fieldDefinitionNode, _key, _parent, path, ancestors) { const appliedPopulatedByDirective = fieldDefinitionNode.directives?.find((directive) => directive.name.value === directives_1.populatedByDirective.name); if (!appliedPopulatedByDirective) { return; } const callbackArg = appliedPopulatedByDirective.arguments?.find((x) => x.name.value === "callback"); if (!callbackArg) { // delegate to DirectiveArgumentOfCorrectType rule 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.populatedByDirective.name}" requires in a type with "@node" or within the "@relationshipProperties" directive`, []); } const callbackName = (0, parse_value_node_1.parseValueNode)(callbackArg.value); if (!callbacks) { throw new document_validation_error_1.DocumentValidationError(`@${directives_1.populatedByDirective.name}.callback needs to be provided in features option.`, ["callback"]); } if (typeof (callbacks || {})[callbackName] !== "function") { throw new document_validation_error_1.DocumentValidationError(`@${directives_1.populatedByDirective.name}.callback \`${callbackName}\` must be of type Function.`, ["callback"]); } if (![ graphql_1.GraphQLInt.name, graphql_1.GraphQLFloat.name, graphql_1.GraphQLString.name, graphql_1.GraphQLBoolean.name, graphql_1.GraphQLID.name, scalars_1.GraphQLBigInt.name, scalars_1.GraphQLDateTime.name, scalars_1.GraphQLDate.name, scalars_1.GraphQLTime.name, scalars_1.GraphQLLocalDateTime.name, scalars_1.GraphQLLocalTime.name, scalars_1.GraphQLDuration.name, ].includes((0, utils_1.getInnerTypeName)(fieldDefinitionNode.type))) { throw new document_validation_error_1.DocumentValidationError("@populatedBy can only be used on fields of type Int, Float, String, Boolean, ID, BigInt, DateTime, Date, Time, LocalDateTime, LocalTime or Duration.", []); } }); 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.populatedByDirective.name}`, ...errorPath], errorMsg, })); } }, }; } //# sourceMappingURL=populated-by.js.map