@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
153 lines • 9.89 kB
JavaScript
;
/*
* 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.validateRelationshipDirective = validateRelationshipDirective;
const graphql_1 = require("graphql");
const directives_1 = require("../../../../graphql/directives");
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_interface_type_1 = require("../utils/location-helpers/is-in-interface-type");
const is_in_node_type_1 = require("../utils/location-helpers/is-in-node-type");
const is_node_type_1 = require("../utils/location-helpers/is-node-type");
const path_parser_1 = require("../utils/path-parser");
const utils_1 = require("../utils/utils");
function validateRelationshipDirective(context) {
const typeMapWithExtensions = context.typeMapWithExtensions;
if (!typeMapWithExtensions) {
throw new Error("No typeMapWithExtensions found in the context");
}
return {
// At the object level we need to check that the relationship directive is not applied to multiple fields of the same type
ObjectTypeDefinition(objectTypeDefinitionNode, _key, _parent, _path, _ancestors) {
const fieldTypes = new Map();
const extensionsFields = (typeMapWithExtensions[objectTypeDefinitionNode.name.value]?.extensions ??
[]).flatMap((extension) => extension.fields ?? []);
[...(objectTypeDefinitionNode.fields ?? []), ...extensionsFields].forEach((field) => {
const appliedRelationship = field.directives?.find((directive) => directive.name.value === directives_1.relationshipDirective.name);
if (!appliedRelationship) {
return;
}
const fieldType = (0, utils_1.getInnerTypeName)(field.type);
const relationshipDirectionArgument = appliedRelationship.arguments?.find((a) => a.name.value === "direction");
const relationshipTypeArgument = appliedRelationship.arguments?.find((a) => a.name.value === "type");
if (!relationshipDirectionArgument || !relationshipTypeArgument) {
// delegate to DirectiveArgumentOfCorrectType rule
return;
}
const directionArgAsString = (0, parse_value_node_1.parseValueNode)(relationshipDirectionArgument.value);
const relationshipTypeArgumentAsString = (0, parse_value_node_1.parseValueNode)(relationshipTypeArgument.value);
// create a map key of field type + relationship type + relationship direction to check for uniqueness
const validUniqueRelationshipKey = `${fieldType}-${relationshipTypeArgumentAsString}-${directionArgAsString}`;
if (fieldTypes.has(validUniqueRelationshipKey)) {
context.reportError((0, document_validation_error_1.createGraphQLError)({
nodes: [field],
path: [
objectTypeDefinitionNode.name.value,
field.name.value,
`@${directives_1.relationshipDirective.name}`,
],
errorMsg: `@${directives_1.relationshipDirective.name} invalid. Multiple fields of the same type cannot have a relationship with the same direction and type combination.`,
}));
}
else {
fieldTypes.set(validUniqueRelationshipKey, field.name.value);
}
});
},
FieldDefinition(fieldDefinitionNode, _key, _parent, path, ancestors) {
const appliedRelationship = fieldDefinitionNode.directives?.find((directive) => directive.name.value === directives_1.relationshipDirective.name);
if (!appliedRelationship) {
return;
}
const isValidLocation = (0, is_in_node_type_1.fieldIsInNodeType)({ path, ancestors, typeMapWithExtensions });
const [pathToNode, _traversedDef, parentOfTraversedDef] = (0, path_parser_1.getPathToNode)(path, ancestors);
const typeArg = appliedRelationship.arguments?.find((a) => a.name.value === "type");
const directionArg = appliedRelationship.arguments?.find((a) => a.name.value === "direction");
const propertiesArg = appliedRelationship.arguments?.find((a) => a.name.value === "properties");
if (!typeArg || !directionArg) {
// delegate to DirectiveArgumentOfCorrectType rule
return;
}
const { isValid, errorMsg, errorPath } = (0, document_validation_error_1.assertValid)(() => {
if (!isValidLocation) {
if ((0, is_in_interface_type_1.fieldIsInInterfaceType)({ path, ancestors, typeMapWithExtensions })) {
// throw more specific error for interface types as in the past it was possible to have relationships on interfaces
throw new document_validation_error_1.DocumentValidationError(`Invalid directive usage: Directive @${directives_1.relationshipDirective.name} is not supported on fields of interface types (${parentOfTraversedDef?.name.value}). Since version 5.0.0, interface fields can only have @${directives_1.declareRelationshipDirective.name}. Please add the @relationship directive to the fields in all types which implement it.`, []);
}
throw new document_validation_error_1.DocumentValidationError(`Directive "${directives_1.relationshipDirective.name}" must be in a type with "@node"`, []);
}
if (propertiesArg) {
// find the relationshipProperties type, if type does not exist, throw error
const propertiesArgAsString = (0, parse_value_node_1.parseValueNode)(propertiesArg.value);
const propertiesType = typeMapWithExtensions[propertiesArgAsString]?.definition;
if (!propertiesType) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.relationshipDirective.name}.properties invalid. Cannot find type to represent the relationship properties: ${propertiesArgAsString}.`, ["properties"]);
}
if (!propertiesType.directives?.find((directive) => directive.name.value === directives_1.relationshipPropertiesDirective.name)) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.relationshipDirective.name}.properties invalid. Properties type ${propertiesArgAsString} must use directive \`@${directives_1.relationshipPropertiesDirective.name}\`.`, ["properties"]);
}
}
validateRelationshipTarget(fieldDefinitionNode, context);
});
if (!isValid) {
context.reportError((0, document_validation_error_1.createGraphQLError)({
nodes: [fieldDefinitionNode],
path: [...pathToNode, `@${directives_1.relationshipDirective.name}`, ...errorPath],
errorMsg,
}));
}
},
};
}
function validateRelationshipTarget(fieldDefinitionNode, context) {
const typeMapWithExtensions = context.typeMapWithExtensions;
const interfacesMap = context.interfacesMap;
if (!typeMapWithExtensions || !interfacesMap) {
throw new Error("No typeMapWithExtensions found in the context");
}
const relatedTypename = (0, utils_1.getInnerTypeName)(fieldDefinitionNode.type);
const relatedType = typeMapWithExtensions[relatedTypename]?.definition;
if (!relatedType) {
return;
}
if (relatedType.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION) {
if (!(0, is_node_type_1.typeIsANodeType)({ objectTypeDefinitionNode: relatedType, typeMapWithExtensions })) {
throw new document_validation_error_1.DocumentValidationError(`Invalid directive usage: Directive @${directives_1.relationshipDirective.name} should be a type with "@node".`, []);
}
}
else if (relatedType.kind === graphql_1.Kind.INTERFACE_TYPE_DEFINITION) {
if (!(0, is_node_type_1.interfaceIsNode)({
interfaceTypeDefinitionNode: relatedType,
typeMapWithExtensions,
interfacesMap,
})) {
throw new document_validation_error_1.DocumentValidationError(`Invalid directive usage: Directive @${directives_1.relationshipDirective.name} should be an interface implemented by a type with "@node".`, []);
}
}
else if (relatedType.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
if (!(0, is_node_type_1.unionIsNode)({
unionTypeDefinitionNode: relatedType,
typeMapWithExtensions,
})) {
throw new document_validation_error_1.DocumentValidationError(`Invalid directive usage: Directive @${directives_1.relationshipDirective.name} to an union should have all its types with "@node".`, []);
}
}
}
//# sourceMappingURL=relationship.js.map