expressApollo.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.graphqlExpress = void 0;
  4. const apollo_server_core_1 = require("apollo-server-core");
  5. function graphqlExpress(options) {
  6. if (!options) {
  7. throw new Error('Apollo Server requires options.');
  8. }
  9. if (arguments.length > 1) {
  10. throw new Error(`Apollo Server expects exactly one argument, got ${arguments.length}`);
  11. }
  12. return (req, res, next) => {
  13. apollo_server_core_1.runHttpQuery([req, res], {
  14. method: req.method,
  15. options: options,
  16. query: req.method === 'POST' ? req.body : req.query,
  17. request: apollo_server_core_1.convertNodeHttpToRequest(req),
  18. }).then(({ graphqlResponse, responseInit }) => {
  19. if (responseInit.headers) {
  20. for (const [name, value] of Object.entries(responseInit.headers)) {
  21. res.setHeader(name, value);
  22. }
  23. }
  24. if (typeof res.send === 'function') {
  25. res.send(graphqlResponse);
  26. }
  27. else {
  28. res.end(graphqlResponse);
  29. }
  30. }, (error) => {
  31. if ('HttpQueryError' !== error.name) {
  32. return next(error);
  33. }
  34. if (error.headers) {
  35. for (const [name, value] of Object.entries(error.headers)) {
  36. res.setHeader(name, value);
  37. }
  38. }
  39. res.statusCode = error.statusCode;
  40. if (typeof res.send === 'function') {
  41. res.send(error.message);
  42. }
  43. else {
  44. res.end(error.message);
  45. }
  46. });
  47. };
  48. }
  49. exports.graphqlExpress = graphqlExpress;
  50. //# sourceMappingURL=expressApollo.js.map