UNPKG

4.87 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.ApolloServer = void 0;
7const cors_1 = __importDefault(require("@koa/cors"));
8const koa_bodyparser_1 = __importDefault(require("koa-bodyparser"));
9const koa_compose_1 = __importDefault(require("koa-compose"));
10const apollo_server_core_1 = require("apollo-server-core");
11const accepts_1 = __importDefault(require("accepts"));
12var apollo_server_core_2 = require("apollo-server-core");
13const middlewareFromPath = (path, middleware) => (ctx, next) => {
14 if (ctx.path === path || ctx.path === `${path}/`) {
15 return middleware(ctx, next);
16 }
17 else {
18 return next();
19 }
20};
21class ApolloServer extends apollo_server_core_1.ApolloServerBase {
22 async createGraphQLServerOptions(ctx) {
23 return super.graphQLServerOptions({ ctx });
24 }
25 applyMiddleware({ app, ...rest }) {
26 this.assertStarted('applyMiddleware');
27 app.use(this.getMiddleware(rest));
28 }
29 getMiddleware({ path, cors, bodyParserConfig, disableHealthCheck, onHealthCheck, } = {}) {
30 if (!path)
31 path = '/graphql';
32 this.assertStarted('getMiddleware');
33 const middlewares = [];
34 if (!disableHealthCheck) {
35 middlewares.push(middlewareFromPath('/.well-known/apollo/server-health', async (ctx) => {
36 ctx.set('Content-Type', 'application/health+json');
37 if (onHealthCheck) {
38 try {
39 await onHealthCheck(ctx);
40 ctx.body = { status: 'pass' };
41 }
42 catch (e) {
43 ctx.status = 503;
44 ctx.body = { status: 'fail' };
45 }
46 }
47 else {
48 ctx.body = { status: 'pass' };
49 }
50 }));
51 }
52 this.graphqlPath = path;
53 if (cors === true || cors === undefined) {
54 middlewares.push(middlewareFromPath(path, (0, cors_1.default)({ origin: '*' })));
55 }
56 else if (cors !== false) {
57 middlewares.push(middlewareFromPath(path, (0, cors_1.default)(cors)));
58 }
59 if (bodyParserConfig === true) {
60 middlewares.push(middlewareFromPath(path, (0, koa_bodyparser_1.default)()));
61 }
62 else if (bodyParserConfig !== false) {
63 middlewares.push(middlewareFromPath(path, (0, koa_bodyparser_1.default)(bodyParserConfig)));
64 }
65 const landingPage = this.getLandingPage();
66 middlewares.push(middlewareFromPath(path, async (ctx) => {
67 if (ctx.request.method === 'OPTIONS') {
68 ctx.status = 204;
69 ctx.body = '';
70 return;
71 }
72 if (landingPage && ctx.request.method === 'GET') {
73 const accept = (0, accepts_1.default)(ctx.req);
74 const types = accept.types();
75 const prefersHtml = types.find((x) => x === 'text/html' || x === 'application/json') === 'text/html';
76 if (prefersHtml) {
77 ctx.set('Content-Type', 'text/html');
78 ctx.body = landingPage.html;
79 return;
80 }
81 }
82 try {
83 const { graphqlResponse, responseInit } = await (0, apollo_server_core_1.runHttpQuery)([ctx], {
84 method: ctx.request.method,
85 options: () => this.createGraphQLServerOptions(ctx),
86 query: ctx.request.method === 'POST'
87 ?
88 ctx.request.body || ctx.req.body
89 : ctx.request.query,
90 request: (0, apollo_server_core_1.convertNodeHttpToRequest)(ctx.req),
91 }, this.csrfPreventionRequestHeaders);
92 if (responseInit.headers) {
93 Object.entries(responseInit.headers).forEach(([headerName, value]) => ctx.set(headerName, value));
94 }
95 ctx.body = graphqlResponse;
96 ctx.status = responseInit.status || 200;
97 }
98 catch (error) {
99 if (!(0, apollo_server_core_1.isHttpQueryError)(error)) {
100 throw error;
101 }
102 if (error.headers) {
103 Object.entries(error.headers).forEach(([headerName, value]) => ctx.set(headerName, value));
104 }
105 ctx.status = error.statusCode;
106 ctx.body = error.message;
107 }
108 }));
109 return (0, koa_compose_1.default)(middlewares);
110 }
111}
112exports.ApolloServer = ApolloServer;
113//# sourceMappingURL=ApolloServer.js.map
\No newline at end of file