123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.ParseGraphQLServer = void 0;
- var _cors = _interopRequireDefault(require("cors"));
- var _graphqlUploadExpress = _interopRequireDefault(require("graphql-upload/graphqlUploadExpress.js"));
- var _server = require("@apollo/server");
- var _express = require("@apollo/server/express4");
- var _disabled = require("@apollo/server/plugin/disabled");
- var _express2 = _interopRequireDefault(require("express"));
- var _graphql = require("graphql");
- var _subscriptionsTransportWs = require("subscriptions-transport-ws");
- var _middlewares = require("../middlewares");
- var _requiredParameter = _interopRequireDefault(require("../requiredParameter"));
- var _logger = _interopRequireDefault(require("../logger"));
- var _ParseGraphQLSchema = require("./ParseGraphQLSchema");
- var _ParseGraphQLController = _interopRequireWildcard(require("../Controllers/ParseGraphQLController"));
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- class ParseGraphQLServer {
- constructor(parseServer, config) {
- this.parseServer = parseServer || (0, _requiredParameter.default)('You must provide a parseServer instance!');
- if (!config || !config.graphQLPath) {
- (0, _requiredParameter.default)('You must provide a config.graphQLPath!');
- }
- this.config = config;
- this.parseGraphQLController = this.parseServer.config.parseGraphQLController;
- this.log = this.parseServer.config && this.parseServer.config.loggerController || _logger.default;
- this.parseGraphQLSchema = new _ParseGraphQLSchema.ParseGraphQLSchema({
- parseGraphQLController: this.parseGraphQLController,
- databaseController: this.parseServer.config.databaseController,
- log: this.log,
- graphQLCustomTypeDefs: this.config.graphQLCustomTypeDefs,
- appId: this.parseServer.config.appId
- });
- }
- async _getGraphQLOptions() {
- try {
- return {
- schema: await this.parseGraphQLSchema.load(),
- context: async ({
- req,
- res
- }) => {
- res.set('access-control-allow-origin', req.get('origin') || '*');
- return {
- info: req.info,
- config: req.config,
- auth: req.auth
- };
- }
- };
- } catch (e) {
- this.log.error(e.stack || typeof e.toString === 'function' && e.toString() || e);
- throw e;
- }
- }
- async _getServer() {
- const schemaRef = this.parseGraphQLSchema.graphQLSchema;
- const newSchemaRef = await this.parseGraphQLSchema.load();
- if (schemaRef === newSchemaRef && this._server) {
- return this._server;
- }
- const {
- schema,
- context
- } = await this._getGraphQLOptions();
- const apollo = new _server.ApolloServer({
- csrfPrevention: {
-
-
- requestHeaders: ['X-Parse-Application-Id']
- },
- introspection: true,
- plugins: [(0, _disabled.ApolloServerPluginCacheControlDisabled)()],
- schema
- });
- await apollo.start();
- this._server = (0, _express.expressMiddleware)(apollo, {
- context
- });
- return this._server;
- }
- _transformMaxUploadSizeToBytes(maxUploadSize) {
- const unitMap = {
- kb: 1,
- mb: 2,
- gb: 3
- };
- return Number(maxUploadSize.slice(0, -2)) * Math.pow(1024, unitMap[maxUploadSize.slice(-2).toLowerCase()]);
- }
- applyGraphQL(app) {
- if (!app || !app.use) {
- (0, _requiredParameter.default)('You must provide an Express.js app instance!');
- }
- app.use(this.config.graphQLPath, (0, _cors.default)());
- app.use(this.config.graphQLPath, _middlewares.handleParseHeaders);
- app.use(this.config.graphQLPath, _middlewares.handleParseSession);
- app.use(this.config.graphQLPath, _middlewares.handleParseErrors);
- app.use(this.config.graphQLPath, (0, _graphqlUploadExpress.default)({
- maxFileSize: this._transformMaxUploadSizeToBytes(this.parseServer.config.maxUploadSize || '20mb')
- }));
- app.use(this.config.graphQLPath, _express2.default.json(), async (req, res, next) => {
- const server = await this._getServer();
- return server(req, res, next);
- });
- }
- applyPlayground(app) {
- if (!app || !app.get) {
- (0, _requiredParameter.default)('You must provide an Express.js app instance!');
- }
- app.get(this.config.playgroundPath || (0, _requiredParameter.default)('You must provide a config.playgroundPath to applyPlayground!'), (_req, res) => {
- res.setHeader('Content-Type', 'text/html');
- res.write(`<div id="sandbox" style="position:absolute;top:0;right:0;bottom:0;left:0"></div>
- <script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
- <script>
- new window.EmbeddedSandbox({
- target: "#sandbox",
- endpointIsEditable: false,
- initialEndpoint: "${JSON.stringify(this.config.graphQLPath)}",
- handleRequest: (endpointUrl, options) => {
- return fetch(endpointUrl, {
- ...options,
- headers: {
- ...options.headers,
- 'X-Parse-Application-Id': "${JSON.stringify(this.parseServer.config.appId)}",
- 'X-Parse-Master-Key': "${JSON.stringify(this.parseServer.config.masterKey)}",
- },
- })
- },
- });
- // advanced options: https://www.apollographql.com/docs/studio/explorer/sandbox#embedding-sandbox
- </script>`);
- res.end();
- });
- }
- createSubscriptions(server) {
- _subscriptionsTransportWs.SubscriptionServer.create({
- execute: _graphql.execute,
- subscribe: _graphql.subscribe,
- onOperation: async (_message, params, webSocket) => Object.assign({}, params, await this._getGraphQLOptions(webSocket.upgradeReq))
- }, {
- server,
- path: this.config.subscriptionsPath || (0, _requiredParameter.default)('You must provide a config.subscriptionsPath to createSubscriptions!')
- });
- }
- setGraphQLConfig(graphQLConfig) {
- return this.parseGraphQLController.updateGraphQLConfig(graphQLConfig);
- }
- }
- exports.ParseGraphQLServer = ParseGraphQLServer;
|