Skip to content

A modern GraphQL API server - showcasing best practices for building scalable and maintainable GraphQL services

License

Notifications You must be signed in to change notification settings

Xjectro/graphql-apollo-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

GraphQL Apollo Server โœจ๐Ÿš€

GraphQL TypeScript Prisma PostgreSQL Apollo GraphQL Docker Redis

๐Ÿ—๏ธ A modern GraphQL API server - showcasing best practices for building scalable and maintainable GraphQL services ๐Ÿ—๏ธ

๐Ÿ“‹ Table of Contents

โœจ Features

  • ๐Ÿ”ฎ GraphQL API: Robust GraphQL support with type checking powered by Apollo Server
  • ๐Ÿ“ TypeScript: Fully typed codebase for improved developer experience and code quality
  • ๐Ÿ” Authentication & Authorization: JWT-based authentication with role-based permissions
  • ๐Ÿ—ƒ๏ธ Database Integration: PostgreSQL database with Prisma ORM for type-safe queries
  • โšก Caching: Redis-based caching for improved performance
  • ๐Ÿ“ฆ Docker Support: Containerized deployment for consistent environments
  • ๐Ÿงฉ GraphQL Directives: Custom directives for authentication and authorization control
  • ๐Ÿ“ˆ Scalable Architecture: Service-based architecture for maintainability and scalability

๐Ÿ› ๏ธ Technology Stack

Technology Description
Node.js JavaScript runtime environment
TypeScript Typed JavaScript for better tooling
Apollo Server GraphQL server implementation
Prisma Next-generation TypeScript ORM
PostgreSQL Powerful open-source relational database
Redis In-memory data structure store for caching
Docker Containerization platform
JWT JSON Web Token for authentication

๐Ÿ—๏ธ Architecture

This API follows a layered architecture approach:

Client Request โ†’ GraphQL API โ†’ Resolvers โ†’ Services โ†’ Data Access (Prisma) โ†’ Database
  • ๐Ÿ“Š GraphQL Layer: Handles incoming requests and response formatting
  • ๐Ÿ”„ Resolver Layer: Maps GraphQL operations to service functions
  • ๐Ÿง  Service Layer: Contains business logic and calls data access methods
  • ๐Ÿ’พ Data Access Layer: Interacts with the database via Prisma

๐Ÿ“ Project Structure

graphql/
โ”œโ”€โ”€ docker-compose.yml    # Docker compose configuration
โ”œโ”€โ”€ Dockerfile            # Docker configuration
โ”œโ”€โ”€ prisma/               # Prisma ORM configurations
โ”‚   โ”œโ”€โ”€ schema.prisma     # Database schema
โ”‚   โ””โ”€โ”€ migrations/       # Database migrations
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ graphql/          # GraphQL schemas and resolvers
โ”‚   โ”‚   โ”œโ”€โ”€ directives/   # Custom GraphQL directives
โ”‚   โ”‚   โ”œโ”€โ”€ resolvers/    # GraphQL resolvers
โ”‚   โ”‚   โ””โ”€โ”€ schemas/      # GraphQL type definitions
โ”‚   โ”œโ”€โ”€ models/           # Database client models
โ”‚   โ”‚   โ”œโ”€โ”€ prisma.client.ts  # Prisma client
โ”‚   โ”‚   โ””โ”€โ”€ redis.client.ts   # Redis client
โ”‚   โ”œโ”€โ”€ services/         # Business logic services
โ”‚   โ””โ”€โ”€ types/            # TypeScript type definitions
โ””โ”€โ”€ ...

๐Ÿ“ GraphQL Schema

The GraphQL API provides the following core types and operations:

๐Ÿ“Š Types

# User type
type User {
  id: ID!
  firstName: String!
  lastName: String!
  email: String!
  password: String!
  permission: Int!
  createdAt: String!
  updatedAt: String!
}

# Authentication response type
type AuthenticationResponse {
  token: String
}

# Custom directives
directive @hasPermission(permission: Int!) on FIELD_DEFINITION
directive @authenticated on FIELD_DEFINITION

๐Ÿ” Queries

type Query {
  currentUser: User @authenticated # Returns the currently logged-in user
}

๐Ÿ”„ Mutations

type Mutation {
  createUser(input: CreateUserInput!): CreateUserResponse! # Creates a new user
  authentication(input: AuthenticationInput!): AuthenticationResponse! # Logs in a user and returns a token
}

input CreateUserInput {
  firstName: String!
  lastName: String!
  email: String!
  password: String!
}

input AuthenticationInput {
  email: String!
  password: String!
}

๐Ÿš€ Getting Started

๐Ÿ”ง Prerequisites

โš™๏ธ Installation

  1. Clone the repository:

    git clone https://github.com/Xjectro/graphql-apollo-server
    cd graphql-apollo-server
  2. Install dependencies:

    pnpm install
  3. Generate Prisma client:

    pnpm db:generate

๐Ÿ”‘ Environment Variables

Create a .env file in the root directory:

# Database
DATABASE_URL="postgresql://username:password@localhost:5432/database_name?schema=public"

# Authentication
JWT_SECRET="your-secure-jwt-secret"
JWT_EXPIRES_IN="24h"

# Redis (Optional - for caching)
REDIS_URL="redis://localhost:6379"
ENABLE_CACHE=true

# Server
PORT=4000
NODE_ENV=development

๐Ÿ’ป Development

Run the development server:

pnpm dev

The GraphQL playground will be available at: http://localhost:4000/graphql

๐Ÿ—„๏ธ Database Management

This project uses Prisma for database management. Available commands:

# Create and apply migrations based on schema changes
pnpm db:migrate

# Apply existing migrations to the database
pnpm db:deploy

# Check migration status
pnpm db:status

# Regenerate Prisma client
pnpm db:generate

# Reset database (caution: deletes all data)
pnpm db:reset

# Push schema changes without migrations (for development)
pnpm db:push

๐Ÿญ Production Deployment

Build and start the production server:

pnpm build
pnpm start

๐Ÿณ Docker Deployment

Build and run with Docker Compose:

# Start all services
pnpm docker

# Stop all services
pnpm docker:stop

# View logs
pnpm docker:logs

The Docker setup includes:

  • GraphQL API server
  • PostgreSQL database
  • Redis cache

๐Ÿ“š API Documentation

When the server is running, you can access the GraphQL Playground at http://localhost:4000/graphql, which provides:

  • Interactive query builder
  • Schema documentation
  • Real-time testing of queries and mutations

๐Ÿ“ Example Queries

Create a new user

mutation {
  signUp(
    input: {
      firstName: "John"
      lastName: "Doe"
      email: "[email protected]"
      password: "securePassword123"
    }
  ) {
    id
    firstName
    lastName
    email
  }
}

Login

mutation {
  signIn(
    input: { email: "[email protected]", password: "securePassword123" }
  ) {
    token
  }
}

Get user profile (authentication required)

query {
  currentUser {
    id
    firstName
    lastName
    email
  }
}

๐Ÿ” Note: To use the currentUser query, you need to send a token in your HTTP headers as Authorization: Bearer <token>.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A modern GraphQL API server - showcasing best practices for building scalable and maintainable GraphQL services

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published