From 75932cccb8ca40eb58b2910824da94e20d767e45 Mon Sep 17 00:00:00 2001 From: Mohsin Ammar Date: Tue, 9 Nov 2021 10:43:27 +0100 Subject: [PATCH] Added support for metadata --- index.d.ts | 1 + src/index.js | 89 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/index.d.ts b/index.d.ts index 156f1bac..e1ba0351 100644 --- a/index.d.ts +++ b/index.d.ts @@ -94,6 +94,7 @@ declare module 'neo4j-graphql-js' { type Neo4jContext> = T & { driver: Driver; + metadata?: { [key: string]: string } }; /** diff --git a/src/index.js b/src/index.js index cdcd674d..f4c2abb1 100644 --- a/src/index.js +++ b/src/index.js @@ -59,6 +59,9 @@ export async function neo4jgraphql( ); } + const requestId = context?.requestId || context?.metadata?.requestId; + const metadata = { ...context?.metadata, requestId }; + let query; let cypherParams; @@ -128,15 +131,21 @@ export async function neo4jgraphql( try { if (isMutationOperation) { - result = await session.writeTransaction(async tx => { - const result = await tx.run(query, cypherParams); - return extractQueryResult(result, resolveInfo.returnType); - }); + result = await session.writeTransaction( + async tx => { + const result = await tx.run(query, cypherParams); + return extractQueryResult(result, resolveInfo.returnType); + }, + { metadata } + ); } else { - result = await session.readTransaction(async tx => { - const result = await tx.run(query, cypherParams); - return extractQueryResult(result, resolveInfo.returnType); - }); + result = await session.readTransaction( + async tx => { + const result = await tx.run(query, cypherParams); + return extractQueryResult(result, resolveInfo.returnType); + }, + { metadata } + ); } } finally { await session.close(); @@ -322,18 +331,23 @@ export const assertSchema = ({ sessionParams = {} }) => { const statement = schemaAssert({ schema, dropExisting }); + const metadata = sessionParams?.metadata || {}; const executeQuery = driver => { const session = driver.session(sessionParams); return session - .writeTransaction(tx => - tx.run(statement).then(result => { - if (debug === true) { - const recordsJSON = result.records.map(record => record.toObject()); - recordsJSON.sort((lhs, rhs) => lhs.label < rhs.label); - console.table(recordsJSON); - } - return result; - }) + .writeTransaction( + tx => + tx.run(statement).then(result => { + if (debug === true) { + const recordsJSON = result.records.map(record => + record.toObject() + ); + recordsJSON.sort((lhs, rhs) => lhs.label < rhs.label); + console.table(recordsJSON); + } + return result; + }), + { metadata } ) .finally(() => session.close()); }; @@ -348,6 +362,7 @@ export const searchSchema = async ({ dropExisting = true }) => { const session = driver.session(sessionParams); + const metadata = sessionParams?.metadata || {}; // drop all search indexes, given they cannot be updated via a second CALL to createNodeIndex const dropStatement = ` CALL db.indexes() YIELD name, provider WHERE provider = "fulltext-1.0" @@ -355,15 +370,17 @@ export const searchSchema = async ({ RETURN TRUE `; if (dropExisting) { - const dropResult = await session.writeTransaction(tx => - tx.run(dropStatement).then(result => { - if (debug === true) { - console.log( - `\n[searchSchema] Search indexes dropped using Cypher:${dropStatement}` - ); - } - return true; - }) + const dropResult = await session.writeTransaction( + tx => + tx.run(dropStatement).then(result => { + if (debug === true) { + console.log( + `\n[searchSchema] Search indexes dropped using Cypher:${dropStatement}` + ); + } + return true; + }), + { metadata } ); if (!dropResult) { await session.close(); @@ -374,15 +391,17 @@ export const searchSchema = async ({ const createStatement = schemaSearch({ schema }); if (createStatement) { return await session - .writeTransaction(tx => - tx.run(createStatement).then(result => { - if (debug === true) { - console.log( - `[searchSchema] Search indexes created using Cypher:\n${createStatement}\n` - ); - } - return true; - }) + .writeTransaction( + tx => + tx.run(createStatement).then(result => { + if (debug === true) { + console.log( + `[searchSchema] Search indexes created using Cypher:\n${createStatement}\n` + ); + } + return true; + }), + { metadata } ) .finally(() => session.close()); }