diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 4ecbf5116..5306c18d7 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -73,12 +73,13 @@ export default { }, AddPostEmotions: async (object, params, context, resolveInfo) => { const session = context.driver.session() - const { from, to, data } = params + const { to, data } = params + const { user } = context const transactionRes = await session.run( - `MATCH (userFrom:User {id: $from.id}), (postTo:Post {id: $to.id}) + `MATCH (userFrom:User {id: $user.id}), (postTo:Post {id: $to.id}) MERGE (userFrom)-[emotedRelation:EMOTED {emotion: $data.emotion}]->(postTo) RETURN userFrom, postTo, emotedRelation`, - { from, to, data }, + { user, to, data }, ) session.close() const [emoted] = transactionRes.records.map(record => { diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 997291264..083283689 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -1,7 +1,7 @@ import { GraphQLClient } from 'graphql-request' import { createTestClient } from 'apollo-server-testing' import Factory from '../../seed/factories' -import { host, login } from '../../jest/helpers' +import { host, login, gql } from '../../jest/helpers' import { neode, getDriver } from '../../bootstrap/neo4j' import createServer from '../../server' @@ -20,7 +20,7 @@ const oldContent = 'Old content' const newTitle = 'New title' const newContent = 'New content' const createPostVariables = { title: postTitle, content: postContent } -const createPostWithCategoriesMutation = ` +const createPostWithCategoriesMutation = gql` mutation($title: String!, $content: String!, $categoryIds: [ID]) { CreatePost(title: $title, content: $content, categoryIds: $categoryIds) { id @@ -33,7 +33,7 @@ const createPostWithCategoriesVariables = { content: postContent, categoryIds: ['cat9', 'cat4', 'cat15'], } -const postQueryWithCategories = ` +const postQueryWithCategories = gql` query($id: ID) { Post(id: $id) { categories { @@ -47,9 +47,9 @@ const createPostWithoutCategoriesVariables = { content: 'I should be able to filter it out', categoryIds: null, } -const postQueryFilteredByCategory = ` -query Post($filter: _PostFilter) { - Post(filter: $filter) { +const postQueryFilteredByCategory = gql` + query Post($filter: _PostFilter) { + Post(filter: $filter) { title id categories { @@ -63,7 +63,7 @@ const postQueryFilteredByCategoryVariables = { filter: postCategoriesFilterParam, } -const createPostMutation = ` +const createPostMutation = gql` mutation($title: String!, $content: String!) { CreatePost(title: $title, content: $content) { id @@ -126,13 +126,15 @@ describe('CreatePost', () => { it('assigns the authenticated user as author', async () => { await client.request(createPostMutation, createPostVariables) const { User } = await client.request( - `{ - User(name: "TestUser") { - contributions { - title + gql` + { + User(name: "TestUser") { + contributions { + title + } } } - }`, + `, { headers }, ) expect(User).toEqual([{ contributions: [{ title: postTitle }] }]) @@ -149,7 +151,7 @@ describe('CreatePost', () => { describe('language', () => { it('allows a user to set the language of the post', async () => { - const createPostWithLanguageMutation = ` + const createPostWithLanguageMutation = gql` mutation($title: String!, $content: String!, $language: String) { CreatePost(title: $title, content: $content, language: $language) { language @@ -237,7 +239,7 @@ describe('UpdatePost', () => { title: oldTitle, content: oldContent, }) - updatePostMutation = ` + updatePostMutation = gql` mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) { UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) { id @@ -343,7 +345,7 @@ describe('UpdatePost', () => { }) describe('DeletePost', () => { - const mutation = ` + const mutation = gql` mutation($id: ID!) { DeletePost(id: $id) { id @@ -416,27 +418,31 @@ describe('emotions', () => { } } ` - const PostsEmotionsQuery = ` - query($id: ID!) { - Post(id: $id) { - emotions { - emotion - User { - id + const PostsEmotionsQuery = gql` + query($id: ID!) { + Post(id: $id) { + emotions { + emotion + User { + id + } } } } - } -` - const addPostEmotionsMutation = ` - mutation($from: _UserInput!, $to: _PostInput!, $data: _EMOTEDInput!) { - AddPostEmotions(from: $from, to: $to, data: $data) { - from { id } - to { id } - emotion + ` + const addPostEmotionsMutation = gql` + mutation($to: _PostInput!, $data: _EMOTEDInput!) { + AddPostEmotions(to: $to, data: $data) { + from { + id + } + to { + id + } + emotion + } } - } -` + ` beforeEach(async () => { userParams.id = 'u1987' authorParams.id = 'u257' @@ -478,7 +484,6 @@ describe('emotions', () => { return query({ query: postQuery, variables }) } addPostEmotionsVariables = { - from: { id: authorParams.id }, to: { id: postToEmote.id }, data: { emotion: 'happy' }, } @@ -509,11 +514,10 @@ describe('emotions', () => { }) it('adds an emotion to the post', async () => { - addPostEmotionsVariables.from.id = userParams.id const expected = { data: { AddPostEmotions: { - from: addPostEmotionsVariables.from, + from: { id: user.id }, to: addPostEmotionsVariables.to, emotion: 'happy', }, @@ -543,8 +547,8 @@ describe('emotions', () => { it('allows a user to add more than one emotion', async () => { const expectedEmotions = [ - { emotion: 'happy', User: { id: authorParams.id } }, - { emotion: 'surprised', User: { id: authorParams.id } }, + { emotion: 'happy', User: { id: user.id } }, + { emotion: 'surprised', User: { id: user.id } }, ] const expectedResponse = { data: { Post: [{ emotions: expect.arrayContaining(expectedEmotions) }] }, @@ -567,7 +571,7 @@ describe('emotions', () => { const expected = { data: { AddPostEmotions: { - from: addPostEmotionsVariables.from, + from: { id: owner.id }, to: addPostEmotionsVariables.to, emotion: 'happy', }, @@ -582,7 +586,7 @@ describe('emotions', () => { describe('RemovePostEmotions', () => { let removePostEmotionsVariables, postsEmotionsQueryVariables - const removePostEmotionsMutation = ` + const removePostEmotionsMutation = gql` mutation($to: _PostInput!, $data: _EMOTEDInput!) { RemovePostEmotions(to: $to, data: $data) } @@ -650,13 +654,13 @@ describe('emotions', () => { let PostsEmotionsCountByEmotionVariables let PostsEmotionsByCurrentUserVariables - const PostsEmotionsCountByEmotionQuery = ` + const PostsEmotionsCountByEmotionQuery = gql` query($postId: ID!, $data: _EMOTEDInput!) { - PostsEmotionsCountByEmotion(postId: $postId, data: $data) + PostsEmotionsCountByEmotion(postId: $postId, data: $data) } - ` + ` - const PostsEmotionsByCurrentUserQuery = ` + const PostsEmotionsByCurrentUserQuery = gql` query($postId: ID!) { PostsEmotionsByCurrentUser(postId: $postId) } diff --git a/backend/src/schema/types/type/Post.gql b/backend/src/schema/types/type/Post.gql index b5265203d..28329ee85 100644 --- a/backend/src/schema/types/type/Post.gql +++ b/backend/src/schema/types/type/Post.gql @@ -91,7 +91,7 @@ type Mutation { language: String categoryIds: [ID] ): Post - AddPostEmotions(from: _UserInput!, to: _PostInput!, data: _EMOTEDInput!): EMOTED + AddPostEmotions(to: _PostInput!, data: _EMOTEDInput!): EMOTED RemovePostEmotions(to: _PostInput!, data: _EMOTEDInput!): Boolean! } diff --git a/backend/src/seed/factories/index.js b/backend/src/seed/factories/index.js index 87d99faab..df3886a6c 100644 --- a/backend/src/seed/factories/index.js +++ b/backend/src/seed/factories/index.js @@ -128,11 +128,10 @@ export default function Factory(options = {}) { this.lastResponse = await cleanDatabase({ driver: this.neo4jDriver }) return this }, - async emote({ from, to, data }) { + async emote({ to, data }) { const mutation = ` mutation { AddPostEmotions( - from: { id: "${from}" }, to: { id: "${to}" }, data: { emotion: ${data} } ) { diff --git a/webapp/graphql/PostMutations.js b/webapp/graphql/PostMutations.js index 4439847c0..3b94e809c 100644 --- a/webapp/graphql/PostMutations.js +++ b/webapp/graphql/PostMutations.js @@ -61,8 +61,8 @@ export default () => { } `, AddPostEmotionsMutation: gql` - mutation($from: _UserInput!, $to: _PostInput!, $data: _EMOTEDInput!) { - AddPostEmotions(from: $from, to: $to, data: $data) { + mutation($to: _PostInput!, $data: _EMOTEDInput!) { + AddPostEmotions(to: $to, data: $data) { emotion from { id