-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphql.ts
52 lines (44 loc) · 1.51 KB
/
graphql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Server } from 'http';
import { mergeSchemas } from '@graphql-tools/schema';
import {
ApolloServerPluginDrainHttpServer,
ApolloServerPluginLandingPageLocalDefault,
PluginDefinition,
} from 'apollo-server-core';
import { ApolloServer } from 'apollo-server-express';
import { Application } from 'express';
import { env } from '../configs/env';
import resolvers from '../resources/resolvers';
import schemas from '../resources/schemas';
import { AppContext } from '../types/common';
import { CORS_APOLLO_STUDIO_URL } from '../utils/constants';
import { buildGraphQLContext } from './config/build-context';
const explorerPlugin: PluginDefinition[] = env.IS_PROD
? []
: [ApolloServerPluginLandingPageLocalDefault({ embed: true })];
export const startGraphqlServer = async (expressApplication: Application, httpServer: Server) => {
const schemaWithResolvers = mergeSchemas({
resolvers,
typeDefs: schemas,
});
const apolloServer = new ApolloServer({
cache: 'bounded',
context: async ({ req, res }): Promise<AppContext> => buildGraphQLContext(req, res),
debug: env.IS_DEV,
introspection: env.ENABLE_INTROSPECTION,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }), // graceful shutdown
...explorerPlugin,
],
schema: schemaWithResolvers,
});
await apolloServer.start();
apolloServer.applyMiddleware({
app: expressApplication,
cors: {
credentials: true,
origin: [env.WEB_APP_URL, CORS_APOLLO_STUDIO_URL],
},
});
return apolloServer;
};