Skip to content

Commit

Permalink
Merge pull request #4150 from thematters/fix/delete-comment-cache
Browse files Browse the repository at this point in the history
fix(comment): make sure finish cache invalidation before return
  • Loading branch information
gary02 committed Aug 21, 2024
2 parents f754fae + 5dc6159 commit e1f18ff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
18 changes: 8 additions & 10 deletions src/mutations/comment/deleteComment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { GQLMutationResolvers, Comment } from 'definitions'
import type { GQLMutationResolvers } from 'definitions'

import { invalidateFQC } from '@matters/apollo-response-cache'

import {
CACHE_KEYWORD,
COMMENT_STATE,
COMMENT_TYPE,
NODE_TYPES,
Expand All @@ -18,7 +19,7 @@ import { fromGlobalId } from 'common/utils'
const resolver: GQLMutationResolvers['deleteComment'] = async (
_,
{ input: { id } },
{ viewer, dataSources: { atomService, notificationService } }
{ viewer, dataSources: { atomService, notificationService, connections } }
) => {
if (!viewer.id) {
throw new AuthenticationError('visitor has no permission')
Expand Down Expand Up @@ -57,12 +58,8 @@ const resolver: GQLMutationResolvers['deleteComment'] = async (
notificationService.withdraw(`put-comment:${dbId}`)

// invalidate extra nodes
;(
newComment as Comment & {
[CACHE_KEYWORD]: [{ id: string; type: NODE_TYPES }]
}
)[CACHE_KEYWORD] = [
{
await invalidateFQC({
node: {
id: comment.targetId,
type:
comment.type === COMMENT_TYPE.article
Expand All @@ -71,7 +68,8 @@ const resolver: GQLMutationResolvers['deleteComment'] = async (
? NODE_TYPES.Moment
: NODE_TYPES.Circle,
},
]
redis: connections.redis,
})

return newComment
}
Expand Down
39 changes: 30 additions & 9 deletions src/mutations/comment/putComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Moment,
} from 'definitions'

import { invalidateFQC } from '@matters/apollo-response-cache'
import { stripHtml } from '@matters/ipns-site-generator'
import {
normalizeCommentHTML,
Expand All @@ -20,7 +21,6 @@ import {
ARTICLE_ACCESS_TYPE,
ARTICLE_STATE,
BUNDLED_NOTICE_TYPE,
CACHE_KEYWORD,
COMMENT_TYPE,
COMMENT_STATE,
NOTICE_TYPE,
Expand Down Expand Up @@ -67,6 +67,7 @@ const resolver: GQLMutationResolvers['putComment'] = async (
articleService,
notificationService,
userService,
connections,
},
}
) => {
Expand Down Expand Up @@ -583,14 +584,34 @@ const resolver: GQLMutationResolvers['putComment'] = async (
})

// invalidate extra nodes
;(newComment as Comment & { [CACHE_KEYWORD]: any })[CACHE_KEYWORD] = [
parentComment ? { id: parentComment.id, type: NODE_TYPES.Comment } : {},
replyToComment ? { id: replyToComment.id, type: NODE_TYPES.Comment } : {},
{
id: article ? article.id : circle?.id,
type: article ? NODE_TYPES.Article : NODE_TYPES.Circle,
},
]
if (parentComment) {
await invalidateFQC({
node: { id: parentComment.id, type: NODE_TYPES.Comment },
redis: connections.redis,
})
}
if (replyToComment) {
await invalidateFQC({
node: { id: replyToComment.id, type: NODE_TYPES.Comment },
redis: connections.redis,
})
}

const node = isArticleType
? {
id: article?.id as string,
type: NODE_TYPES.Article,
}
: isMoment
? {
id: moment?.id as string,
type: NODE_TYPES.Moment,
}
: {
id: circle?.id as string,
type: NODE_TYPES.Circle,
}
await invalidateFQC({ node, redis: connections.redis })

return newComment
}
Expand Down
2 changes: 1 addition & 1 deletion src/mutations/comment/updateCommentsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const resolver: GQLMutationResolvers['updateCommentsState'] = async (
})

if (comment.type === COMMENT_TYPE.article) {
invalidateFQC({
await invalidateFQC({
node: { type: NODE_TYPES.Article, id: comment.targetId },
redis: connections.redis,
})
Expand Down

0 comments on commit e1f18ff

Please sign in to comment.