8000 GitHub - Xjectro/graphql-apollo-server: A modern GraphQL API server - showcasing best practices for building scalable and maintainable GraphQL services
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Repository files navigation

GraphQL Apollo Server ✨🚀

< 8000 a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/ba9790d8993e444ef470ced67bcc6937bd4f8b4f5d1eb9ce4a16597be158dba6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d4772617068514c2d4531303039383f7374796c653d666f722d7468652d6261646765266c6f676f3d6772617068716c266c6f676f436f6c6f723d7768697465">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: "john.doe@example.com"
      password: "securePassword123"
    }
  ) {
    id
    firstName
    lastName
    email
  }
}

Login

mutation {
  signIn(
    input: { email: "john.doe@example.com", 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
0