From dd9383ef40fab2b4b616cde39e5e5bca4c1c72d0 Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Tue, 16 Apr 2019 23:02:57 -0300 Subject: [PATCH] Create two custom resolvers, get working with front end - Had difficulty adding a relationship with one custom resolver, if id for comment was not passed in, the comment was not created, hard coding it in also wasn't a good solution --- backend/src/resolvers/comments.js | 23 ++----- backend/src/resolvers/comments.spec.js | 12 ++-- backend/src/schema.graphql | 1 - backend/src/seed/factories/comments.js | 2 - webapp/graphql/AddPostComments.js | 18 ++++++ webapp/pages/post/_id/_slug/index.vue | 90 +++++++++++++++++--------- 6 files changed, 89 insertions(+), 57 deletions(-) create mode 100644 webapp/graphql/AddPostComments.js diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index 5ab20cf46..4542c075a 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -2,24 +2,11 @@ import { neo4jgraphql } from 'neo4j-graphql-js' export default { Mutation: { - CreateComment: async (object, params, context, resolveInfo) => { - const { postId } = params - - const comment = await neo4jgraphql(object, params, context, resolveInfo, true) - - const session = context.driver.session() - const transactionRes = await session.run(` - MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}) - MERGE (post)<-[:COMMENTS]-(comment) - RETURN comment {.id, .content}`, { - postId, - commentId: comment.id - } - ) - - session.close() - - return comment + CreateComment: (object, params, context, resolveInfo) => { + return neo4jgraphql(object, params, context, resolveInfo, false) + }, + AddPostComments: (object, params, context, resolveInfo) => { + return neo4jgraphql(object, params, context, resolveInfo, false) } } } diff --git a/backend/src/resolvers/comments.spec.js b/backend/src/resolvers/comments.spec.js index e17f94ec9..2fcd7245f 100644 --- a/backend/src/resolvers/comments.spec.js +++ b/backend/src/resolvers/comments.spec.js @@ -19,8 +19,8 @@ afterEach(async () => { describe('CreateComment', () => { const mutation = ` - mutation($id: ID!, $postId: ID!, $content: String!) { - CreateComment(id: $id, postId: $postId, content: $content) { + mutation($id: ID!, $content: String!) { + CreateComment(id: $id, content: $content) { id content } @@ -29,8 +29,7 @@ describe('CreateComment', () => { describe('unauthenticated', () => { it('throws authorization error', async () => { variables = { - id: 'c1', - postId: 'p1', + id: 'c1', content: "I'm not authorised to comment" } client = new GraphQLClient(host) @@ -46,9 +45,8 @@ describe('CreateComment', () => { }) it('creates a post', async () => { - variables = { - id: 'c1', - postId: 'p1', + variables = { + id: 'c1', content: "I'm authorised to comment" } const expected = { diff --git a/backend/src/schema.graphql b/backend/src/schema.graphql index 6c35e082d..94e28d0d7 100644 --- a/backend/src/schema.graphql +++ b/backend/src/schema.graphql @@ -27,7 +27,6 @@ type Mutation { enable(id: ID!): ID reward(fromBadgeId: ID!, toUserId: ID!): ID unreward(fromBadgeId: ID!, toUserId: ID!): ID - CreateComment(id: ID!, postId: ID!, content: String!) : Comment "Shout the given Type and ID" shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """ MATCH (n {id: $id})<-[:WROTE]-(wu:User), (u:User {id: $cypherParams.currentUserId}) diff --git a/backend/src/seed/factories/comments.js b/backend/src/seed/factories/comments.js index 29d0722b7..b6ada7ac9 100644 --- a/backend/src/seed/factories/comments.js +++ b/backend/src/seed/factories/comments.js @@ -4,7 +4,6 @@ import uuid from 'uuid/v4' export default function (params) { const { id = uuid(), - postId = uuid(), content = [ faker.lorem.sentence(), faker.lorem.sentence() @@ -15,7 +14,6 @@ export default function (params) { mutation { CreateComment( id: "${id}", - postId: "${postId}", content: "${content}" ) { id, content } } diff --git a/webapp/graphql/AddPostComments.js b/webapp/graphql/AddPostComments.js new file mode 100644 index 000000000..ab9ef8ca5 --- /dev/null +++ b/webapp/graphql/AddPostComments.js @@ -0,0 +1,18 @@ +import gql from 'graphql-tag' + +export default app => { + return { + AddPostComments: gql(` + mutation($from: _CommentInput!, $to: _PostInput!) { + AddPostComments(from: $from, to: $to) { + from { + id + } + to { + id + } + } + } + `) + } +} diff --git a/webapp/pages/post/_id/_slug/index.vue b/webapp/pages/post/_id/_slug/index.vue index 8af07bc26..ef1121031 100644 --- a/webapp/pages/post/_id/_slug/index.vue +++ b/webapp/pages/post/_id/_slug/index.vue @@ -117,11 +117,11 @@ v-model="value" /> + - + {{ $t('actions.cancel') }} @@ -140,23 +140,23 @@ - -
- -
- + +
+ +
+ @@ -198,7 +198,9 @@ export default { return { post: null, ready: false, - title: 'loading' + title: 'loading', + loading: false, + disabled: false } }, watch: { @@ -315,18 +317,48 @@ export default { return this.$store.getters['auth/user'].id === id }, handleSubmit() { - this.$apollo.mutate({ - mutation: gql` - mutation($content: String!) { - CreateComment(content: $content) { - content + this.loading = true + this.$apollo + .mutate({ + mutation: gql` + mutation($content: String!) { + CreateComment(content: $content) { + id + content + } } + `, + variables: { + content: this.value } - `, - variables: { - content: this.value - } - }) + }) + .then(res => { + this.disabled = true + this.loading = false + const { id } = res.data.CreateComment + const commentId = { id: id } + const postId = { id: this.post.id } + const AddPostComments = require('~/graphql/AddPostComments.js').default( + this + ) + + this.$apollo + .mutate({ + mutation: AddPostComments.AddPostComments, + variables: { + from: commentId, + to: postId + } + }) + .then(res => { + this.$toast.success('Saved!') + }) + }) + .catch(err => { + this.$toast.error(err.message) + this.loading = false + this.disabled = false + }) } } }