@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
102 lines • 5.54 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.validateFulltextDirective = validateFulltextDirective;
const graphql_1 = require("graphql");
const directives_1 = require("../../../../graphql/directives");
const parse_value_node_1 = require("../../../../schema-model/parser/parse-value-node");
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");
function validateFulltextDirective(context) {
const typeMapWithExtensions = context.typeMapWithExtensions;
if (!typeMapWithExtensions) {
throw new Error("No typeMapWithExtensions found in the context");
}
return {
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 appliedFulltextDirective = allDirectives.find((directive) => directive.name.value === directives_1.fulltextDirective.name);
if (!appliedFulltextDirective) {
return;
}
const indexesArg = appliedFulltextDirective.arguments?.find((a) => a.name.value === "indexes");
if (!indexesArg) {
// delegate to DirectiveArgumentOfCorrectType rule
return;
}
const compatibleFields = getFulltextCompatibleFields(objectTypeDefinitionNode);
const isValidLocation = (0, is_node_type_1.typeIsANodeType)({ objectTypeDefinitionNode, typeMapWithExtensions });
const { isValid, errorMsg, errorPath } = (0, document_validation_error_1.assertValid)(() => {
if (!isValidLocation) {
throw new document_validation_error_1.DocumentValidationError(`Directive "${directives_1.fulltextDirective.name}" requires in a type with "@node"`, []);
}
const indexesValues = (0, parse_value_node_1.parseValueNode)(indexesArg.value);
indexesValues.forEach((indexValue) => {
const indexName = indexValue.indexName;
const indexNames = indexesValues.filter((i) => indexName === i.indexName);
if (indexNames.length > 1) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.fulltextDirective.name}.indexes invalid value for: ${indexName}. Duplicate index name.`, ["indexes"]);
}
const queryName = indexValue.queryName;
const queryNames = indexesValues.filter((i) => queryName === i.queryName);
if (queryNames.length > 1) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.fulltextDirective.name}.indexes invalid value for: ${queryName}. Duplicate query name.`, ["indexes"]);
}
(0, utils_1.asArray)(indexValue.fields).forEach((indexField) => {
if (!compatibleFields[indexField]) {
throw new document_validation_error_1.DocumentValidationError(`@${directives_1.fulltextDirective.name}.indexes invalid value for: ${indexValue.indexName}. Field ${indexField} is not of type String or ID.`, ["indexes"]);
}
});
});
});
if (!isValid) {
context.reportError((0, document_validation_error_1.createGraphQLError)({
nodes: [objectTypeDefinitionNode],
path: [objectTypeDefinitionNode.name.value, `@${directives_1.fulltextDirective.name}`, ...errorPath],
errorMsg,
}));
}
},
};
}
function getFulltextCompatibleFields(objectTypeDefinitionNode) {
return (objectTypeDefinitionNode.fields ?? []).reduce((acc, f) => {
if (isFieldFullTextCompatible(f.type)) {
acc[f.name.value] = f;
}
return acc;
}, {});
}
function isFieldFullTextCompatible(fieldType) {
if (fieldType.kind === graphql_1.Kind.NAMED_TYPE) {
return [graphql_1.GraphQLString.name, graphql_1.GraphQLID.name].includes(fieldType.name.value);
}
if (fieldType.kind === graphql_1.Kind.NON_NULL_TYPE) {
return isFieldFullTextCompatible(fieldType.type);
}
return false;
}
//# sourceMappingURL=fulltext.js.map