This repository contains a GraphQL API service built with PostGraphile, providing a thin API layer on top of PostgreSQL. The service handles authentication via JWT tokens (compatible with Supabase) and provides dynamic schema generation during development.
The backend is built using:
- PostGraphile: Auto-generates a GraphQL API from your PostgreSQL schema
- Express: Web framework for handling HTTP requests
- JWT: Token-based authentication compatible with Supabase
- Docker: Containerization for consistent deployment
- Node.js (v18+)
- PostgreSQL database
- Docker (for deployment)
DATABASE_URL=postgresql://user:password@host:5432/dbname
JWT_SECRET=your_jwt_secret
PORT=5001 (default)
CORS_ORIGIN=* (default)
- Install dependencies:
npm install
- Start in development mode:
npm run start:watch
This will run the server in watch mode, which:
- Automatically restarts on file changes
- Regenerates the GraphQL schema (
schemas/schema.graphql
) - Enables GraphiQL interface for API exploration
- Provides detailed error messages and debugging information
The service uses JWT tokens for authentication, compatible with Supabase's auth system. The JWT middleware:
- Extracts the JWT token from the Authorization header
- Verifies the token signature using
JWT_SECRET
- Sets PostgreSQL role and user context based on token claims
- Provides role-based access control through PostgreSQL policies
- GraphiQL Interface: Available at
/api/v1/graphiql
for API exploration - Dynamic JSON: Native JSON type handling
- Query Batching: Optimized multiple query execution
- Subscriptions: Real-time data updates (enabled but requires additional setup)
- Simplified Schema: Uses
@graphile-contrib/pg-simplify-inflector
for cleaner naming
The service is deployed using GitHub Actions and Docker. The workflow:
- Builds Docker image
- Transfers image to deployment server
- Runs container with proper configuration
Configure these secrets in GitHub repository settings:
SSH_PRIVATE_KEY
: SSH key for server accessSERVER_HOST
: Deployment server hostnameSERVER_USER
: SSH user for deploymentDATABASE_URL
: PostgreSQL connection stringJWT_SECRET
: Secret for JWT verification
The container runs with:
- Port 5001 exposed
- Automatic restart policy
- Health checks every 30s
- Log rotation (10MB max, 3 files)
- Memory limits and resource constraints
npm start
: Run in development modenpm run start:watch
: Run with auto-reloadnpm run ts
: Run TypeScript type checking in watch modenpm run ts:check
: One-time TypeScript checknpm run format
: Format code using Prettiernpm run format:check
: Check code formatting
The GraphQL schema is available through the GraphiQL interface at /api/v1/graphiql
. In development mode, the schema is also exported to schemas/schema.graphql
for version control and frontend consumption.