UNPKG

3.57 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.addMiddleware = addMiddleware;
7
8var _koaCompose = require('koa-compose');
9
10var _koaCompose2 = _interopRequireDefault(_koaCompose);
11
12var _graphql = require('graphql');
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
17
18const wrapMiddleware = function wrapMiddleware(fn) {
19 return function () {
20 let context = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
21 let next = arguments[1];
22
23 const args = (context.args || []).concat(next);
24 return fn.apply(this, args);
25 };
26};
27
28const getType = function getType(type) {
29 type = (0, _graphql.getNullableType)(type);
30 if (type.ofType) {
31 type = getType(type.ofType);
32 }
33 return type;
34};
35
36const pre = function pre(pre, fn) {
37 pre = wrapMiddleware(pre);
38 if (!fn.__chain) {
39 fn = function (fn) {
40 return (() => {
41 var _ref = _asyncToGenerator(function* (_ref2) {
42 let { args } = _ref2;
43
44 return fn.apply(this, args);
45 });
46
47 return function (_x2) {
48 return _ref.apply(this, arguments);
49 };
50 })();
51 }(fn);
52 }
53 const chain = fn.__chain || [fn];
54 const last = chain.pop();
55 chain.push(pre);
56 chain.push(last);
57 const newFn = (0, _koaCompose2.default)(chain);
58 const wrappedFn = (() => {
59 var _ref3 = _asyncToGenerator(function* () {
60 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
61 args[_key] = arguments[_key];
62 }
63
64 return newFn.call(this, { args });
65 });
66
67 return function wrappedFn() {
68 return _ref3.apply(this, arguments);
69 };
70 })();
71 wrappedFn.__chain = chain;
72 return wrappedFn;
73};
74
75function addMiddleware(schema, path, fn) {
76 if (!fn) {
77 fn = path;
78 path = null;
79 }
80 let parentType, parentField;
81 if (path) {
82 [parentType, parentField] = path.split('.');
83 }
84 const rootTypes = [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()].filter(x => !!x);
85 rootTypes.forEach(type => {
86 addMiddlewareToType(type, fn, { parentType, parentField });
87 });
88};
89
90const addMiddlewareToType = function (type, fn, _ref4) {
91 let {
92 parentType,
93 parentField,
94 middlewaredTypes = {}
95 } = _ref4;
96
97
98 if (type && type.name && middlewaredTypes[type.name]) {
99 // Stop going into recursion with adding middlewares
100 // on recursive types
101 return;
102 } else {
103 middlewaredTypes[type.name] = true;
104 }
105
106 const matchesParent = parentType ? parentType === type.name : true;
107 const fields = type.getFields();
108 Object.keys(fields).forEach(fieldName => {
109 const matchesField = parentField ? parentField === fieldName : true;
110 if (fields[fieldName].resolve && matchesField && matchesParent) {
111 fields[fieldName].resolve = pre(fn, fields[fieldName].resolve);
112 }
113 const fieldType = getType(fields[fieldName].type);
114 if (fieldType.getFields) {
115 addMiddlewareToType(fieldType, fn, {
116 parentType,
117 parentField,
118 middlewaredTypes
119 });
120 }
121 });
122};
\No newline at end of file