What Is GraphQL?
GraphQL is a query language for APIs that lets clients request exactly the data they need. Setting up a GraphQL server on your Breeze provides a flexible, efficient API layer for your applications.
Prerequisites
- A Breeze running Ubuntu 22.04 or later
- Node.js 18+ installed
- Nginx installed
Step 1: Initialize the Project
mkdir /var/www/graphql-api && cd /var/www/graphql-api
npm init -y
npm install @apollo/server graphqlStep 2: Create the Server
Create index.js:
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
const typeDefs = `#graphql
type Query {
hello: String
users: [User]
}
type User {
id: ID
name: String
email: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello from GraphQL!',
users: () => [
{ id: 1, name: 'Alice', email: 'alice@example.com' }
],
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });
console.log(`GraphQL server ready at ${url}`);Step 3: Run in Production
pm2 start index.js --name "graphql-api"
pm2 save && pm2 startupStep 4: Nginx Reverse Proxy
server {
listen 80;
server_name api.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:4000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}sudo ln -s /etc/nginx/sites-available/graphql /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d api.yourdomain.comNext Steps
Connect a database with Prisma or TypeORM, add authentication middleware, and use @apollo/server/plugin for caching and rate limiting.