@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
91 lines • 4.26 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.CustomCypherSelection = void 0;
const cypher_builder_1 = __importDefault(require("@neo4j/cypher-builder"));
const QueryASTContext_1 = require("../QueryASTContext");
const EntitySelection_1 = require("./EntitySelection");
const replace_arguments_in_statement_1 = require("../../utils/replace-arguments-in-statement");
/** Variable exposed to the user in their custom cypher */
const CYPHER_TARGET_VARIABLE = new cypher_builder_1.default.NamedVariable("this");
class CustomCypherSelection extends EntitySelection_1.EntitySelection {
constructor({ operationField, rawArguments = {}, isNested, }) {
super();
this.operationField = operationField;
this.rawArguments = rawArguments;
this.isNested = isNested;
if (!this.operationField.annotations.cypher) {
throw new Error("Missing Cypher Annotation on Cypher field");
}
this.cypherAnnotation = this.operationField.annotations.cypher;
}
apply(context) {
const extraParams = {};
if (this.cypherAnnotation.statement.includes("$jwt") && context.neo4jGraphQLContext.authorization.jwtParam) {
extraParams.jwt = context.neo4jGraphQLContext.authorization.jwtParam.value;
}
const returnVariable = new cypher_builder_1.default.NamedVariable(this.cypherAnnotation.columnName);
const statementCypherQuery = new cypher_builder_1.default.Raw((env) => {
const statement = (0, replace_arguments_in_statement_1.replaceArgumentsInStatement)({
env,
definedArguments: this.operationField.args,
rawArguments: this.rawArguments,
statement: this.cypherAnnotation.statement,
});
return [statement, extraParams];
});
const thisVariable = new cypher_builder_1.default.Node();
let statementSubquery;
if (this.isNested && context.target) {
const aliasTargetToPublicTarget = new cypher_builder_1.default.With([context.target, CYPHER_TARGET_VARIABLE]);
statementSubquery = new cypher_builder_1.default.Call(cypher_builder_1.default.utils.concat(aliasTargetToPublicTarget, statementCypherQuery), [
context.target,
]);
}
else {
statementSubquery = new cypher_builder_1.default.Call(statementCypherQuery);
}
let selection;
const unwindVariable = new cypher_builder_1.default.Variable();
if (this.operationField.typeHelper.isList() &&
(this.operationField.typeHelper.isScalar() || this.operationField.typeHelper.isSpatial())) {
selection = statementSubquery.unwind([returnVariable, unwindVariable]).with([unwindVariable, thisVariable]);
}
else {
selection = statementSubquery.with([returnVariable, thisVariable]);
}
return {
selection,
nestedContext: new QueryASTContext_1.QueryASTContext({
source: context.target,
target: thisVariable,
neo4jGraphQLContext: context.neo4jGraphQLContext,
returnVariable: thisVariable,
env: context.env,
shouldCollect: this.isNested,
}),
};
}
}
exports.CustomCypherSelection = CustomCypherSelection;
//# sourceMappingURL=CustomCypherSelection.js.map