From 6ef9ca3343c8e6976dce06e563f49326b7665e00 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Wed, 11 Dec 2019 19:09:38 +0100 Subject: [PATCH] Refactor to use readTransaction --- backend/src/schema/resolvers/statistics.js | 39 ++++++++++++---------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/backend/src/schema/resolvers/statistics.js b/backend/src/schema/resolvers/statistics.js index 07b9e4cea..7ca9239f3 100644 --- a/backend/src/schema/resolvers/statistics.js +++ b/backend/src/schema/resolvers/statistics.js @@ -1,8 +1,10 @@ +import log from './helpers/databaseLogger' + export default { Query: { statistics: async (_parent, _args, { driver }) => { const session = driver.session() - const response = {} + const counts = {} try { const mapping = { countUsers: 'User', @@ -13,27 +15,28 @@ export default { countFollows: 'FOLLOWS', countShouts: 'SHOUTED', } - const cypher = ` - CALL apoc.meta.stats() YIELD labels, relTypesCount - RETURN labels, relTypesCount - ` - const result = await session.run(cypher) - const [statistics] = await result.records.map(record => { - return { - ...record.get('labels'), - ...record.get('relTypesCount'), - } + const statisticsReadTxResultPromise = session.readTransaction(async transaction => { + const statisticsTransactionResponse = await transaction.run( + ` + CALL apoc.meta.stats() YIELD labels, relTypesCount + RETURN labels, relTypesCount + `, + ) + log(statisticsTransactionResponse) + return statisticsTransactionResponse.records.map(record => { + return { + ...record.get('labels'), + ...record.get('relTypesCount'), + } + }) }) + const [statistics] = await statisticsReadTxResultPromise Object.keys(mapping).forEach(key => { const stat = statistics[mapping[key]] - response[key] = stat ? stat.toNumber() : 0 + counts[key] = stat ? stat.toNumber() : 0 }) - - /* - * Note: invites count is calculated this way because invitation codes are not in use yet - */ - response.countInvites = response.countEmails - response.countUsers - return response + counts.countInvites = counts.countEmails - counts.countUsers + return counts } finally { session.close() }