UNPKG

@neo4j/graphql

Version:

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations

90 lines 3.77 kB
"use strict"; /* * 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.CallbackBucket = void 0; const graphql_1 = require("graphql"); const scalars_1 = require("../../../graphql/scalars"); const AttributeType_1 = require("../../../schema-model/attribute/AttributeType"); class CallbackBucket { constructor(context) { this.context = context; this.callbacks = []; } addCallback(callback) { this.callbacks.push(callback); } /** Executes the callbacks and updates the values of the Cypher parameters attached to these callbacks */ async resolveCallbacks() { const callbacksList = this.context.features.populatedBy?.callbacks ?? {}; await Promise.all(this.callbacks.map(async (cb) => { const callbackFunction = callbacksList[cb.functionName]; if (callbackFunction) { const paramValue = await callbackFunction(cb.parent, {}, { ...this.context, populatedByOperation: cb.operation }); if (paramValue === undefined) { cb.param.value = undefined; } else if (paramValue === null) { cb.param.value = null; } else { cb.param.value = this.parseCallbackResult(paramValue, cb.type); } } })); } parseCallbackResult(result, type) { if (type instanceof AttributeType_1.ListType) { 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.ofType)); } 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.CallbackBucket = CallbackBucket; //# sourceMappingURL=callback-bucket.js.map