forked from w3tecch/express-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphqlLoader.ts
46 lines (38 loc) · 1.84 KB
/
graphqlLoader.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
import * as express from 'express';
import GraphQLHTTP from 'express-graphql';
import { MicroframeworkLoader, MicroframeworkSettings } from 'microframework-w3tec';
import * as path from 'path';
import { buildSchema } from 'type-graphql';
import Container from 'typedi';
import { env } from '../env';
import { getErrorCode, getErrorMessage, handlingErrors } from '../lib/graphql';
export const graphqlLoader: MicroframeworkLoader = async (settings: MicroframeworkSettings | undefined) => {
if (settings && env.graphql.enabled) {
const expressApp = settings.getData('express_app');
const schema = await buildSchema({
resolvers: env.app.dirs.resolvers,
// automatically create `schema.gql` file with schema definition in current folder
emitSchemaFile: path.resolve(__dirname, '../api', 'schema.gql'),
});
handlingErrors(schema);
// Add graphql layer to the express app
expressApp.use(env.graphql.route, (request: express.Request, response: express.Response) => {
// Build GraphQLContext
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); // uuid-like
const container = Container.of(requestId); // get scoped container
const context = { requestId, container, request, response }; // create our context
container.set('context', context); // place context or other data in container
// Setup GraphQL Server
GraphQLHTTP({
schema,
context,
graphiql: env.graphql.editor,
formatError: error => ({
code: getErrorCode(error.message),
message: getErrorMessage(error.message),
path: error.path,
}),
})(request, response);
});
}
};