Scaffold Apollo backend domain API
This commit is contained in:
50
src/server.js
Normal file
50
src/server.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'dotenv/config';
|
||||
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
import bodyParser from 'body-parser';
|
||||
import cors from 'cors';
|
||||
import express from 'express';
|
||||
import { ApolloServer } from '@apollo/server';
|
||||
import { expressMiddleware } from '@as-integrations/express5';
|
||||
|
||||
import { buildContext } from './context.js';
|
||||
import { prisma } from './prisma-client.js';
|
||||
import { resolvers } from './resolvers.js';
|
||||
|
||||
const typeDefs = readFileSync(new URL('./schema.graphql', import.meta.url), 'utf8');
|
||||
|
||||
const server = new ApolloServer({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
});
|
||||
|
||||
await server.start();
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
app.use(bodyParser.json({ limit: '1mb' }));
|
||||
|
||||
app.get('/healthz', (_, res) => {
|
||||
res.json({ status: 'ok' });
|
||||
});
|
||||
|
||||
app.use(
|
||||
'/graphql',
|
||||
expressMiddleware(server, {
|
||||
context: async ({ req }) => buildContext(req),
|
||||
}),
|
||||
);
|
||||
|
||||
const port = Number(process.env.PORT ?? 4000);
|
||||
app.listen(port, () => {
|
||||
console.log(`apollo-backend running at http://localhost:${port}/graphql`);
|
||||
});
|
||||
|
||||
async function shutdown() {
|
||||
await server.stop();
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
|
||||
process.on('SIGINT', shutdown);
|
||||
process.on('SIGTERM', shutdown);
|
||||
Reference in New Issue
Block a user