Skip to content

MahmoudElfiky-1/GraphQl-Lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL API with Apollo Server and MongoDB

Features

  • ✅ 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

Project Structure

├── 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

Installation

  1. Install dependencies:

    npm install
  2. Start the server:

    npm start

    Or for development with auto-reload:

    npm run dev
  3. Access GraphQL Playground:

    • Open your browser and go to http://localhost:4000
    • You'll see the Apollo GraphQL Playground interface

Data Models

User

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
  createdAt: String!
}

Post

type Post {
  id: ID!
  title: String!
  content: String!
  author: User!
  comments: [Comment!]!
  createdAt: String!
}

Comment

type Comment {
  id: ID!
  text: String!
  author: User!
  post: Post!
  createdAt: String!
}

API Queries

User Queries

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
  }
}

Post Queries

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
  }
}

Comment Queries

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
    }
  }
}

API Mutations

User Mutations

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")
}

Post Mutations

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")
}

Comment Mutations

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")
}

Relationships

The API supports the following relationships:

  1. User → Posts: A user can have multiple posts

    query {
      getUserById(id: "1") {
        posts {
          id
          title
        }
      }
    }
  2. Post → Author: A post belongs to a user

    query {
      getPostById(id: "1") {
        author {
          name
          email
        }
      }
    }
  3. Post → Comments: A post can have multiple comments

    query {
      getPostById(id: "1") {
        comments {
          text
          author {
            name
          }
        }
      }
    }
  4. Comment → Author: A comment belongs to a user

    query {
      getCommentById(id: "1") {
        author {
          name
        }
      }
    }
  5. Comment → Post: A comment belongs to a post

    query {
      getCommentById(id: "1") {
        post {
          title
        }
      }
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors