Refactor graphql queries and fix bug

It's sometimes unbelievable how many bugs you find when doing
refactoring. This time there was some apparent confusion about
`commentsCount` and `commentedCount`. The counters on the post card were
never showing the correct number.
This commit is contained in:
roschaefer 2019-09-03 15:36:52 +02:00
parent 7b44a87e3b
commit bb3f419532
8 changed files with 79 additions and 141 deletions

View File

@ -217,6 +217,8 @@ export default {
disabledBy: '<-[:DISABLED]-(related:User)',
},
count: {
commentsCount:
'<-[:COMMENTS]-(related:Comment) WHERE NOT related.deleted = true AND NOT related.disabled = true',
shoutedCount:
'<-[:SHOUTED]-(related:User) WHERE NOT related.deleted = true AND NOT related.disabled = true',
emotionsCount: '<-[related:EMOTED]-(:User)',

View File

@ -29,6 +29,11 @@ type Post {
categories: [Category]! @relation(name: "CATEGORIZED", direction: "OUT")
comments: [Comment]! @relation(name: "COMMENTS", direction: "IN")
commentsCount: Int!
@cypher(
statement: "MATCH (this)<-[:COMMENTS]-(r:Comment) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)"
)
shoutedBy: [User]! @relation(name: "SHOUTED", direction: "IN")
shoutedCount: Int!
@cypher(

View File

@ -38,7 +38,7 @@ const post = {
],
__typename: 'User',
},
commentedCount: 12,
commentsCount: 12,
categories: [],
shoutedCount: 421,
__typename: 'Post',

View File

@ -51,9 +51,9 @@
</span>
&nbsp;
<!-- Comments Count -->
<span :style="{ opacity: post.commentedCount ? 1 : 0.5 }">
<span :style="{ opacity: post.commentsCount ? 1 : 0.5 }">
<ds-icon name="comments" />
<small>{{ post.commentedCount }}</small>
<small>{{ post.commentsCount }}</small>
</span>
<!-- Menu -->
<content-menu

View File

@ -122,7 +122,7 @@ describe('SearchInput.vue', () => {
name: 'Trick',
slug: 'trick',
},
commentedCount: 0,
commentsCount: 0,
createdAt: '2019-03-13T11:00:20.835Z',
id: 'p10',
label: 'Eos aut illo omnis quis eaque et iure aut.',

View File

@ -46,7 +46,7 @@
<ds-flex-item>
<ds-text size="small" color="softer" class="search-meta">
<span style="text-align: right;">
<b>{{ option.commentedCount }}</b>
<b>{{ option.commentsCount }}</b>
<ds-icon name="comments" />
</span>
<span style="width: 36px; display: inline-block; text-align: right;">

View File

@ -1,41 +1,62 @@
import gql from 'graphql-tag'
const fragments = lang => gql`
fragment user on User {
id
slug
name
avatar
disabled
deleted
shoutedCount
contributionsCount
commentedCount
followedByCount
followedByCurrentUser
location {
name: name${lang}
}
badges {
id
icon
}
}
fragment post on Post {
id
title
content
contentExcerpt
createdAt
disabled
deleted
slug
image
commentsCount
shoutedCount
shoutedByCurrentUser
emotionsCount
author {
...user
}
tags {
id
}
categories {
id
name
icon
}
}
`
export default i18n => {
const lang = i18n.locale().toUpperCase()
return gql`
${fragments(lang)}
query Post($id: ID!) {
Post(id: $id) {
id
title
content
createdAt
disabled
deleted
slug
image
author {
id
slug
name
avatar
disabled
deleted
shoutedCount
contributionsCount
commentedCount
followedByCount
followedByCurrentUser
location {
name: name${lang}
}
badges {
id
icon
}
}
tags {
id
}
...post
comments(orderBy: createdAt_asc) {
id
contentExcerpt
@ -44,34 +65,9 @@ export default i18n => {
disabled
deleted
author {
id
slug
name
avatar
disabled
deleted
shoutedCount
contributionsCount
commentedCount
followedByCount
followedByCurrentUser
location {
name: name${lang}
}
badges {
id
icon
}
...user
}
}
categories {
id
name
icon
}
shoutedCount
shoutedByCurrentUser
emotionsCount
}
}
`
@ -80,45 +76,13 @@ export default i18n => {
export const filterPosts = i18n => {
const lang = i18n.locale().toUpperCase()
return gql`
query Post($filter: _PostFilter, $first: Int, $offset: Int, $orderBy: [_PostOrdering]) {
Post(filter: $filter, first: $first, offset: $offset, orderBy: $orderBy) {
id
title
contentExcerpt
createdAt
disabled
deleted
slug
image
author {
id
avatar
slug
name
disabled
deleted
contributionsCount
shoutedCount
commentedCount
followedByCount
followedByCurrentUser
location {
name: name${lang}
}
badges {
id
icon
}
${fragments(lang)}
query Post($filter: _PostFilter, $first: Int, $offset: Int, $orderBy: [_PostOrdering]) {
Post(filter: $filter, first: $first, offset: $offset, orderBy: $orderBy) {
...post
}
categories {
id
name
icon
}
shoutedCount
}
}
`
`
}
export const PostsEmotionsByCurrentUser = () => {
@ -131,48 +95,15 @@ export const PostsEmotionsByCurrentUser = () => {
export const relatedContributions = i18n => {
const lang = i18n.locale().toUpperCase()
return gql`query Post($slug: String!) {
Post(slug: $slug) {
id
title
tags {
id
}
categories {
id
name
icon
}
relatedContributions(first: 2) {
id
title
slug
contentExcerpt
shoutedCount
categories {
id
name
icon
}
author {
id
name
slug
avatar
contributionsCount
followedByCount
followedByCurrentUser
commentedCount
location {
name: name${lang}
}
badges {
id
icon
}
return gql`
${fragments(lang)}
query Post($slug: String!) {
Post(slug: $slug) {
...post
relatedContributions(first: 2) {
...post
}
}
shoutedCount
}
}`
`
}

View File

@ -115,7 +115,7 @@ export default {
label: this.$t('sorting.commented'),
value: 'Commented',
icons: 'comment',
order: 'commentedCount_desc',
order: 'commentsCount_desc',
},
],
}