Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
hfpp2012
/
merng
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit d0e2e134
authored
Sep 18, 2019
by
qiuzhi99
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
createComment deleteComment likePost
1 parent
9b4cad9d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
2 deletions
graphql/resolvers/comments.js
graphql/resolvers/index.js
graphql/resolvers/posts.js
graphql/typeDefs.js
graphql/resolvers/comments.js
0 → 100644
View file @
d0e2e13
const
Post
=
require
(
"../../models/Post"
);
const
checkAuth
=
require
(
"../../util/check-auth"
);
const
{
AuthenticationError
,
UserInputError
}
=
require
(
"apollo-server"
);
module
.
exports
=
{
Mutation
:
{
createComment
:
async
(
_
,
{
postId
,
body
},
context
)
=>
{
const
{
username
}
=
checkAuth
(
context
);
if
(
body
.
trim
()
===
""
)
{
throw
new
UserInputError
(
"Empty comment"
,
{
errors
:
{
body
:
"Comment body must not empty"
}
});
}
const
post
=
await
Post
.
findById
(
postId
);
if
(
post
)
{
post
.
comments
.
unshift
({
body
,
username
,
createdAt
:
new
Date
().
toISOString
()
});
await
post
.
save
();
return
post
;
}
else
throw
new
UserInputError
(
"Post not found"
);
},
deleteComment
:
async
(
_
,
{
postId
,
commentId
},
context
)
=>
{
const
{
username
}
=
checkAuth
(
context
);
const
post
=
await
Post
.
findById
(
postId
);
if
(
post
)
{
const
commentIndex
=
post
.
comments
.
findIndex
(
c
=>
c
.
id
===
commentId
);
if
(
post
.
comments
[
commentIndex
].
username
===
username
)
{
post
.
comments
.
splice
(
commentIndex
,
1
);
await
post
.
save
();
return
post
;
}
else
{
throw
new
AuthenticationError
(
"Action not allowed"
);
}
}
else
{
throw
new
UserInputError
(
"Post not found"
);
}
}
}
};
graphql/resolvers/index.js
View file @
d0e2e13
const
postsResolvers
=
require
(
"./posts"
);
const
usersResolvers
=
require
(
"./users"
);
const
commentsResolvers
=
require
(
"./comments"
);
module
.
exports
=
{
Query
:
{
...
...
@@ -7,6 +8,7 @@ module.exports = {
},
Mutation
:
{
...
usersResolvers
.
Mutation
,
...
postsResolvers
.
Mutation
...
postsResolvers
.
Mutation
,
...
commentsResolvers
.
Mutation
}
};
graphql/resolvers/posts.js
View file @
d0e2e13
const
Post
=
require
(
"../../models/Post"
);
const
checkAuth
=
require
(
"../../util/check-auth"
);
const
{
AuthenticationError
}
=
require
(
"apollo-server"
);
const
{
AuthenticationError
,
UserInputError
}
=
require
(
"apollo-server"
);
module
.
exports
=
{
Query
:
{
...
...
@@ -55,6 +55,27 @@ module.exports = {
}
catch
(
err
)
{
throw
new
Error
(
err
);
}
},
async
likePost
(
_
,
{
postId
},
context
)
{
const
{
username
}
=
checkAuth
(
context
);
const
post
=
await
Post
.
findById
(
postId
);
if
(
post
)
{
if
(
post
.
likes
.
find
(
like
=>
like
.
username
===
username
))
{
post
.
likes
=
post
.
likes
.
filter
(
like
=>
like
.
username
!==
username
);
}
else
{
post
.
likes
.
push
({
username
,
createdAt
:
new
Date
().
toISOString
()
});
}
await
post
.
save
();
return
post
;
}
else
{
throw
new
UserInputError
(
"Post not found"
);
}
}
}
};
graphql/typeDefs.js
View file @
d0e2e13
...
...
@@ -6,6 +6,21 @@ module.exports = gql`
body: String!
createdAt: String!
username: String!
comments: [Comment]!
likes: [Like]!
}
type Comment {
id: ID!
body: String!
username: String!
createdAt: String!
}
type Like {
id: ID!
username: String!
createdAt: String!
}
type Query {
...
...
@@ -33,5 +48,8 @@ module.exports = gql`
login(username: String!, password: String!): User!
createPost(body: String!): Post!
deletePost(postId: ID!): String!
createComment(postId: ID!, body: String!): Post!
deleteComment(postId: ID!, commentId: ID!): Post!
likePost(postId: ID!): Post!
}
`
;
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment