UNPKG

@javiercrowsoft/graffiti

Version:
84 lines (64 loc) 2.48 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = middleware; var _graphql = require('graphql'); var _util = require('../util'); function accepts(type) { return this.headers && this.headers.accept && this.headers.accept.includes(type); } function middleware() { var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var _ref$graphiql = _ref.graphiql; var graphiql = _ref$graphiql === undefined ? true : _ref$graphiql; var _ref$schema = _ref.schema; var schema = _ref$schema === undefined ? (0, _util.required)() : _ref$schema; return regeneratorRuntime.mark(function middleware(next) { var body, _Object$assign, query, variables, parsedVariables; return regeneratorRuntime.wrap(function middleware$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!((0, _util.isPath)(this) && ((0, _util.isPost)(this) || (0, _util.isGet)(this)))) { _context.next = 15; break; } body = this.request.body; _Object$assign = Object.assign({}, body, this.query); query = _Object$assign.query; variables = _Object$assign.variables; if (!((0, _util.isGet)(this) && accepts.call(this, 'html') && graphiql)) { _context.next = 8; break; } this.body = (0, _util.renderGraphiQL)({ query: query, variables: variables }); return _context.abrupt('return', this.body); case 8: if ((0, _util.isGet)(this) && query && query.includes('mutation')) { this.throw(406, 'GraphQL mutation only allowed in POST request.'); } parsedVariables = variables; try { parsedVariables = JSON.parse(variables); } catch (err) { // ignore } _context.next = 13; return (0, _graphql.graphql)(schema, query, this, this, parsedVariables); case 13: this.body = _context.sent; return _context.abrupt('return', this.body); case 15: _context.next = 17; return next; case 17: return _context.abrupt('return', _context.sent); case 18: case 'end': return _context.stop(); } } }, middleware, this); }); }