From 20112bdd9b9e79247bacc8687948a4b99c00af47 Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Tue, 23 Apr 2019 12:24:27 -0300 Subject: [PATCH] Add back asyncData, use CommentByPost query --- webapp/graphql/CommentQuery.js | 13 +++ webapp/pages/post/_id/_slug/index.vue | 116 ++++++++++++++++++++++++-- 2 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 webapp/graphql/CommentQuery.js diff --git a/webapp/graphql/CommentQuery.js b/webapp/graphql/CommentQuery.js new file mode 100644 index 000000000..1c3f9be20 --- /dev/null +++ b/webapp/graphql/CommentQuery.js @@ -0,0 +1,13 @@ +import gql from 'graphql-tag' + +export default app => { + return gql(` + query CommentByPost($postId: ID!) { + CommentByPost(postId: $postId, orderBy: createdAt_desc) { + id + contentExcerpt + createdAt + } + } + `) +} diff --git a/webapp/pages/post/_id/_slug/index.vue b/webapp/pages/post/_id/_slug/index.vue index 27b552608..0ecd98c4e 100644 --- a/webapp/pages/post/_id/_slug/index.vue +++ b/webapp/pages/post/_id/_slug/index.vue @@ -102,7 +102,7 @@ - +
@@ -210,6 +210,7 @@ export default { data() { return { post: null, + comments: null, ready: false, title: 'loading', loading: false, @@ -226,6 +227,105 @@ export default { Post(post) { this.post = post[0] || {} this.title = this.post.title + }, + CommentByPost(comments) { + this.comments = comments || [] + } + }, + async asyncData(context) { + const { + params, + error, + app: { apolloProvider, $i18n } + } = context + const client = apolloProvider.defaultClient + const query = gql(` + query Post($slug: String!) { + Post(slug: $slug) { + id + title + content + createdAt + disabled + deleted + slug + image + author { + id + slug + name + avatar + disabled + deleted + shoutedCount + contributionsCount + commentsCount + followedByCount + followedByCurrentUser + location { + name: name${$i18n.locale().toUpperCase()} + } + badges { + id + key + icon + } + } + tags { + name + } + commentsCount + comments(orderBy: createdAt_desc) { + id + contentExcerpt + createdAt + disabled + deleted + author { + id + slug + name + avatar + disabled + deleted + shoutedCount + contributionsCount + commentsCount + followedByCount + followedByCurrentUser + location { + name: name${$i18n.locale().toUpperCase()} + } + badges { + id + key + icon + } + } + } + categories { + id + name + icon + } + shoutedCount + shoutedByCurrentUser + } + } + `) + const variables = { slug: params.slug } + const { + data: { Post } + } = await client.query({ query, variables }) + if (Post.length <= 0) { + // TODO: custom 404 error page with translations + const message = 'This post could not be found' + return error({ statusCode: 404, message }) + } + const [post] = Post + return { + post, + title: post.title } }, mounted() { @@ -248,7 +348,7 @@ export default { this.form.content = ' ' }, addComment(comment) { - this.$apollo.queries.Post.refetch() + this.$apollo.queries.CommentByPost.refetch() // this.post = { ...this.post, comments: [...this.post.comments, comment] } }, handleSubmit() { @@ -283,13 +383,13 @@ export default { } }, apollo: { - Post: { + CommentByPost: { query() { - return require('~/graphql/PostQuery.js').default(this) + return require('~/graphql/CommentQuery.js').default(this) }, variables() { return { - slug: this.$route.params.slug + postId: this.post.id } }, fetchPolicy: 'cache-and-network'