@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
116 lines • 6.13 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.validateLimitDirective = validateLimitDirective;
const directives_1 = require("../../../../graphql/directives");
const utils_1 = require("../../../../utils/utils");
const document_validation_error_1 = require("../utils/document-validation-error");
const is_node_type_1 = require("../utils/location-helpers/is-node-type");
const utils_2 = require("../utils/utils");
function validateLimitDirective(context) {
const typeMapWithExtensions = context.typeMapWithExtensions;
if (!typeMapWithExtensions) {
throw new Error("No typeMapWithExtensions found in the context");
}
return {
InterfaceTypeDefinition(interfaceTypeDefinitionNode, _key, _parent, _path, _ancestors) {
const { directives } = interfaceTypeDefinitionNode;
const objectTypeExtensionNodes = typeMapWithExtensions[interfaceTypeDefinitionNode.name.value]?.extensions;
const extensionsDirectives = (0, utils_1.asArray)(objectTypeExtensionNodes).flatMap((extensionNode) => {
return extensionNode.directives ?? [];
});
const allDirectives = [...(directives ?? []), ...extensionsDirectives];
const appliedLimitDirective = allDirectives.find((directive) => directive.name.value === directives_1.limitDirective.name);
if (!appliedLimitDirective) {
return;
}
const { isValid, errorMsg, errorPath } = (0, document_validation_error_1.assertValid)(() => {
assertLimitDirectiveIsValid(appliedLimitDirective);
});
if (!isValid) {
context.reportError((0, document_validation_error_1.createGraphQLError)({
nodes: [interfaceTypeDefinitionNode],
path: [interfaceTypeDefinitionNode.name.value, `@${directives_1.limitDirective.name}`, ...errorPath],
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];
const appliedLimitDirective = allDirectives.find((directive) => directive.name.value === directives_1.limitDirective.name);
if (!appliedLimitDirective) {
return;
}
const { isValid, errorMsg, errorPath } = (0, document_validation_error_1.assertValid)(() => {
const isValidLocation = (0, is_node_type_1.typeIsANodeType)({ objectTypeDefinitionNode, typeMapWithExtensions });
if (!isValidLocation) {
throw new document_validation_error_1.DocumentValidationError(`Directive "${directives_1.limitDirective.name}" requires in a type with "@node" or in an interface type`, []);
}
assertLimitDirectiveIsValid(appliedLimitDirective);
});
if (!isValid) {
context.reportError((0, document_validation_error_1.createGraphQLError)({
nodes: [objectTypeDefinitionNode],
path: [objectTypeDefinitionNode.name.value, `@${directives_1.limitDirective.name}`, ...errorPath],
errorMsg,
}));
}
},
};
}
// shared assertion code between limit validation between interface and object types
function assertLimitDirectiveIsValid(appliedLimitDirective) {
const defaultArg = appliedLimitDirective.arguments?.find((a) => a.name.value === "default");
const maxArg = appliedLimitDirective.arguments?.find((a) => a.name.value === "max");
if (!defaultArg && !maxArg) {
// nothing to check, fields are optional
return;
}
const defaultLimit = (0, utils_2.parseArgumentToInt)(defaultArg);
const maxLimit = (0, utils_2.parseArgumentToInt)(maxArg);
if (defaultLimit) {
const defaultValue = defaultLimit.toNumber();
// default must be greater than 0
if (defaultValue <= 0) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.limitDirective.name}.default invalid value: ${defaultValue}. Must be greater than 0.`, ["default"]);
}
}
if (maxLimit) {
const maxValue = maxLimit.toNumber();
// max must be greater than 0
if (maxValue <= 0) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.limitDirective.name}.max invalid value: ${maxValue}. Must be greater than 0.`, ["max"]);
}
}
if (defaultLimit && maxLimit) {
const defaultValue = defaultLimit.toNumber();
const maxValue = maxLimit.toNumber();
// default must be smaller than max
if (maxLimit < defaultLimit) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.limitDirective.name}.max invalid value: ${maxValue}. Must be greater than limit.default: ${defaultValue}.`, ["max"]);
}
}
}
//# sourceMappingURL=limit.js.map