diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 753b896e3..c27fdbb48 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -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)', diff --git a/backend/src/schema/types/type/Post.gql b/backend/src/schema/types/type/Post.gql index cbbcddfc9..4beee0e36 100644 --- a/backend/src/schema/types/type/Post.gql +++ b/backend/src/schema/types/type/Post.gql @@ -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( diff --git a/webapp/components/PostCard/PostCard.story.js b/webapp/components/PostCard/PostCard.story.js index 7cd579140..1f9f70110 100644 --- a/webapp/components/PostCard/PostCard.story.js +++ b/webapp/components/PostCard/PostCard.story.js @@ -38,7 +38,7 @@ const post = { ], __typename: 'User', }, - commentedCount: 12, + commentsCount: 12, categories: [], shoutedCount: 421, __typename: 'Post', diff --git a/webapp/components/PostCard/index.vue b/webapp/components/PostCard/index.vue index 43f846d89..3cbcbdd9e 100644 --- a/webapp/components/PostCard/index.vue +++ b/webapp/components/PostCard/index.vue @@ -51,9 +51,9 @@   - + - {{ post.commentedCount }} + {{ post.commentsCount }} { 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.', diff --git a/webapp/components/SearchInput.vue b/webapp/components/SearchInput.vue index c93e65d61..098b30610 100644 --- a/webapp/components/SearchInput.vue +++ b/webapp/components/SearchInput.vue @@ -46,7 +46,7 @@ - {{ option.commentedCount }} + {{ option.commentsCount }} diff --git a/webapp/graphql/PostQuery.js b/webapp/graphql/PostQuery.js index a5415c6cf..c646052f7 100644 --- a/webapp/graphql/PostQuery.js +++ b/webapp/graphql/PostQuery.js @@ -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 } - }` + ` } diff --git a/webapp/pages/index.vue b/webapp/pages/index.vue index a89e1ce76..5d03a309c 100644 --- a/webapp/pages/index.vue +++ b/webapp/pages/index.vue @@ -115,7 +115,7 @@ export default { label: this.$t('sorting.commented'), value: 'Commented', icons: 'comment', - order: 'commentedCount_desc', + order: 'commentsCount_desc', }, ], }