68 lines
2 KiB
TypeScript
68 lines
2 KiB
TypeScript
import { ApolloServer } from '@apollo/server';
|
|
import { expressMiddleware } from '@apollo/server/express4';
|
|
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
|
|
import cookies from 'cookie-parser';
|
|
import cors from 'cors';
|
|
import 'dotenv/config';
|
|
import express from 'express';
|
|
import fs from 'fs';
|
|
import { GraphQLJSON } from 'graphql-scalars';
|
|
import https from 'https';
|
|
import path from 'path';
|
|
import ViteExpress from 'vite-express';
|
|
import { toAppConfig } from './config';
|
|
import { refreshTokenController } from './express/auth/refreshToken';
|
|
import { Mutation, Query } from './graphql/graphql';
|
|
import { getIdentityContext, IdentityContext } from './identity';
|
|
|
|
export const config = toAppConfig();
|
|
|
|
const __dirname = import.meta.dirname;
|
|
const key = fs.readFileSync(path.join(__dirname + '../../../certs/selfsigned.key'));
|
|
const cert = fs.readFileSync(path.join(__dirname + '../../../certs/selfsigned.crt'));
|
|
|
|
const app = express();
|
|
const server = https.createServer({ key, cert }, app);
|
|
|
|
const typeDefs = fs.readFileSync(path.join(__dirname, 'graphql', 'schema.graphql'), 'utf8');
|
|
|
|
const resolvers = {
|
|
JSON: GraphQLJSON,
|
|
Query,
|
|
Mutation
|
|
// Subscription,
|
|
};
|
|
|
|
const apollo = new ApolloServer<IdentityContext>({
|
|
typeDefs,
|
|
resolvers,
|
|
introspection: !config.isProd,
|
|
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
|
|
formatError: formattedError => {
|
|
process.stdout.write('\n```\n');
|
|
console.error(formattedError);
|
|
process.stdout.write('\n```\n');
|
|
return formattedError;
|
|
}
|
|
});
|
|
|
|
await apollo.start();
|
|
|
|
app.use(cors<cors.CorsRequest>(config.corsOptions));
|
|
app.use(express.json(config.limit));
|
|
app.use(cookies());
|
|
app.get('/api/refreshAccessToken', refreshTokenController);
|
|
app.use(
|
|
'/graphql',
|
|
expressMiddleware(apollo, {
|
|
context: getIdentityContext
|
|
})
|
|
);
|
|
|
|
await new Promise<void>(resolve =>
|
|
server.listen({ hostname: config.host, port: config.port }, resolve)
|
|
);
|
|
|
|
process.stdout.write(config.greeting);
|
|
|
|
ViteExpress.bind(app, server);
|