1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __rest = (this && this.__rest) || function (s, e) {
|
12 | var t = {};
|
13 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
14 | t[p] = s[p];
|
15 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
16 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
17 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
18 | t[p[i]] = s[p[i]];
|
19 | }
|
20 | return t;
|
21 | };
|
22 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
23 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | exports.ApolloServer = void 0;
|
27 | const express_1 = __importDefault(require("express"));
|
28 | const cors_1 = __importDefault(require("cors"));
|
29 | const body_parser_1 = require("body-parser");
|
30 | const graphql_playground_html_1 = require("@apollographql/graphql-playground-html");
|
31 | const apollo_server_core_1 = require("apollo-server-core");
|
32 | const accepts_1 = __importDefault(require("accepts"));
|
33 | const type_is_1 = __importDefault(require("type-is"));
|
34 | const expressApollo_1 = require("./expressApollo");
|
35 | var apollo_server_core_2 = require("apollo-server-core");
|
36 | Object.defineProperty(exports, "GraphQLExtension", { enumerable: true, get: function () { return apollo_server_core_2.GraphQLExtension; } });
|
37 | const fileUploadMiddleware = (uploadsConfig, server) => (req, res, next) => {
|
38 | if (typeof apollo_server_core_1.processFileUploads === 'function' &&
|
39 | type_is_1.default(req, ['multipart/form-data'])) {
|
40 | apollo_server_core_1.processFileUploads(req, res, uploadsConfig)
|
41 | .then(body => {
|
42 | req.body = body;
|
43 | next();
|
44 | })
|
45 | .catch(error => {
|
46 | if (error.status && error.expose)
|
47 | res.status(error.status);
|
48 | next(apollo_server_core_1.formatApolloErrors([error], {
|
49 | formatter: server.requestOptions.formatError,
|
50 | debug: server.requestOptions.debug,
|
51 | }));
|
52 | });
|
53 | }
|
54 | else {
|
55 | next();
|
56 | }
|
57 | };
|
58 | class ApolloServer extends apollo_server_core_1.ApolloServerBase {
|
59 | constructor(config) {
|
60 | super(config);
|
61 | }
|
62 | createGraphQLServerOptions(req, res) {
|
63 | const _super = Object.create(null, {
|
64 | graphQLServerOptions: { get: () => super.graphQLServerOptions }
|
65 | });
|
66 | return __awaiter(this, void 0, void 0, function* () {
|
67 | return _super.graphQLServerOptions.call(this, { req, res });
|
68 | });
|
69 | }
|
70 | supportsSubscriptions() {
|
71 | return true;
|
72 | }
|
73 | supportsUploads() {
|
74 | return true;
|
75 | }
|
76 | applyMiddleware(_a) {
|
77 | var { app } = _a, rest = __rest(_a, ["app"]);
|
78 | app.use(this.getMiddleware(rest));
|
79 | }
|
80 | getMiddleware({ path, cors, bodyParserConfig, disableHealthCheck, onHealthCheck, } = {}) {
|
81 | if (!path)
|
82 | path = '/graphql';
|
83 | const router = express_1.default.Router();
|
84 | const promiseWillStart = this.willStart();
|
85 | router.use(path, (_req, _res, next) => {
|
86 | promiseWillStart.then(() => next()).catch(next);
|
87 | });
|
88 | if (!disableHealthCheck) {
|
89 | router.use('/.well-known/apollo/server-health', (req, res) => {
|
90 | res.type('application/health+json');
|
91 | if (onHealthCheck) {
|
92 | onHealthCheck(req)
|
93 | .then(() => {
|
94 | res.json({ status: 'pass' });
|
95 | })
|
96 | .catch(() => {
|
97 | res.status(503).json({ status: 'fail' });
|
98 | });
|
99 | }
|
100 | else {
|
101 | res.json({ status: 'pass' });
|
102 | }
|
103 | });
|
104 | }
|
105 | let uploadsMiddleware;
|
106 | if (this.uploadsConfig && typeof apollo_server_core_1.processFileUploads === 'function') {
|
107 | uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
|
108 | }
|
109 | this.graphqlPath = path;
|
110 | if (cors === true) {
|
111 | router.use(path, cors_1.default());
|
112 | }
|
113 | else if (cors !== false) {
|
114 | router.use(path, cors_1.default(cors));
|
115 | }
|
116 | if (bodyParserConfig === true) {
|
117 | router.use(path, body_parser_1.json());
|
118 | }
|
119 | else if (bodyParserConfig !== false) {
|
120 | router.use(path, body_parser_1.json(bodyParserConfig));
|
121 | }
|
122 | if (uploadsMiddleware) {
|
123 | router.use(path, uploadsMiddleware);
|
124 | }
|
125 | router.use(path, (req, res, next) => {
|
126 | if (this.playgroundOptions && req.method === 'GET') {
|
127 | const accept = accepts_1.default(req);
|
128 | const types = accept.types();
|
129 | const prefersHTML = types.find((x) => x === 'text/html' || x === 'application/json') === 'text/html';
|
130 | if (prefersHTML) {
|
131 | const playgroundRenderPageOptions = Object.assign({ endpoint: req.originalUrl, subscriptionEndpoint: this.subscriptionsPath }, this.playgroundOptions);
|
132 | res.setHeader('Content-Type', 'text/html');
|
133 | const playground = graphql_playground_html_1.renderPlaygroundPage(playgroundRenderPageOptions);
|
134 | res.write(playground);
|
135 | res.end();
|
136 | return;
|
137 | }
|
138 | }
|
139 | return expressApollo_1.graphqlExpress(() => this.createGraphQLServerOptions(req, res))(req, res, next);
|
140 | });
|
141 | return router;
|
142 | }
|
143 | }
|
144 | exports.ApolloServer = ApolloServer;
|
145 |
|
\ | No newline at end of file |