perf(neo4j): Improve currentUser read performance

This commit is contained in:
roschaefer 2020-03-03 19:06:55 +01:00
parent 09d0953bb4
commit 7d9cbb3ce9
4 changed files with 47 additions and 32 deletions

View File

@ -12,10 +12,28 @@ export default {
isLoggedIn: (_, args, { driver, user }) => { isLoggedIn: (_, args, { driver, user }) => {
return Boolean(user && user.id) return Boolean(user && user.id)
}, },
currentUser: async (object, params, ctx, resolveInfo) => { currentUser: async (object, params, context, resolveInfo) => {
if (!ctx.user) return null const { user, driver } = context
const user = await neode.find('User', ctx.user.id) if (!user) return null
return user.toJson() const session = driver.session()
const currentUserTransactionPromise = session.readTransaction(async transaction => {
const result = await transaction.run(
`
MATCH (user:User {id: $id})
WITH user, [(user)<-[:OWNED_BY]-(medium:SocialMedia) | properties(medium) ] as media
RETURN user {.*, socialMedia: media } as user
`,
{ id: user.id },
)
log(result)
return result.records.map(record => record.get('user'))
})
try {
const [currentUser] = await currentUserTransactionPromise
return currentUser
} finally {
session.close()
}
}, },
}, },
Mutation: { Mutation: {

View File

@ -97,14 +97,14 @@ type User {
contributions: [Post]! @relation(name: "WROTE", direction: "OUT") contributions: [Post]! @relation(name: "WROTE", direction: "OUT")
contributionsCount: Int! @cypher( contributionsCount: Int! @cypher(
statement: """ statement: """
MATCH (this)-[: WROTE]->(r: Post) MATCH (this)-[:WROTE]->(r:Post)
WHERE NOT r.deleted = true AND NOT r.disabled = true WHERE NOT r.deleted = true AND NOT r.disabled = true
RETURN COUNT(r) RETURN COUNT(r)
""" """
) )
comments: [Comment]! @relation(name: "WROTE", direction: "OUT") comments: [Comment]! @relation(name: "WROTE", direction: "OUT")
commentedCount: Int! @cypher(statement: "MATCH (this)-[: WROTE]->(: Comment)-[: COMMENTS]->(p: Post) WHERE NOT p.deleted = true AND NOT p.disabled = true RETURN COUNT(DISTINCT(p))") commentedCount: Int! @cypher(statement: "MATCH (this)-[:WROTE]->(:Comment)-[:COMMENTS]->(p:Post) WHERE NOT p.deleted = true AND NOT p.disabled = true RETURN COUNT(DISTINCT(p))")
shouted: [Post]! @relation(name: "SHOUTED", direction: "OUT") shouted: [Post]! @relation(name: "SHOUTED", direction: "OUT")
shoutedCount: Int! @cypher(statement: "MATCH (this)-[: SHOUTED]->(r: Post) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)") shoutedCount: Int! @cypher(statement: "MATCH (this)-[: SHOUTED]->(r: Post) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)")

View File

@ -258,3 +258,24 @@ export const checkSlugAvailableQuery = gql`
} }
} }
` `
export const currentUserQuery = gql`
${userFragment}
query {
currentUser {
...user
email
role
about
locationName
locale
allowEmbedIframes
showShoutsPublicly
termsAndConditionsAgreedVersion
socialMedia {
id
url
}
}
}
`

View File

@ -1,5 +1,6 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
import { VERSION } from '~/constants/terms-and-conditions-version.js' import { VERSION } from '~/constants/terms-and-conditions-version.js'
import { currentUserQuery } from '~/graphql/User'
export const state = () => { export const state = () => {
return { return {
@ -72,32 +73,7 @@ export const actions = {
const client = this.app.apolloProvider.defaultClient const client = this.app.apolloProvider.defaultClient
const { const {
data: { currentUser }, data: { currentUser },
} = await client.query({ } = await client.query({ query: currentUserQuery })
query: gql`
query {
currentUser {
id
name
slug
email
avatar
role
about
locationName
locale
contributionsCount
commentedCount
allowEmbedIframes
showShoutsPublicly
termsAndConditionsAgreedVersion
socialMedia {
id
url
}
}
}
`,
})
if (!currentUser) return dispatch('logout') if (!currentUser) return dispatch('logout')
commit('SET_USER', currentUser) commit('SET_USER', currentUser)
return currentUser return currentUser