Merge pull request #3207 from Human-Connection/improve_performance_of_current_user_query

perf(neo4j): Improve currentUser read performance
This commit is contained in:
Robert Schäfer 2020-03-05 12:33:06 +01:00 committed by GitHub
commit 47fb70bb4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 39 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

@ -27,7 +27,7 @@ type User {
id: ID!
actorId: String
name: String
email: String! @cypher(statement: "MATCH (this)-[: PRIMARY_EMAIL]->(e: EmailAddress) RETURN e.email")
email: String! @cypher(statement: "MATCH (this)-[:PRIMARY_EMAIL]->(e:EmailAddress) RETURN e.email")
slug: String!
avatar: String
coverImg: String
@ -38,7 +38,7 @@ type User {
invitedBy: User @relation(name: "INVITED", direction: "IN")
invited: [User] @relation(name: "INVITED", direction: "OUT")
location: Location @cypher(statement: "MATCH (this)-[: IS_IN]->(l: Location) RETURN l")
location: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
locationName: String
about: String
socialMedia: [SocialMedia]! @relation(name: "OWNED_BY", direction: "IN")
@ -53,13 +53,13 @@ type User {
showShoutsPublicly: Boolean
locale: String
friends: [User]! @relation(name: "FRIENDS", direction: "BOTH")
friendsCount: Int! @cypher(statement: "MATCH (this)<-[: FRIENDS]->(r: User) RETURN COUNT(DISTINCT r)")
friendsCount: Int! @cypher(statement: "MATCH (this)<-[:FRIENDS]->(r:User) RETURN COUNT(DISTINCT r)")
following: [User]! @relation(name: "FOLLOWS", direction: "OUT")
followingCount: Int! @cypher(statement: "MATCH (this)-[: FOLLOWS]->(r: User) RETURN COUNT(DISTINCT r)")
followingCount: Int! @cypher(statement: "MATCH (this)-[:FOLLOWS]->(r:User) RETURN COUNT(DISTINCT r)")
followedBy: [User]! @relation(name: "FOLLOWS", direction: "IN")
followedByCount: Int! @cypher(statement: "MATCH (this)<-[: FOLLOWS]-(r: User) RETURN COUNT(DISTINCT r)")
followedByCount: Int! @cypher(statement: "MATCH (this)<-[:FOLLOWS]-(r:User) RETURN COUNT(DISTINCT r)")
# Is the currently logged in user following that user?
followedByCurrentUser: Boolean! @cypher(
@ -97,22 +97,22 @@ 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)")
shoutedCount: Int! @cypher(statement: "MATCH (this)-[:SHOUTED]->(r:Post) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)")
categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT")
badges: [Badge]! @relation(name: "REWARDED", direction: "IN")
badgesCount: Int! @cypher(statement: "MATCH (this)<-[: REWARDED]-(r: Badge) RETURN COUNT(r)")
badgesCount: Int! @cypher(statement: "MATCH (this)<-[:REWARDED]-(r:Badge) RETURN COUNT(r)")
emotions: [EMOTED]
}

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