@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
98 lines • 3.9 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VALIDATE_OBJECT_FIELD_WARN_MSG = void 0;
exports.WarnObjectFieldsWithoutResolver = WarnObjectFieldsWithoutResolver;
const debug_1 = __importDefault(require("debug"));
const graphql_1 = require("graphql");
const constants_1 = require("../../../../constants");
const utils_1 = require("../../../../utils/utils");
const get_directive_names_1 = require("../utils/get-directive-names");
const utils_2 = require("../utils/utils");
const debug = (0, debug_1.default)(constants_1.DEBUG_GRAPHQL);
exports.VALIDATE_OBJECT_FIELD_WARN_MSG = "Object types need a way to be resolved for field: ";
function WarnObjectFieldsWithoutResolver({ customResolvers }) {
return (context) => {
return {
ObjectTypeDefinition(objectType) {
const directiveNames = (0, get_directive_names_1.getDirectiveNames)(objectType);
const ignoreObjectsWithDirective = (0, utils_1.haveSharedElement)(directiveNames, ["jwt"]);
if (ignoreObjectsWithDirective) {
return;
}
for (const fieldNode of objectType.fields ?? []) {
const innerType = (0, utils_2.getInnerTypeName)(fieldNode.type);
if (typeNeedsResolver(innerType, context.getDocument())) {
const hasResolvableDirective = fieldHasResolverDirective(fieldNode);
const hasCustomResolver = fieldHasCustomResolver({
customResolvers,
fieldNode,
objectType,
});
if (!hasResolvableDirective && !hasCustomResolver) {
const fieldName = fieldNode.name.value;
debug(`${exports.VALIDATE_OBJECT_FIELD_WARN_MSG} ${fieldName}`);
}
}
}
},
};
};
}
function fieldHasCustomResolver({ customResolvers, fieldNode, objectType, }) {
const fieldName = fieldNode.name.value;
const typeName = objectType.name.value;
for (const resolver of customResolvers) {
if (resolver[typeName]?.[fieldName]) {
return true;
}
}
return false;
}
function fieldHasResolverDirective(fieldNode) {
const directiveNames = (0, get_directive_names_1.getDirectiveNames)(fieldNode);
return (0, utils_1.haveSharedElement)(directiveNames, [
"cypher",
"relationship",
"declareRelationship",
"customResolver",
"populatedBy",
]);
}
function typeNeedsResolver(type, document) {
const typeDef = document.definitions.find((def) => {
if (def.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION) {
if (def.name.value === type) {
return true;
}
return false;
}
});
if (!typeDef) {
return false;
}
else {
return true;
}
}
//# sourceMappingURL=object-fields-without-resolver.js.map