1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.intoObjectType = exports.mutation = exports.type = undefined;
|
7 |
|
8 | var type = exports.type = function () {
|
9 | var _ref = _asyncToGenerator( regeneratorRuntime.mark(function _callee(client, name) {
|
10 | var jscramblerVersion, query, res, __type;
|
11 |
|
12 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
13 | while (1) {
|
14 | switch (_context.prev = _context.next) {
|
15 | case 0:
|
16 | jscramblerVersion = client.options.jscramblerVersion;
|
17 |
|
18 |
|
19 | if (!typeCache[jscramblerVersion]) {
|
20 | typeCache[jscramblerVersion] = {};
|
21 | }
|
22 |
|
23 | if (!typeCache[jscramblerVersion][name]) {
|
24 | _context.next = 4;
|
25 | break;
|
26 | }
|
27 |
|
28 | return _context.abrupt('return', typeCache[jscramblerVersion][name]);
|
29 |
|
30 | case 4:
|
31 | query = {
|
32 | query: '\nquery getType($name: String!) {\n __type(name: $name) {\n kind\n name\n description\n fields(includeDeprecated: true) {\n name\n description\n args {\n ...InputValue\n }\n type {\n ...TypeRef\n }\n isDeprecated\n deprecationReason\n }\n inputFields {\n ...InputValue\n }\n interfaces {\n ...TypeRef\n }\n enumValues(includeDeprecated: true) {\n name\n description\n isDeprecated\n deprecationReason\n }\n possibleTypes {\n ...TypeRef\n }\n }\n}\n\nfragment InputValue on __InputValue {\n name\n description\n type { ...TypeRef }\n defaultValue\n}\n\nfragment TypeRef on __Type {\n kind\n name\n inputFields {\n name\n type {\n name\n kind\n }\n }\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n }\n }\n }\n}',
|
33 | params: JSON.stringify({
|
34 | name: name
|
35 | })
|
36 | };
|
37 | _context.next = 7;
|
38 | return client.get('/application', query);
|
39 |
|
40 | case 7:
|
41 | res = _context.sent;
|
42 | __type = res.data.__type;
|
43 |
|
44 |
|
45 | typeCache[jscramblerVersion][__type.name] = __type;
|
46 |
|
47 | return _context.abrupt('return', __type);
|
48 |
|
49 | case 11:
|
50 | case 'end':
|
51 | return _context.stop();
|
52 | }
|
53 | }
|
54 | }, _callee, this);
|
55 | }));
|
56 |
|
57 | return function type(_x, _x2) {
|
58 | return _ref.apply(this, arguments);
|
59 | };
|
60 | }();
|
61 |
|
62 | var mutation = exports.mutation = function () {
|
63 | var _ref2 = _asyncToGenerator( regeneratorRuntime.mark(function _callee2(client, name) {
|
64 | var rootMutation, mutationType;
|
65 | return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
66 | while (1) {
|
67 | switch (_context2.prev = _context2.next) {
|
68 | case 0:
|
69 | _context2.next = 2;
|
70 | return type(client, 'RootMutation');
|
71 |
|
72 | case 2:
|
73 | rootMutation = _context2.sent;
|
74 | mutationType = rootMutation.fields.find(function (f) {
|
75 | return f.name === name;
|
76 | });
|
77 | return _context2.abrupt('return', mutationType);
|
78 |
|
79 | case 5:
|
80 | case 'end':
|
81 | return _context2.stop();
|
82 | }
|
83 | }
|
84 | }, _callee2, this);
|
85 | }));
|
86 |
|
87 | return function mutation(_x3, _x4) {
|
88 | return _ref2.apply(this, arguments);
|
89 | };
|
90 | }();
|
91 |
|
92 | var intoObjectType = exports.intoObjectType = function () {
|
93 | var _ref3 = _asyncToGenerator( regeneratorRuntime.mark(function _callee4(client, obj, name) {
|
94 | var _this = this;
|
95 |
|
96 | var fields, finalObj, keys;
|
97 | return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
98 | while (1) {
|
99 | switch (_context4.prev = _context4.next) {
|
100 | case 0:
|
101 | _context4.next = 2;
|
102 | return type(client, name);
|
103 |
|
104 | case 2:
|
105 | fields = _context4.sent.fields;
|
106 | finalObj = {};
|
107 | keys = Object.keys(obj);
|
108 | _context4.next = 7;
|
109 | return Promise.all(keys.map(function () {
|
110 | var _ref4 = _asyncToGenerator( regeneratorRuntime.mark(function _callee3(k) {
|
111 | var field;
|
112 | return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
113 | while (1) {
|
114 | switch (_context3.prev = _context3.next) {
|
115 | case 0:
|
116 | field = fields.find(function (f) {
|
117 | return f.name === k;
|
118 | });
|
119 |
|
120 | if (!(field && field.type)) {
|
121 | _context3.next = 14;
|
122 | break;
|
123 | }
|
124 |
|
125 | finalObj[k] = (0, _lodash2.default)(obj[k]);
|
126 |
|
127 | if (!(field.type.kind === 'OBJECT' && !!field.type.name)) {
|
128 | _context3.next = 8;
|
129 | break;
|
130 | }
|
131 |
|
132 | _context3.next = 6;
|
133 | return intoObjectType(client, finalObj[k], field.type.name);
|
134 |
|
135 | case 6:
|
136 | finalObj[k] = _context3.sent;
|
137 | return _context3.abrupt('return');
|
138 |
|
139 | case 8:
|
140 | if (!((field.type.kind === 'NON_NULL' || field.type.kind === 'LIST') && field.type.ofType.kind === 'OBJECT')) {
|
141 | _context3.next = 13;
|
142 | break;
|
143 | }
|
144 |
|
145 | _context3.next = 11;
|
146 | return intoObjectType(client, finalObj[k], field.type.ofType.name);
|
147 |
|
148 | case 11:
|
149 | finalObj[k] = _context3.sent;
|
150 | return _context3.abrupt('return');
|
151 |
|
152 | case 13:
|
153 |
|
154 | if (field.type.name === 'String' && typeof finalObj[k] !== 'string') {
|
155 | finalObj[k] = JSON.stringify(finalObj[k]);
|
156 | }
|
157 |
|
158 | case 14:
|
159 | case 'end':
|
160 | return _context3.stop();
|
161 | }
|
162 | }
|
163 | }, _callee3, _this);
|
164 | }));
|
165 |
|
166 | return function (_x8) {
|
167 | return _ref4.apply(this, arguments);
|
168 | };
|
169 | }()));
|
170 |
|
171 | case 7:
|
172 | return _context4.abrupt('return', finalObj);
|
173 |
|
174 | case 8:
|
175 | case 'end':
|
176 | return _context4.stop();
|
177 | }
|
178 | }
|
179 | }, _callee4, this);
|
180 | }));
|
181 |
|
182 | return function intoObjectType(_x5, _x6, _x7) {
|
183 | return _ref3.apply(this, arguments);
|
184 | };
|
185 | }();
|
186 |
|
187 | var _lodash = require('lodash.clonedeep');
|
188 |
|
189 | var _lodash2 = _interopRequireDefault(_lodash);
|
190 |
|
191 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
192 |
|
193 | function _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"); }); }; }
|
194 |
|
195 | var typeCache = {}; |
\ | No newline at end of file |