ApolloServer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (!server.disableUploads() &&
  39. typeof apollo_server_core_1.processFileUploads === 'function' &&
  40. type_is_1.default(req, ['multipart/form-data'])) {
  41. apollo_server_core_1.processFileUploads(req, res, uploadsConfig)
  42. .then(body => {
  43. req.body = body;
  44. next();
  45. })
  46. .catch(error => {
  47. if (error.status && error.expose)
  48. res.status(error.status);
  49. next(apollo_server_core_1.formatApolloErrors([error], {
  50. formatter: server.requestOptions.formatError,
  51. debug: server.requestOptions.debug,
  52. }));
  53. });
  54. }
  55. else {
  56. next();
  57. }
  58. };
  59. class ApolloServer extends apollo_server_core_1.ApolloServerBase {
  60. constructor(config) {
  61. super(config);
  62. }
  63. createGraphQLServerOptions(req, res) {
  64. const _super = Object.create(null, {
  65. graphQLServerOptions: { get: () => super.graphQLServerOptions }
  66. });
  67. return __awaiter(this, void 0, void 0, function* () {
  68. return _super.graphQLServerOptions.call(this, { req, res });
  69. });
  70. }
  71. supportsSubscriptions() {
  72. return true;
  73. }
  74. supportsUploads() {
  75. return true;
  76. }
  77. applyMiddleware(_a) {
  78. var { app } = _a, rest = __rest(_a, ["app"]);
  79. app.use(this.getMiddleware(rest));
  80. }
  81. getMiddleware({ path, cors, bodyParserConfig, disableHealthCheck, onHealthCheck, } = {}) {
  82. if (!path)
  83. path = '/graphql';
  84. this.ensureStarting();
  85. const router = express_1.default.Router();
  86. if (!disableHealthCheck) {
  87. router.use('/.well-known/apollo/server-health', (req, res) => {
  88. res.type('application/health+json');
  89. if (onHealthCheck) {
  90. onHealthCheck(req)
  91. .then(() => {
  92. res.json({ status: 'pass' });
  93. })
  94. .catch(() => {
  95. res.status(503).json({ status: 'fail' });
  96. });
  97. }
  98. else {
  99. res.json({ status: 'pass' });
  100. }
  101. });
  102. }
  103. let uploadsMiddleware;
  104. if (this.uploadsConfig && typeof apollo_server_core_1.processFileUploads === 'function') {
  105. uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
  106. }
  107. this.graphqlPath = path;
  108. if (cors === true) {
  109. router.use(path, cors_1.default());
  110. }
  111. else if (cors !== false) {
  112. router.use(path, cors_1.default(cors));
  113. }
  114. if (bodyParserConfig === true) {
  115. router.use(path, body_parser_1.json());
  116. }
  117. else if (bodyParserConfig !== false) {
  118. router.use(path, body_parser_1.json(bodyParserConfig));
  119. }
  120. if (uploadsMiddleware) {
  121. router.use(path, uploadsMiddleware);
  122. }
  123. router.use(path, (req, res, next) => {
  124. if (this.playgroundOptions && req.method === 'GET') {
  125. const accept = accepts_1.default(req);
  126. const types = accept.types();
  127. const prefersHTML = types.find((x) => x === 'text/html' || x === 'application/json') === 'text/html';
  128. if (prefersHTML) {
  129. const playgroundRenderPageOptions = Object.assign({ endpoint: req.originalUrl, subscriptionEndpoint: this.subscriptionsPath }, this.playgroundOptions);
  130. res.setHeader('Content-Type', 'text/html');
  131. const playground = graphql_playground_html_1.renderPlaygroundPage(playgroundRenderPageOptions);
  132. res.write(playground);
  133. res.end();
  134. return;
  135. }
  136. }
  137. return expressApollo_1.graphqlExpress(() => this.createGraphQLServerOptions(req, res))(req, res, next);
  138. });
  139. return router;
  140. }
  141. }
  142. exports.ApolloServer = ApolloServer;
  143. //# sourceMappingURL=ApolloServer.js.map