UNPKG

5.25 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 express_1 = __importDefault(require("express"));
8const cors_1 = __importDefault(require("cors"));
9const body_parser_1 = require("body-parser");
10const apollo_server_core_1 = require("apollo-server-core");
11const accepts_1 = __importDefault(require("accepts"));
12var apollo_server_core_2 = require("apollo-server-core");
13class ApolloServer extends apollo_server_core_1.ApolloServerBase {
14 async createGraphQLServerOptions(req, res) {
15 const contextParams = { req, res };
16 return super.graphQLServerOptions(contextParams);
17 }
18 applyMiddleware({ app, ...rest }) {
19 this.assertStarted('applyMiddleware');
20 app.use(this.getMiddleware(rest));
21 }
22 getMiddleware({ path, cors, bodyParserConfig, disableHealthCheck, onHealthCheck, __internal_healthCheckPath, } = {}) {
23 if (!path)
24 path = '/graphql';
25 this.assertStarted('getMiddleware');
26 const router = express_1.default.Router();
27 if (!disableHealthCheck && __internal_healthCheckPath !== null) {
28 router.use(__internal_healthCheckPath !== null && __internal_healthCheckPath !== void 0 ? __internal_healthCheckPath : '/.well-known/apollo/server-health', (req, res) => {
29 res.type('application/health+json');
30 if (onHealthCheck) {
31 onHealthCheck(req)
32 .then(() => {
33 res.json({ status: 'pass' });
34 })
35 .catch(() => {
36 res.status(503).json({ status: 'fail' });
37 });
38 }
39 else {
40 res.json({ status: 'pass' });
41 }
42 });
43 }
44 this.graphqlPath = path;
45 if (cors === true) {
46 router.use(path, (0, cors_1.default)());
47 }
48 else if (cors !== false) {
49 router.use(path, (0, cors_1.default)(cors));
50 }
51 if (bodyParserConfig === true) {
52 router.use(path, (0, body_parser_1.json)());
53 }
54 else if (bodyParserConfig !== false) {
55 router.use(path, (0, body_parser_1.json)(bodyParserConfig));
56 }
57 const landingPage = this.getLandingPage();
58 router.use(path, (req, res, next) => {
59 if (landingPage && prefersHtml(req)) {
60 res.setHeader('Content-Type', 'text/html');
61 res.write(landingPage.html);
62 res.end();
63 return;
64 }
65 if (!req.body) {
66 res.status(500);
67 if (bodyParserConfig === false) {
68 res.send('`res.body` is not set; you passed `bodyParserConfig: false`, ' +
69 'but you still need to use `body-parser` middleware yourself.');
70 }
71 else {
72 res.send('`res.body` is not set even though Apollo Server installed ' +
73 "`body-parser` middleware; this shouldn't happen!");
74 }
75 return;
76 }
77 (0, apollo_server_core_1.runHttpQuery)([], {
78 method: req.method,
79 options: () => this.createGraphQLServerOptions(req, res),
80 query: req.method === 'POST' ? req.body : req.query,
81 request: (0, apollo_server_core_1.convertNodeHttpToRequest)(req),
82 }, this.csrfPreventionRequestHeaders).then(({ graphqlResponse, responseInit }) => {
83 if (responseInit.headers) {
84 for (const [name, value] of Object.entries(responseInit.headers)) {
85 res.setHeader(name, value);
86 }
87 }
88 res.statusCode = responseInit.status || 200;
89 if (typeof res.send === 'function') {
90 res.send(graphqlResponse);
91 }
92 else {
93 res.end(graphqlResponse);
94 }
95 }, (error) => {
96 if (!(0, apollo_server_core_1.isHttpQueryError)(error)) {
97 return next(error);
98 }
99 if (error.headers) {
100 for (const [name, value] of Object.entries(error.headers)) {
101 res.setHeader(name, value);
102 }
103 }
104 res.statusCode = error.statusCode;
105 if (typeof res.send === 'function') {
106 res.send(error.message);
107 }
108 else {
109 res.end(error.message);
110 }
111 });
112 });
113 return router;
114 }
115}
116exports.ApolloServer = ApolloServer;
117function prefersHtml(req) {
118 if (req.method !== 'GET') {
119 return false;
120 }
121 const accept = (0, accepts_1.default)(req);
122 const types = accept.types();
123 return (types.find((x) => x === 'text/html' || x === 'application/json') ===
124 'text/html');
125}
126//# sourceMappingURL=ApolloServer.js.map
\No newline at end of file