@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
72 lines • 3.81 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.ValidateAuthorizationLikeDirectives = ValidateAuthorizationLikeDirectives;
const graphql_1 = require("graphql");
const validation_error_codes_1 = require("../utils/validation-error-codes");
const document_validation_error_1 = require("./utils/document-validation-error");
const path_parser_1 = require("./utils/path-parser");
const utils_1 = require("./utils/utils");
/**
* ValidateAuthorizationLikeDirectives validates the directives subscriptionsAuthorization, authorization, authentication
**/
function ValidateAuthorizationLikeDirectives(context) {
const validationSchema = context.getSchema();
if (!validationSchema) {
throw new Error("Validation error: schema is not available");
}
const schema = (0, graphql_1.extendSchema)(validationSchema, context.getDocument(), { assumeValid: true, assumeValidSDL: true });
return {
Directive(directiveNode, _key, _parent, path, ancestors) {
const authorizationLikeDirective = ["subscriptionsAuthorization", "authorization", "authentication"].find((authLikeDirective) => {
// find authorizationLike directive generated for validation purposes such a MovieAuthorization
// see packages/graphql/src/graphql/directives/type-dependant-directives/authorization.ts as example
return directiveNode.name.value.toLowerCase().includes(authLikeDirective.toLowerCase());
});
if (!authorizationLikeDirective) {
return;
}
const directiveDefinition = schema.getDirective(directiveNode.name.value);
if (!directiveDefinition) {
// Do not report, delegate this report to KnownDirectivesRule
return;
}
const pathToHere = [...(0, path_parser_1.getPathToNode)(path, ancestors)[0], `@${authorizationLikeDirective}`];
for (const argument of directiveNode.arguments ?? []) {
const argumentDefinition = (0, utils_1.findArgumentDefinitionNodeByName)(directiveDefinition.args, argument.name.value);
if (!argumentDefinition) {
return; // If argument name is not found, delegate to KnownArgumentNamesRule
}
const { isValid, errorMsg, errorPath } = (0, utils_1.assertArgumentType)(argument, argumentDefinition);
if (!isValid) {
context.reportError((0, document_validation_error_1.createGraphQLError)({
nodes: [argument, directiveNode],
path: [...pathToHere, argument.name.value, ...errorPath],
errorMsg: `Invalid argument: ${argument.name.value}, error: ${errorMsg}`,
extensions: {
exception: { code: validation_error_codes_1.VALIDATION_ERROR_CODES[authorizationLikeDirective.toUpperCase()] },
},
}));
}
}
},
};
}
//# sourceMappingURL=validate-authorization-like-directives.js.map