Refactor to use readTransaction

This commit is contained in:
mattwr18 2019-12-11 19:09:38 +01:00
parent 1e85cbb6a2
commit 6ef9ca3343

View File

@ -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()
}