@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
83 lines • 3.3 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.globalNodeResolver = globalNodeResolver;
const graphql_parse_resolve_info_1 = require("graphql-parse-resolve-info");
const translate_1 = require("../../../translate");
const utils_1 = require("../../../utils");
const get_neo4j_resolve_tree_1 = __importDefault(require("../../../utils/get-neo4j-resolve-tree"));
const global_ids_1 = require("../../../utils/global-ids");
function globalNodeResolver({ entities }) {
async function resolve(_root, args, context, info) {
const { typeName, field, id } = (0, global_ids_1.fromGlobalId)(args.id);
if (!typeName || !field || !id)
return null;
const entityAdapter = entities.find((n) => n.name === typeName);
if (!entityAdapter)
return null;
// modify the resolve tree and append the fragment selectionSet
const parseInfo = (0, graphql_parse_resolve_info_1.parseResolveInfo)(info) ?? { fieldsByTypeName: [] };
const fieldsByTypeName = Object.entries(parseInfo.fieldsByTypeName).reduce((res, [key, value]) => {
if (key === "Node")
return res;
if (key === typeName) {
const fields = { ...value, id: { name: "id", alias: "id", args: {}, fieldsByTypeName: {} } };
return { ...res, [key]: fields };
}
return { ...res, [key]: value };
}, {});
const resolveTree = {
name: entityAdapter.plural,
alias: "node",
args: { where: { [`${field}_EQ`]: id } },
fieldsByTypeName,
};
context.resolveTree = (0, get_neo4j_resolve_tree_1.default)(info, { resolveTree });
const { cypher, params } = (0, translate_1.translateRead)({
context: context,
entityAdapter,
varName: "this",
});
const executeResult = await (0, utils_1.execute)({
cypher,
params,
defaultAccessMode: "READ",
context,
info,
});
let obj = null;
const thisValue = executeResult.records[0]?.this;
if (executeResult.records.length && thisValue) {
obj = { ...thisValue, id: args.id, __resolveType: entityAdapter.name };
}
return obj;
}
return {
type: `Node`,
resolve,
args: {
id: "ID!",
},
};
}
//# sourceMappingURL=global-node.js.map
;