mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Refactor graphql queries
- Remove duplicate queries - Use smart query in pages/post/_id/_slug/index.vue to avoid multiple db requests for a post and its comments. We cannot update the apollo cache with asyncData and smart queries have a prefetch policy set to true by default, which means that they will resolve in a similar timeframe. https://stackoverflow.com/questions/55885337/in-nuxt-should-i-use-asyncdata-or-default-apollo-queries https://vue-apollo.netlify.com/api/smart-query.html#options https://vue-apollo.netlify.com/guide/ssr.html#vue-cli-plugin
This commit is contained in:
parent
cd80981130
commit
d511d6aa78
@ -24,10 +24,10 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import gql from 'graphql-tag'
|
import gql from 'graphql-tag'
|
||||||
import HcEditor from '~/components/Editor/Editor'
|
|
||||||
import PostCommentsQuery from '~/graphql/PostCommentsQuery.js'
|
|
||||||
import CommentMutations from '~/graphql/CommentMutations.js'
|
|
||||||
import { mapGetters } from 'vuex'
|
import { mapGetters } from 'vuex'
|
||||||
|
import HcEditor from '~/components/Editor/Editor'
|
||||||
|
import PostQuery from '~/graphql/PostQuery'
|
||||||
|
import CommentMutations from '~/graphql/CommentMutations'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -35,7 +35,6 @@ export default {
|
|||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
post: { type: Object, default: () => {} },
|
post: { type: Object, default: () => {} },
|
||||||
comments: { type: Array, default: () => [] },
|
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -77,11 +76,11 @@ export default {
|
|||||||
},
|
},
|
||||||
update: (store, { data: { CreateComment } }) => {
|
update: (store, { data: { CreateComment } }) => {
|
||||||
const data = store.readQuery({
|
const data = store.readQuery({
|
||||||
query: PostCommentsQuery(this.$i18n),
|
query: PostQuery(this.$i18n),
|
||||||
variables: { slug: this.post.slug },
|
variables: { slug: this.post.slug },
|
||||||
})
|
})
|
||||||
data.Post[0].comments.push(CreateComment)
|
data.Post[0].comments.push(CreateComment)
|
||||||
store.writeQuery({ query: PostCommentsQuery(this.$i18n), data })
|
store.writeQuery({ query: PostQuery(this.$i18n), data })
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
@ -98,14 +97,16 @@ export default {
|
|||||||
apollo: {
|
apollo: {
|
||||||
User: {
|
User: {
|
||||||
query() {
|
query() {
|
||||||
return gql(`{
|
return gql`
|
||||||
User(orderBy: slug_asc) {
|
{
|
||||||
id
|
User(orderBy: slug_asc) {
|
||||||
slug
|
id
|
||||||
name
|
slug
|
||||||
avatar
|
name
|
||||||
|
avatar
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}`)
|
`
|
||||||
},
|
},
|
||||||
result(result) {
|
result(result) {
|
||||||
this.users = result.data.User
|
this.users = result.data.User
|
||||||
|
|||||||
@ -4,25 +4,25 @@
|
|||||||
<span>
|
<span>
|
||||||
<ds-icon name="comments" />
|
<ds-icon name="comments" />
|
||||||
<ds-tag
|
<ds-tag
|
||||||
v-if="comments"
|
v-if="post.comments"
|
||||||
style="margin-top: -4px; margin-left: -12px; position: absolute;"
|
style="margin-top: -4px; margin-left: -12px; position: absolute;"
|
||||||
color="primary"
|
color="primary"
|
||||||
size="small"
|
size="small"
|
||||||
round
|
round
|
||||||
>
|
>
|
||||||
{{ comments.length }}
|
{{ post.comments.length }}
|
||||||
</ds-tag>
|
</ds-tag>
|
||||||
Comments
|
Comments
|
||||||
</span>
|
</span>
|
||||||
</h3>
|
</h3>
|
||||||
<ds-space margin-bottom="large" />
|
<ds-space margin-bottom="large" />
|
||||||
<div v-if="comments && comments.length" id="comments" class="comments">
|
<div v-if="post.comments && post.comments.length" id="comments" class="comments">
|
||||||
<comment
|
<comment
|
||||||
v-for="(comment, index) in comments"
|
v-for="(comment, index) in post.comments"
|
||||||
:key="comment.id"
|
:key="comment.id"
|
||||||
:comment="comment"
|
:comment="comment"
|
||||||
:post="post"
|
:post="post"
|
||||||
@deleteComment="comments.splice(index, 1)"
|
@deleteComment="post.comments.splice(index, 1)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<hc-empty v-else name="empty" icon="messages" />
|
<hc-empty v-else name="empty" icon="messages" />
|
||||||
@ -31,7 +31,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import Comment from '~/components/Comment.vue'
|
import Comment from '~/components/Comment.vue'
|
||||||
import HcEmpty from '~/components/Empty.vue'
|
import HcEmpty from '~/components/Empty.vue'
|
||||||
import PostCommentsQuery from '~/graphql/PostCommentsQuery.js'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -41,29 +40,5 @@ export default {
|
|||||||
props: {
|
props: {
|
||||||
post: { type: Object, default: () => {} },
|
post: { type: Object, default: () => {} },
|
||||||
},
|
},
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
comments: [],
|
|
||||||
}
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
Post(post) {
|
|
||||||
const [first] = post
|
|
||||||
this.comments = (first && first.comments) || []
|
|
||||||
},
|
|
||||||
},
|
|
||||||
apollo: {
|
|
||||||
Post: {
|
|
||||||
query() {
|
|
||||||
return PostCommentsQuery(this.$i18n)
|
|
||||||
},
|
|
||||||
variables() {
|
|
||||||
return {
|
|
||||||
slug: this.post.slug,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fetchPolicy: 'cache-and-network',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -1,39 +0,0 @@
|
|||||||
import gql from 'graphql-tag'
|
|
||||||
|
|
||||||
export default i18n => {
|
|
||||||
const lang = i18n.locale().toUpperCase()
|
|
||||||
return gql(`
|
|
||||||
query Post($slug: String!) {
|
|
||||||
Post(slug: $slug) {
|
|
||||||
comments(orderBy: createdAt_asc) {
|
|
||||||
id
|
|
||||||
contentExcerpt
|
|
||||||
content
|
|
||||||
createdAt
|
|
||||||
disabled
|
|
||||||
deleted
|
|
||||||
author {
|
|
||||||
id
|
|
||||||
slug
|
|
||||||
name
|
|
||||||
avatar
|
|
||||||
disabled
|
|
||||||
deleted
|
|
||||||
shoutedCount
|
|
||||||
contributionsCount
|
|
||||||
commentsCount
|
|
||||||
followedByCount
|
|
||||||
followedByCurrentUser
|
|
||||||
location {
|
|
||||||
name: name${lang}
|
|
||||||
}
|
|
||||||
badges {
|
|
||||||
id
|
|
||||||
icon
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
@ -37,9 +37,10 @@ export default i18n => {
|
|||||||
name
|
name
|
||||||
}
|
}
|
||||||
commentsCount
|
commentsCount
|
||||||
comments(orderBy: createdAt_desc) {
|
comments(orderBy: createdAt_asc) {
|
||||||
id
|
id
|
||||||
contentExcerpt
|
contentExcerpt
|
||||||
|
content
|
||||||
createdAt
|
createdAt
|
||||||
disabled
|
disabled
|
||||||
deleted
|
deleted
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import gql from 'graphql-tag'
|
|||||||
|
|
||||||
export default i18n => {
|
export default i18n => {
|
||||||
const lang = i18n.locale().toUpperCase()
|
const lang = i18n.locale().toUpperCase()
|
||||||
return gql(`
|
return gql`
|
||||||
query User($id: ID!) {
|
query User($id: ID!) {
|
||||||
User(id: $id) {
|
User(id: $id) {
|
||||||
id
|
id
|
||||||
@ -73,5 +73,5 @@ export default i18n => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`)
|
`
|
||||||
}
|
}
|
||||||
@ -1,38 +0,0 @@
|
|||||||
import gql from 'graphql-tag'
|
|
||||||
|
|
||||||
export default i18n => {
|
|
||||||
const lang = i18n.locale().toUpperCase()
|
|
||||||
return gql(`
|
|
||||||
query Post($filter: _PostFilter, $first: Int, $offset: Int) {
|
|
||||||
Post(filter: $filter, first: $first, offset: $offset, orderBy: createdAt_desc) {
|
|
||||||
id
|
|
||||||
slug
|
|
||||||
title
|
|
||||||
contentExcerpt
|
|
||||||
shoutedCount
|
|
||||||
commentsCount
|
|
||||||
deleted
|
|
||||||
image
|
|
||||||
createdAt
|
|
||||||
disabled
|
|
||||||
deleted
|
|
||||||
categories {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
icon
|
|
||||||
}
|
|
||||||
author {
|
|
||||||
id
|
|
||||||
slug
|
|
||||||
avatar
|
|
||||||
name
|
|
||||||
disabled
|
|
||||||
deleted
|
|
||||||
location {
|
|
||||||
name: name${lang}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
@ -84,7 +84,7 @@ import HcShoutButton from '~/components/ShoutButton.vue'
|
|||||||
import HcCommentForm from '~/components/comments/CommentForm'
|
import HcCommentForm from '~/components/comments/CommentForm'
|
||||||
import HcCommentList from '~/components/comments/CommentList'
|
import HcCommentList from '~/components/comments/CommentList'
|
||||||
import { postMenuModalsData, deletePostMutation } from '~/components/utils/PostHelpers'
|
import { postMenuModalsData, deletePostMutation } from '~/components/utils/PostHelpers'
|
||||||
import PostQuery from '~/graphql/PostQuery.js'
|
import PostQuery from '~/graphql/PostQuery'
|
||||||
import HcEmotions from '~/components/Emotions/Emotions'
|
import HcEmotions from '~/components/Emotions/Emotions'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -122,29 +122,6 @@ export default {
|
|||||||
this.title = this.post.title
|
this.title = this.post.title
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
async asyncData(context) {
|
|
||||||
const {
|
|
||||||
params,
|
|
||||||
error,
|
|
||||||
app: { apolloProvider, $i18n },
|
|
||||||
} = context
|
|
||||||
const client = apolloProvider.defaultClient
|
|
||||||
const query = PostQuery($i18n)
|
|
||||||
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() {
|
mounted() {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// NOTE: quick fix for jumping flexbox implementation
|
// NOTE: quick fix for jumping flexbox implementation
|
||||||
@ -175,6 +152,19 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
apollo: {
|
||||||
|
Post: {
|
||||||
|
query() {
|
||||||
|
return PostQuery(this.$i18n)
|
||||||
|
},
|
||||||
|
variables() {
|
||||||
|
return {
|
||||||
|
slug: this.$route.params.slug,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fetchPolicy: 'cache-and-network',
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -187,7 +177,6 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.post-card {
|
.post-card {
|
||||||
// max-width: 800px;
|
|
||||||
margin: auto;
|
margin: auto;
|
||||||
|
|
||||||
.comments {
|
.comments {
|
||||||
|
|||||||
@ -254,8 +254,9 @@ import HcEmpty from '~/components/Empty.vue'
|
|||||||
import ContentMenu from '~/components/ContentMenu'
|
import ContentMenu from '~/components/ContentMenu'
|
||||||
import HcUpload from '~/components/Upload'
|
import HcUpload from '~/components/Upload'
|
||||||
import HcAvatar from '~/components/Avatar/Avatar.vue'
|
import HcAvatar from '~/components/Avatar/Avatar.vue'
|
||||||
import PostQuery from '~/graphql/UserProfile/Post.js'
|
import { filterPosts } from '~/graphql/PostQuery'
|
||||||
import UserQuery from '~/graphql/UserProfile/User.js'
|
// import PostQuery from '~/graphql/UserProfile/Post.js'
|
||||||
|
import UserQuery from '~/graphql/User'
|
||||||
import { Block, Unblock } from '~/graphql/settings/BlockedUsers.js'
|
import { Block, Unblock } from '~/graphql/settings/BlockedUsers.js'
|
||||||
|
|
||||||
const tabToFilterMapping = ({ tab, id }) => {
|
const tabToFilterMapping = ({ tab, id }) => {
|
||||||
@ -401,7 +402,7 @@ export default {
|
|||||||
apollo: {
|
apollo: {
|
||||||
Post: {
|
Post: {
|
||||||
query() {
|
query() {
|
||||||
return PostQuery(this.$i18n)
|
return filterPosts(this.$i18n)
|
||||||
},
|
},
|
||||||
variables() {
|
variables() {
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user