- ✅ Complete GraphQL schema with User, Post, and Comment types
- ✅ Full CRUD operations (Create, Read, Update, Delete) for all entities
- ✅ Relationship queries to fetch related data
- ✅ MongoDB integration for data persistence
- ✅ Error handling and validation
- ✅ Apollo Server with GraphQL Playground
├── src/
│ ├── index.js # Apollo Server setup and entry point
│ ├── schema.js # GraphQL schema definitions
│ ├── resolvers.js # Query and Mutation resolvers
│ └── models.js # MongoDB models (User, Post, Comment)
├── .env # Environment variables
├── package.json # Dependencies and scripts
└── README.md # This file
-
Install dependencies:
npm install
-
Start the server:
npm start
Or for development with auto-reload:
npm run dev
-
Access GraphQL Playground:
- Open your browser and go to
http://localhost:4000 - You'll see the Apollo GraphQL Playground interface
- Open your browser and go to
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
}type Post {
id: ID!
title: String!
content: String!
author: User!
comments: [Comment!]!
createdAt: String!
}type Comment {
id: ID!
text: String!
author: User!
post: Post!
createdAt: String!
}Get all users:
query {
getAllUsers {
id
name
email
createdAt
}
}Get user by ID:
query {
getUserById(id: "1") {
id
name
email
posts {
id
title
}
}
}Get posts by a specific user:
query {
getPostsByUser(userId: "1") {
id
title
content
createdAt
}
}Get all posts:
query {
getAllPosts {
id
title
content
author {
name
email
}
createdAt
}
}Get post by ID:
query {
getPostById(id: "1") {
id
title
content
author {
id
name
email
}
comments {
id
text
author {
name
}
}
}
}Get comments on a specific post:
query {
getCommentsByPost(postId: "1") {
id
text
author {
name
email
}
createdAt
}
}Get all comments:
query {
getAllComments {
id
text
author {
name
}
post {
title
}
createdAt
}
}Get comment by ID:
query {
getCommentById(id: "1") {
id
text
author {
name
email
}
post {
title
}
}
}Create a new user:
mutation {
addUser(name: "John Doe", email: "john@example.com") {
id
name
email
createdAt
}
}Update a user:
mutation {
updateUser(id: "1", name: "Jane Doe", email: "jane@example.com") {
id
name
email
}
}Delete a user:
mutation {
deleteUser(id: "1")
}Create a new post:
mutation {
addPost(
title: "My First Post"
content: "This is the content of my first post."
authorId: "1"
) {
id
title
content
author {
name
}
createdAt
}
}Update a post:
mutation {
updatePost(id: "1", title: "Updated Title", content: "Updated content") {
id
title
content
}
}Delete a post:
mutation {
deletePost(id: "1")
}Create a new comment:
mutation {
addComment(text: "Great post!", authorId: "2", postId: "1") {
id
text
author {
name
}
post {
title
}
createdAt
}
}Update a comment:
mutation {
updateComment(id: "1", text: "Updated comment text") {
id
text
}
}Delete a comment:
mutation {
deleteComment(id: "1")
}The API supports the following relationships:
-
User → Posts: A user can have multiple posts
query { getUserById(id: "1") { posts { id title } } }
-
Post → Author: A post belongs to a user
query { getPostById(id: "1") { author { name email } } }
-
Post → Comments: A post can have multiple comments
query { getPostById(id: "1") { comments { text author { name } } } }
-
Comment → Author: A comment belongs to a user
query { getCommentById(id: "1") { author { name } } }
-
Comment → Post: A comment belongs to a post
query { getCommentById(id: "1") { post { title } } }