UNPKG

graphql-anywhere-mongodb-express

Version:

express middleware that accepts graphql requests and runs them as mongo queries

36 lines 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const handle_async_1 = require("../handle-async"); const parser_1 = require("graphql/language/parser"); const log_1 = require("../log"); function mongoGraphQLMiddleware() { return handle_async_1.handleAsync((req, res) => tslib_1.__awaiter(this, void 0, void 0, function* () { // Get client off request const client = req['__mongoGraphQLClient']; try { // Extract query and variables from body const { query, variables } = req.body; log_1.log(`GQL Request :`, query); log_1.log(`GQL Variables :`, variables); // Convert raw graphql query to GraphQL AST const processedQuery = parser_1.parse(query); // Execute it and return a result const result = yield client.find(processedQuery, variables); res.json(result); } catch (err) { log_1.log(`Error processing graphql request`, err); res.json({ data: null, errors: [ { message: err.message || err } ] }); } })); } exports.mongoGraphQLMiddleware = mongoGraphQLMiddleware; //# sourceMappingURL=mongo-graphql-middleware.js.map