snowballtools-base/packages/backend/src/server.ts
neerajvijay1997 e4c099f8c3 Refactor database setup and add user context to GQL requests (#17)
* Add context to each request

* Implement get user db query

* Move constants to a separate file

* Refactor database init method
2024-02-01 11:37:57 +05:30

63 lines
1.5 KiB
TypeScript

import debug from 'debug';
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { createServer } from 'http';
import {
ApolloServerPluginDrainHttpServer,
ApolloServerPluginLandingPageLocalDefault
} from 'apollo-server-core';
import { TypeSource } from '@graphql-tools/utils';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { ServerConfig } from './config';
import { DEFAULT_GQL_PATH, USER_ID } from './constants';
const log = debug('snowball:server');
export const createAndStartServer = async (
typeDefs: TypeSource,
resolvers: any,
serverConfig: ServerConfig
): Promise<ApolloServer> => {
const { host, port, gqlPath = DEFAULT_GQL_PATH } = serverConfig;
const app = express();
// Create HTTP server
const httpServer = createServer(app);
// Create the schema
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const server = new ApolloServer({
schema,
csrfPrevention: true,
context: () => {
// TODO: Use userId derived from auth token
return { userId: USER_ID };
},
plugins: [
// Proper shutdown for the HTTP server
ApolloServerPluginDrainHttpServer({ httpServer }),
ApolloServerPluginLandingPageLocalDefault({ embed: true })
]
});
await server.start();
server.applyMiddleware({
app,
path: gqlPath
});
httpServer.listen(port, host, () => {
log(`Server is listening on ${host}:${port}${server.graphqlPath}`);
});
return server;
};