mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-12 23:35:58 +00:00
perf(neo4j): Improve currentUser read performance
This commit is contained in:
parent
09d0953bb4
commit
7d9cbb3ce9
@ -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: {
|
||||
|
||||
@ -97,14 +97,14 @@ type User {
|
||||
contributions: [Post]! @relation(name: "WROTE", direction: "OUT")
|
||||
contributionsCount: Int! @cypher(
|
||||
statement: """
|
||||
MATCH (this)-[: WROTE]->(r: Post)
|
||||
MATCH (this)-[:WROTE]->(r:Post)
|
||||
WHERE NOT r.deleted = true AND NOT r.disabled = true
|
||||
RETURN COUNT(r)
|
||||
"""
|
||||
)
|
||||
|
||||
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")
|
||||
shoutedCount: Int! @cypher(statement: "MATCH (this)-[: SHOUTED]->(r: Post) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)")
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user