Skip to content

A comprehensive GraphQL API built with Apollo Server, Express.js, and PostgreSQL for the CodeVa Internship Program.

Notifications You must be signed in to change notification settings

Manaf-Yakubu/GraphQL-API_Apollo-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CodeVa GraphQL API - Advanced Task 3

A comprehensive GraphQL API built with Apollo Server, Express.js, and PostgreSQL for the CodeVa Internship Program. Demo

πŸš€ Features

Core GraphQL Features

  • Apollo Server 4 with Express integration
  • Comprehensive Schema with queries, mutations, and subscriptions
  • Type-safe resolvers with proper error handling
  • Real-time subscriptions for posts and comments
  • GraphQL Playground for API exploration (development mode)

Authentication & Authorization

  • JWT-based authentication with access and refresh tokens
  • Role-based access control (USER, ADMIN, MODERATOR)
  • Secure password hashing with bcrypt
  • Token validation middleware for protected operations

Database Integration

  • PostgreSQL with Sequelize ORM
  • Database migrations for schema management
  • Comprehensive models with associations and validations
  • Database indexing for optimal performance
  • Seed data for development and testing

Performance Optimization

  • DataLoader for efficient database queries
  • Query batching to prevent N+1 problems
  • Database connection pooling
  • Proper indexing strategy

Security Features

  • Rate limiting on GraphQL endpoints
  • CORS protection with configurable origins
  • Helmet.js for security headers
  • Input validation and sanitization
  • SQL injection prevention

Developer Experience

  • Comprehensive error handling with proper GraphQL errors
  • Development hot reload with nodemon
  • Environment configuration with dotenv
  • Health check endpoint for monitoring
  • Graceful shutdown handling

πŸ“‹ Prerequisites

  • Node.js (v16 or higher)
  • PostgreSQL (v12 or higher)
  • npm or yarn package manager

πŸ› οΈ Installation

  1. Clone and navigate to the project:

    cd c:\Users\AMN21\codeva_Internship\Advanced\Task_3
  2. Install dependencies:

    npm install
  3. Set up environment variables:

    cp .env.example .env

    Update the .env file with your database credentials:

    DB_HOST=localhost
    DB_PORT=5432
    DB_NAME=codeva_graphql_db
    DB_USER=postgres
    DB_PASSWORD=your_password
    JWT_SECRET=your_jwt_secret
    JWT_REFRESH_SECRET=your_refresh_secret
  4. Create PostgreSQL database:

    CREATE DATABASE codeva_graphql_db;
  5. Run database migrations:

    npm run db:migrate
  6. Seed the database with sample data:

    npm run db:seed

πŸš€ Usage

Development Mode

npm run dev

Production Mode

npm start

The GraphQL server will be available at:

πŸ“Š GraphQL Schema

Types

User

type User {
  id: ID!
  username: String!
  email: String!
  firstName: String!
  lastName: String!
  fullName: String!
  role: UserRole!
  isActive: Boolean!
  posts: [Post!]!
  comments: [Comment!]!
}

Post

type Post {
  id: ID!
  title: String!
  content: String!
  slug: String!
  status: PostStatus!
  author: User!
  category: Category
  tags: [Tag!]!
  comments: [Comment!]!
  viewCount: Int!
}

Comment

type Comment {
  id: ID!
  content: String!
  status: CommentStatus!
  author: User!
  post: Post!
  parent: Comment
  replies: [Comment!]!
}

Key Queries

Authentication

# Get current user
query Me {
  me {
    id
    username
    email
    role
  }
}

Posts

# Get paginated posts with filters
query Posts($filters: PostFilters, $pagination: PaginationInput) {
  posts(filters: $filters, pagination: $pagination) {
    posts {
      id
      title
      excerpt
      author {
        username
      }
      category {
        name
      }
    }
    pagination {
      currentPage
      totalPages
      totalItems
    }
  }
}

Key Mutations

Authentication

# Register new user
mutation Register($input: RegisterInput!) {
  register(input: $input) {
    token
    refreshToken
    user {
      id
      username
      email
    }
  }
}

# Login user
mutation Login($input: LoginInput!) {
  login(input: $input) {
    token
    refreshToken
    user {
      id
      username
      role
    }
  }
}

Content Management

# Create new post
mutation CreatePost($input: CreatePostInput!) {
  createPost(input: $input) {
    id
    title
    slug
    status
    author {
      username
    }
  }
}

Subscriptions

# Subscribe to new posts
subscription PostAdded {
  postAdded {
    id
    title
    author {
      username
    }
  }
}

# Subscribe to new comments on a post
subscription CommentAdded($postId: ID!) {
  commentAdded(postId: $postId) {
    id
    content
    author {
      username
    }
  }
}

πŸ” Authentication

Demo Accounts

The seeded database includes these demo accounts:

Role Email Password Username
ADMIN admin@codeva.com Admin123! admin
MODERATOR manaf@codeva.com User123! yakubu_manaf
USER john@example.com User123! john_doe
USER jane@example.com User123! jane_smith

Using Authentication

  1. Login to get tokens:

    mutation {
      login(input: { email: "admin@codeva.com", password: "Admin123!" }) {
        token
        refreshToken
        user { id username role }
      }
    }
  2. Include token in headers:

    {
      "Authorization": "Bearer YOUR_JWT_TOKEN"
    }

πŸ§ͺ Testing

Run Tests

npm test

Test Coverage

npm run test:coverage

Manual Testing with GraphQL Playground

  1. Start the development server: npm run dev
  2. Open http://localhost:4000/graphql in your browser
  3. Use the demo accounts to test authentication
  4. Explore the schema documentation in the playground

πŸ“ Project Structure

src/
β”œβ”€β”€ config/
β”‚   └── database.js          # Database configuration
β”œβ”€β”€ migrations/              # Database migrations
β”œβ”€β”€ models/                  # Sequelize models
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ User.js
β”‚   β”œβ”€β”€ Post.js
β”‚   β”œβ”€β”€ Comment.js
β”‚   β”œβ”€β”€ Category.js
β”‚   β”œβ”€β”€ Tag.js
β”‚   └── PostTag.js
β”œβ”€β”€ resolvers/               # GraphQL resolvers
β”‚   β”œβ”€β”€ index.js
β”‚   β”œβ”€β”€ authResolvers.js
β”‚   β”œβ”€β”€ userResolvers.js
β”‚   β”œβ”€β”€ postResolvers.js
β”‚   β”œβ”€β”€ commentResolvers.js
β”‚   β”œβ”€β”€ categoryResolvers.js
β”‚   β”œβ”€β”€ tagResolvers.js
β”‚   └── statsResolvers.js
β”œβ”€β”€ schema/
β”‚   └── typeDefs.js          # GraphQL schema definitions
β”œβ”€β”€ seeders/                 # Database seeders
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ auth.js              # Authentication utilities
β”‚   └── dataLoader.js        # DataLoader for optimization
└── server.js                # Main server file

πŸ”§ Configuration

Environment Variables

  • PORT: Server port (default: 4000)
  • NODE_ENV: Environment (development/production)
  • DB_*: Database connection settings
  • JWT_SECRET: JWT signing secret
  • JWT_REFRESH_SECRET: Refresh token secret
  • CORS_ORIGIN: Allowed CORS origin

Database Scripts

  • npm run db:migrate: Run migrations
  • npm run db:seed: Run seeders
  • npm run db:reset: Reset database (drop, create, migrate, seed)

πŸš€ Deployment

Production Checklist

  1. Set NODE_ENV=production
  2. Use strong JWT secrets
  3. Configure proper CORS origins
  4. Set up SSL/TLS certificates
  5. Configure database connection pooling
  6. Set up monitoring and logging
  7. Configure rate limiting appropriately

Docker Support (Optional)

FROM node:16-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 4000
CMD ["npm", "start"]

πŸ“ˆ Performance Considerations

DataLoader Implementation

  • Batches database queries to prevent N+1 problems
  • Caches results within a single request
  • Automatically handles query deduplication

Database Optimization

  • Proper indexing on frequently queried fields
  • Connection pooling for concurrent requests
  • Efficient association loading with Sequelize

Security Best Practices

  • JWT token expiration and refresh mechanism
  • Rate limiting to prevent abuse
  • Input validation and sanitization
  • SQL injection prevention with parameterized queries

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

πŸ“ License

This project is part of the CodeVa Internship Program and is for educational purposes.

πŸ‘¨β€πŸ’» Author

Yakubu Abdul Manaf

πŸ™ Acknowledgments

  • CodeVa Internship Program
  • Apollo GraphQL Team
  • Sequelize ORM Team
  • PostgreSQL Community

Built with ❀️ in Ghana for the CodeVa Internship Program

About

A comprehensive GraphQL API built with Apollo Server, Express.js, and PostgreSQL for the CodeVa Internship Program.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published