@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
92 lines • 3.71 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.CallbackBucketDeprecated = void 0;
const graphql_1 = require("graphql");
const scalars_1 = require("../graphql/scalars");
/** @deprecated */
class CallbackBucketDeprecated {
constructor(context) {
this.context = context;
this.callbacks = [];
}
addCallback(callback) {
this.callbacks.push(callback);
}
async resolveCallbacksAndFilterCypher(options) {
const params = {};
let cypher = options.cypher;
await Promise.all(this.callbacks.map(async (cb) => {
const callbackFunction = (this.context.features.populatedBy?.callbacks)[cb.functionName];
const param = await callbackFunction(cb.parent, {}, this.context);
if (param === undefined) {
cypher = cypher
.split("\n")
.filter((line) => !line.includes(`$resolvedCallbacks.${cb.paramName}`))
.join("\n");
}
else if (param === null) {
params[cb.paramName] = null;
}
else {
params[cb.paramName] = this.parseCallbackResult(param, cb.type);
}
}));
return { cypher, params };
}
parseCallbackResult(result, type) {
if (type.array) {
if (!Array.isArray(result)) {
throw new graphql_1.GraphQLError("Expected list as callback result but did not.");
}
return result.map((r) => this.parseCallbackResult(r, { ...type, array: false }));
}
switch (type.name) {
case "Int":
return graphql_1.GraphQLInt.parseValue(result);
case "Float":
return graphql_1.GraphQLFloat.parseValue(result);
case "String":
return graphql_1.GraphQLString.parseValue(result);
case "Boolean":
return graphql_1.GraphQLBoolean.parseValue(result);
case "ID":
return graphql_1.GraphQLID.parseValue(result);
case "BigInt":
return scalars_1.GraphQLBigInt.parseValue(result);
case "DateTime":
return scalars_1.GraphQLDateTime.parseValue(result);
case "Date":
return scalars_1.GraphQLDate.parseValue(result);
case "Time":
return scalars_1.GraphQLTime.parseValue(result);
case "LocalDateTime":
return scalars_1.GraphQLLocalDateTime.parseValue(result);
case "LocalTime":
return scalars_1.GraphQLLocalTime.parseValue(result);
case "Duration":
return scalars_1.GraphQLDuration.parseValue(result);
default:
throw new graphql_1.GraphQLError("Callback result received for field of unsupported type.");
}
}
}
exports.CallbackBucketDeprecated = CallbackBucketDeprecated;
//# sourceMappingURL=CallbackBucketDeprecated.js.map
;