59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { ApolloServer } from '@apollo/server';
|
|
import { expressMiddleware } from '@apollo/server/express4';
|
|
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
|
|
import cors from 'cors';
|
|
import 'dotenv/config';
|
|
import express from 'express';
|
|
import fs from 'fs';
|
|
import http from 'http';
|
|
import path from 'path';
|
|
import ViteExpress from 'vite-express';
|
|
import { getIdentityContext, IdentityContext } from './identity';
|
|
|
|
import { Mutation, Query } from './graphql/graphql';
|
|
|
|
const __dirname = import.meta.dirname;
|
|
const HOST = process.env.VITE_HOST || 'localhost';
|
|
const PORT = parseInt(process.env.VITE_PORT || '3000');
|
|
const MOTD = `\n\n\n\x1b[35m-server is listening at...-\x1b[0m\n\nhttp://${HOST}:${PORT}\nhttp://localhost:${PORT}\n\n\n`;
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
|
|
const typeDefs = fs.readFileSync(path.join(__dirname, 'graphql', 'schema.graphql'), 'utf8');
|
|
|
|
const resolvers = {
|
|
Query,
|
|
Mutation
|
|
// Subscription,
|
|
};
|
|
|
|
const apollo = new ApolloServer<IdentityContext>({
|
|
typeDefs,
|
|
resolvers,
|
|
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
|
|
formatError: (formattedError, error) => {
|
|
process.stdout.write('\n```\n');
|
|
console.error(`${error}`);
|
|
process.stdout.write('\n```\n');
|
|
|
|
return formattedError;
|
|
}
|
|
});
|
|
|
|
await apollo.start();
|
|
|
|
app.use(
|
|
'/graphql',
|
|
cors<cors.CorsRequest>(),
|
|
express.json(),
|
|
expressMiddleware(apollo, {
|
|
context: getIdentityContext
|
|
})
|
|
);
|
|
|
|
await new Promise<void>(resolve => server.listen({ hostname: HOST, port: PORT }, resolve));
|
|
|
|
process.stdout.write(MOTD);
|
|
|
|
ViteExpress.bind(app, server);
|