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 }) => {
return Boolean(user && user.id)
},
currentUser: async (object, params, ctx, resolveInfo) => {
if (!ctx.user) return null
const user = await neode.find('User', ctx.user.id)
return user.toJson()
currentUser: async (object, params, context, resolveInfo) => {
const { user, driver } = context
if (!user) return null
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: {

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