@graphql-tools/mock 8.3.2-alpha-5d885fa3.0 → 8.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MockStore.d.ts +1 -0
- package/index.js +3 -0
- package/index.mjs +3 -0
- package/package.json +3 -3
- package/types.d.ts +4 -0
- package/es5/MockList.d.ts +0 -25
- package/es5/MockStore.d.ts +0 -93
- package/es5/README.md +0 -5
- package/es5/addMocksToSchema.d.ts +0 -78
- package/es5/index.d.ts +0 -5
- package/es5/index.js +0 -901
- package/es5/index.mjs +0 -885
- package/es5/mockServer.d.ts +0 -15
- package/es5/package.json +0 -37
- package/es5/types.d.ts +0 -194
- package/es5/utils.d.ts +0 -8
package/es5/index.js
DELETED
|
@@ -1,901 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
-
|
|
7
|
-
const tslib = require('tslib');
|
|
8
|
-
const graphql = require('graphql');
|
|
9
|
-
const stringify = _interopDefault(require('fast-json-stable-stringify'));
|
|
10
|
-
const utils = require('@graphql-tools/utils/es5');
|
|
11
|
-
const schema = require('@graphql-tools/schema/es5');
|
|
12
|
-
|
|
13
|
-
function isRef(maybeRef) {
|
|
14
|
-
return !!(maybeRef && typeof maybeRef === 'object' && '$ref' in maybeRef);
|
|
15
|
-
}
|
|
16
|
-
function assertIsRef(maybeRef, message) {
|
|
17
|
-
if (!isRef(maybeRef)) {
|
|
18
|
-
throw new Error(message || "Expected " + maybeRef + " to be a valid Ref.");
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function isRecord(obj) {
|
|
22
|
-
return typeof obj === 'object' && obj !== null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function uuidv4() {
|
|
26
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
27
|
-
var r = (Math.random() * 16) | 0;
|
|
28
|
-
// eslint-disable-next-line eqeqeq
|
|
29
|
-
var v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
30
|
-
return v.toString(16);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
var randomListLength = function () {
|
|
34
|
-
// Mocking has always returned list of length 2 by default
|
|
35
|
-
// return 1 + Math.round(Math.random() * 10)
|
|
36
|
-
return 2;
|
|
37
|
-
};
|
|
38
|
-
var takeRandom = function (arr) { return arr[Math.floor(Math.random() * arr.length)]; };
|
|
39
|
-
function makeRef(typeName, key) {
|
|
40
|
-
return { $ref: { key: key, typeName: typeName } };
|
|
41
|
-
}
|
|
42
|
-
function isObject(thing) {
|
|
43
|
-
return thing === Object(thing) && !Array.isArray(thing);
|
|
44
|
-
}
|
|
45
|
-
function copyOwnPropsIfNotPresent(target, source) {
|
|
46
|
-
var e_1, _a;
|
|
47
|
-
try {
|
|
48
|
-
for (var _b = tslib.__values(Object.getOwnPropertyNames(source)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
49
|
-
var prop = _c.value;
|
|
50
|
-
if (!Object.getOwnPropertyDescriptor(target, prop)) {
|
|
51
|
-
var propertyDescriptor = Object.getOwnPropertyDescriptor(source, prop);
|
|
52
|
-
Object.defineProperty(target, prop, propertyDescriptor == null ? {} : propertyDescriptor);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e_1) throw e_1.error; }
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function copyOwnProps(target) {
|
|
65
|
-
var e_2, _a;
|
|
66
|
-
var sources = [];
|
|
67
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
68
|
-
sources[_i - 1] = arguments[_i];
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
for (var sources_1 = tslib.__values(sources), sources_1_1 = sources_1.next(); !sources_1_1.done; sources_1_1 = sources_1.next()) {
|
|
72
|
-
var source = sources_1_1.value;
|
|
73
|
-
var chain = source;
|
|
74
|
-
while (chain != null) {
|
|
75
|
-
copyOwnPropsIfNotPresent(target, chain);
|
|
76
|
-
chain = Object.getPrototypeOf(chain);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
81
|
-
finally {
|
|
82
|
-
try {
|
|
83
|
-
if (sources_1_1 && !sources_1_1.done && (_a = sources_1.return)) _a.call(sources_1);
|
|
84
|
-
}
|
|
85
|
-
finally { if (e_2) throw e_2.error; }
|
|
86
|
-
}
|
|
87
|
-
return target;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @internal
|
|
92
|
-
*/
|
|
93
|
-
function isMockList(obj) {
|
|
94
|
-
if (typeof (obj === null || obj === void 0 ? void 0 : obj.len) === 'number' || (Array.isArray(obj === null || obj === void 0 ? void 0 : obj.len) && typeof (obj === null || obj === void 0 ? void 0 : obj.len[0]) === 'number')) {
|
|
95
|
-
if (typeof obj.wrappedFunction === 'undefined' || typeof obj.wrappedFunction === 'function') {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* This is an object you can return from your mock resolvers which calls the
|
|
103
|
-
* provided `mockFunction` once for each list item.
|
|
104
|
-
*/
|
|
105
|
-
var MockList = /** @class */ (function () {
|
|
106
|
-
/**
|
|
107
|
-
* @param length Either the exact length of items to return or an inclusive
|
|
108
|
-
* range of possible lengths.
|
|
109
|
-
* @param mockFunction The function to call for each item in the list to
|
|
110
|
-
* resolve it. It can return another MockList or a value.
|
|
111
|
-
*/
|
|
112
|
-
function MockList(length, mockFunction) {
|
|
113
|
-
this.len = length;
|
|
114
|
-
if (typeof mockFunction !== 'undefined') {
|
|
115
|
-
if (typeof mockFunction !== 'function') {
|
|
116
|
-
throw new Error('Second argument to MockList must be a function or undefined');
|
|
117
|
-
}
|
|
118
|
-
this.wrappedFunction = mockFunction;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* @internal
|
|
123
|
-
*/
|
|
124
|
-
MockList.prototype.mock = function () {
|
|
125
|
-
var arr;
|
|
126
|
-
if (Array.isArray(this.len)) {
|
|
127
|
-
arr = new Array(this.randint(this.len[0], this.len[1]));
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
arr = new Array(this.len);
|
|
131
|
-
}
|
|
132
|
-
for (var i = 0; i < arr.length; i++) {
|
|
133
|
-
if (typeof this.wrappedFunction === 'function') {
|
|
134
|
-
var res = this.wrappedFunction();
|
|
135
|
-
if (isMockList(res)) {
|
|
136
|
-
arr[i] = res.mock();
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
arr[i] = res;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
arr[i] = undefined;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return arr;
|
|
147
|
-
};
|
|
148
|
-
MockList.prototype.randint = function (low, high) {
|
|
149
|
-
return Math.floor(Math.random() * (high - low + 1) + low);
|
|
150
|
-
};
|
|
151
|
-
return MockList;
|
|
152
|
-
}());
|
|
153
|
-
function deepResolveMockList(mockList) {
|
|
154
|
-
return mockList.mock().map(function (v) {
|
|
155
|
-
if (isMockList(v))
|
|
156
|
-
return deepResolveMockList(v);
|
|
157
|
-
return v;
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
var defaultMocks = {
|
|
162
|
-
Int: function () { return Math.round(Math.random() * 200) - 100; },
|
|
163
|
-
Float: function () { return Math.random() * 200 - 100; },
|
|
164
|
-
String: function () { return 'Hello World'; },
|
|
165
|
-
Boolean: function () { return Math.random() > 0.5; },
|
|
166
|
-
ID: function () { return uuidv4(); },
|
|
167
|
-
};
|
|
168
|
-
var defaultKeyFieldNames = ['id', '_id'];
|
|
169
|
-
var MockStore = /** @class */ (function () {
|
|
170
|
-
function MockStore(_a) {
|
|
171
|
-
var schema = _a.schema, mocks = _a.mocks, typePolicies = _a.typePolicies;
|
|
172
|
-
this.store = {};
|
|
173
|
-
this.schema = schema;
|
|
174
|
-
this.mocks = tslib.__assign(tslib.__assign({}, defaultMocks), mocks);
|
|
175
|
-
this.typePolicies = typePolicies || {};
|
|
176
|
-
}
|
|
177
|
-
MockStore.prototype.get = function (_typeName, _key, _fieldName, _fieldArgs) {
|
|
178
|
-
if (typeof _typeName !== 'string') {
|
|
179
|
-
if (_key === undefined) {
|
|
180
|
-
if (isRef(_typeName)) {
|
|
181
|
-
throw new Error("Can't provide a ref as first argument and no other argument");
|
|
182
|
-
}
|
|
183
|
-
// get({...})
|
|
184
|
-
return this.getImpl(_typeName);
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
assertIsRef(_typeName);
|
|
188
|
-
var $ref = _typeName.$ref;
|
|
189
|
-
// arguments shift
|
|
190
|
-
_fieldArgs = _fieldName;
|
|
191
|
-
_fieldName = _key;
|
|
192
|
-
_key = $ref.key;
|
|
193
|
-
_typeName = $ref.typeName;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
var args = {
|
|
197
|
-
typeName: _typeName,
|
|
198
|
-
};
|
|
199
|
-
if (isRecord(_key) || _key === undefined) {
|
|
200
|
-
// get('User', { name: 'Alex'})
|
|
201
|
-
args.defaultValue = _key;
|
|
202
|
-
return this.getImpl(args);
|
|
203
|
-
}
|
|
204
|
-
args.key = _key;
|
|
205
|
-
if (Array.isArray(_fieldName) && _fieldName.length === 1) {
|
|
206
|
-
_fieldName = _fieldName[0];
|
|
207
|
-
}
|
|
208
|
-
if (typeof _fieldName !== 'string' && !Array.isArray(_fieldName)) {
|
|
209
|
-
// get('User', 'me', { name: 'Alex'})
|
|
210
|
-
args.defaultValue = _fieldName;
|
|
211
|
-
return this.getImpl(args);
|
|
212
|
-
}
|
|
213
|
-
if (Array.isArray(_fieldName)) {
|
|
214
|
-
// get('User', 'me', ['father', 'name'])
|
|
215
|
-
var ref = this.get(_typeName, _key, _fieldName[0], _fieldArgs);
|
|
216
|
-
assertIsRef(ref);
|
|
217
|
-
return this.get(ref.$ref.typeName, ref.$ref.key, _fieldName.slice(1, _fieldName.length));
|
|
218
|
-
}
|
|
219
|
-
// get('User', 'me', 'name'...);
|
|
220
|
-
args.fieldName = _fieldName;
|
|
221
|
-
args.fieldArgs = _fieldArgs;
|
|
222
|
-
return this.getImpl(args);
|
|
223
|
-
};
|
|
224
|
-
MockStore.prototype.set = function (_typeName, _key, _fieldName, _value) {
|
|
225
|
-
if (typeof _typeName !== 'string') {
|
|
226
|
-
if (_key === undefined) {
|
|
227
|
-
if (isRef(_typeName)) {
|
|
228
|
-
throw new Error("Can't provide a ref as first argument and no other argument");
|
|
229
|
-
}
|
|
230
|
-
// set({...})
|
|
231
|
-
return this.setImpl(_typeName);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
assertIsRef(_typeName);
|
|
235
|
-
var $ref = _typeName.$ref;
|
|
236
|
-
// arguments shift
|
|
237
|
-
_value = _fieldName;
|
|
238
|
-
_fieldName = _key;
|
|
239
|
-
_key = $ref.key;
|
|
240
|
-
_typeName = $ref.typeName;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
assertIsDefined(_key, 'key was not provided');
|
|
244
|
-
var args = {
|
|
245
|
-
typeName: _typeName,
|
|
246
|
-
key: _key,
|
|
247
|
-
};
|
|
248
|
-
if (typeof _fieldName !== 'string') {
|
|
249
|
-
// set('User', 1, { name: 'Foo' })
|
|
250
|
-
if (!isRecord(_fieldName))
|
|
251
|
-
throw new Error('Expected value to be a record');
|
|
252
|
-
args.value = _fieldName;
|
|
253
|
-
return this.setImpl(args);
|
|
254
|
-
}
|
|
255
|
-
args.fieldName = _fieldName;
|
|
256
|
-
args.value = _value;
|
|
257
|
-
return this.setImpl(args);
|
|
258
|
-
};
|
|
259
|
-
MockStore.prototype.reset = function () {
|
|
260
|
-
this.store = {};
|
|
261
|
-
};
|
|
262
|
-
MockStore.prototype.filter = function (key, predicate) {
|
|
263
|
-
var entity = this.store[key];
|
|
264
|
-
return Object.values(entity).filter(predicate);
|
|
265
|
-
};
|
|
266
|
-
MockStore.prototype.find = function (key, predicate) {
|
|
267
|
-
var entity = this.store[key];
|
|
268
|
-
return Object.values(entity).find(predicate);
|
|
269
|
-
};
|
|
270
|
-
MockStore.prototype.getImpl = function (args) {
|
|
271
|
-
var _this = this;
|
|
272
|
-
var typeName = args.typeName, key = args.key, fieldName = args.fieldName, fieldArgs = args.fieldArgs, defaultValue = args.defaultValue;
|
|
273
|
-
if (!fieldName) {
|
|
274
|
-
if (defaultValue !== undefined && !isRecord(defaultValue)) {
|
|
275
|
-
throw new Error('`defaultValue` should be an object');
|
|
276
|
-
}
|
|
277
|
-
var valuesToInsert = defaultValue || {};
|
|
278
|
-
if (key) {
|
|
279
|
-
valuesToInsert = tslib.__assign(tslib.__assign({}, valuesToInsert), makeRef(typeName, key));
|
|
280
|
-
}
|
|
281
|
-
return this.insert(typeName, valuesToInsert, true);
|
|
282
|
-
}
|
|
283
|
-
assertIsDefined(key, 'key argument should be given when fieldName is given');
|
|
284
|
-
var fieldNameInStore = getFieldNameInStore(fieldName, fieldArgs);
|
|
285
|
-
if (this.store[typeName] === undefined ||
|
|
286
|
-
this.store[typeName][key] === undefined ||
|
|
287
|
-
this.store[typeName][key][fieldNameInStore] === undefined) {
|
|
288
|
-
var value = void 0;
|
|
289
|
-
if (defaultValue !== undefined) {
|
|
290
|
-
value = defaultValue;
|
|
291
|
-
}
|
|
292
|
-
else if (this.isKeyField(typeName, fieldName)) {
|
|
293
|
-
value = key;
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
value = this.generateFieldValue(typeName, fieldName, function (otherFieldName, otherValue) {
|
|
297
|
-
// if we get a key field in the mix we don't care
|
|
298
|
-
if (_this.isKeyField(typeName, otherFieldName))
|
|
299
|
-
return;
|
|
300
|
-
_this.set({ typeName: typeName, key: key, fieldName: otherFieldName, value: otherValue, noOverride: true });
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
this.set({ typeName: typeName, key: key, fieldName: fieldName, fieldArgs: fieldArgs, value: value, noOverride: true });
|
|
304
|
-
}
|
|
305
|
-
return this.store[typeName][key][fieldNameInStore];
|
|
306
|
-
};
|
|
307
|
-
MockStore.prototype.setImpl = function (args) {
|
|
308
|
-
var _a;
|
|
309
|
-
var _this = this;
|
|
310
|
-
var typeName = args.typeName, key = args.key, fieldName = args.fieldName, fieldArgs = args.fieldArgs, noOverride = args.noOverride;
|
|
311
|
-
var value = args.value;
|
|
312
|
-
if (isMockList(value)) {
|
|
313
|
-
value = deepResolveMockList(value);
|
|
314
|
-
}
|
|
315
|
-
if (!fieldName) {
|
|
316
|
-
if (!isRecord(value)) {
|
|
317
|
-
throw new Error('When no `fieldName` is provided, `value` should be a record.');
|
|
318
|
-
}
|
|
319
|
-
for (var fieldName_1 in value) {
|
|
320
|
-
this.setImpl({
|
|
321
|
-
typeName: typeName,
|
|
322
|
-
key: key,
|
|
323
|
-
fieldName: fieldName_1,
|
|
324
|
-
value: value[fieldName_1],
|
|
325
|
-
noOverride: noOverride,
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
var fieldNameInStore = getFieldNameInStore(fieldName, fieldArgs);
|
|
331
|
-
if (this.isKeyField(typeName, fieldName) && value !== key) {
|
|
332
|
-
throw new Error("Field " + fieldName + " is a key field of " + typeName + " and you are trying to set it to " + value + " while the key is " + key);
|
|
333
|
-
}
|
|
334
|
-
if (this.store[typeName] === undefined) {
|
|
335
|
-
this.store[typeName] = {};
|
|
336
|
-
}
|
|
337
|
-
if (this.store[typeName][key] === undefined) {
|
|
338
|
-
this.store[typeName][key] = {};
|
|
339
|
-
}
|
|
340
|
-
// if already set and we don't override
|
|
341
|
-
if (this.store[typeName][key][fieldNameInStore] !== undefined && noOverride) {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
var fieldType = this.getFieldType(typeName, fieldName);
|
|
345
|
-
var currentValue = this.store[typeName][key][fieldNameInStore];
|
|
346
|
-
var valueToStore;
|
|
347
|
-
try {
|
|
348
|
-
valueToStore = this.normalizeValueToStore(fieldType, value, currentValue, function (typeName, values) {
|
|
349
|
-
return _this.insert(typeName, values, noOverride);
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
catch (e) {
|
|
353
|
-
throw new Error("Value to set in " + typeName + "." + fieldName + " in not normalizable: " + e.message);
|
|
354
|
-
}
|
|
355
|
-
this.store[typeName][key] = tslib.__assign(tslib.__assign({}, this.store[typeName][key]), (_a = {}, _a[fieldNameInStore] = valueToStore, _a));
|
|
356
|
-
};
|
|
357
|
-
MockStore.prototype.normalizeValueToStore = function (fieldType, value, currentValue, onInsertType) {
|
|
358
|
-
var _this = this;
|
|
359
|
-
var fieldTypeName = fieldType.toString();
|
|
360
|
-
if (value === null) {
|
|
361
|
-
if (!graphql.isNullableType(fieldType)) {
|
|
362
|
-
throw new Error("should not be null because " + fieldTypeName + " is not nullable. Received null.");
|
|
363
|
-
}
|
|
364
|
-
return null;
|
|
365
|
-
}
|
|
366
|
-
var nullableFieldType = graphql.getNullableType(fieldType);
|
|
367
|
-
if (value === undefined)
|
|
368
|
-
return this.generateValueFromType(nullableFieldType);
|
|
369
|
-
// deal with nesting insert
|
|
370
|
-
if (graphql.isCompositeType(nullableFieldType)) {
|
|
371
|
-
if (!isRecord(value))
|
|
372
|
-
throw new Error("should be an object or null or undefined. Received " + value);
|
|
373
|
-
var joinedTypeName = void 0;
|
|
374
|
-
if (graphql.isAbstractType(nullableFieldType)) {
|
|
375
|
-
if (isRef(value)) {
|
|
376
|
-
joinedTypeName = value.$ref.typeName;
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
if (typeof value['__typename'] !== 'string') {
|
|
380
|
-
throw new Error("should contain a '__typename' because " + nullableFieldType.name + " an abstract type");
|
|
381
|
-
}
|
|
382
|
-
joinedTypeName = value['__typename'];
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
joinedTypeName = nullableFieldType.name;
|
|
387
|
-
}
|
|
388
|
-
return onInsertType(joinedTypeName, isRef(currentValue) ? tslib.__assign(tslib.__assign({}, currentValue), value) : value);
|
|
389
|
-
}
|
|
390
|
-
if (graphql.isListType(nullableFieldType)) {
|
|
391
|
-
if (!Array.isArray(value))
|
|
392
|
-
throw new Error("should be an array or null or undefined. Received " + value);
|
|
393
|
-
return value.map(function (v, index) {
|
|
394
|
-
return _this.normalizeValueToStore(nullableFieldType.ofType, v, typeof currentValue === 'object' && currentValue != null && currentValue[index] ? currentValue : undefined, onInsertType);
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
return value;
|
|
398
|
-
};
|
|
399
|
-
MockStore.prototype.insert = function (typeName, values, noOverride) {
|
|
400
|
-
var keyFieldName = this.getKeyFieldName(typeName);
|
|
401
|
-
var key;
|
|
402
|
-
// when we generate a key for the type, we might produce
|
|
403
|
-
// other associated values with it
|
|
404
|
-
// We keep track of them and we'll insert them, with propririty
|
|
405
|
-
// for the ones that we areasked to insert
|
|
406
|
-
var otherValues = {};
|
|
407
|
-
if (isRef(values)) {
|
|
408
|
-
key = values.$ref.key;
|
|
409
|
-
}
|
|
410
|
-
else if (keyFieldName && keyFieldName in values) {
|
|
411
|
-
key = values[keyFieldName];
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
key = this.generateKeyForType(typeName, function (otherFieldName, otherFieldValue) {
|
|
415
|
-
otherValues[otherFieldName] = otherFieldValue;
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
var toInsert = tslib.__assign(tslib.__assign({}, otherValues), values);
|
|
419
|
-
for (var fieldName in toInsert) {
|
|
420
|
-
if (fieldName === '$ref')
|
|
421
|
-
continue;
|
|
422
|
-
if (fieldName === '__typename')
|
|
423
|
-
continue;
|
|
424
|
-
this.set({
|
|
425
|
-
typeName: typeName,
|
|
426
|
-
key: key,
|
|
427
|
-
fieldName: fieldName,
|
|
428
|
-
value: toInsert[fieldName],
|
|
429
|
-
noOverride: noOverride,
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
return makeRef(typeName, key);
|
|
433
|
-
};
|
|
434
|
-
MockStore.prototype.generateFieldValue = function (typeName, fieldName, onOtherFieldsGenerated) {
|
|
435
|
-
var mockedValue = this.generateFieldValueFromMocks(typeName, fieldName, onOtherFieldsGenerated);
|
|
436
|
-
if (mockedValue !== undefined)
|
|
437
|
-
return mockedValue;
|
|
438
|
-
var fieldType = this.getFieldType(typeName, fieldName);
|
|
439
|
-
return this.generateValueFromType(fieldType);
|
|
440
|
-
};
|
|
441
|
-
MockStore.prototype.generateFieldValueFromMocks = function (typeName, fieldName, onOtherFieldsGenerated) {
|
|
442
|
-
var e_1, _a;
|
|
443
|
-
var value;
|
|
444
|
-
var mock = this.mocks ? this.mocks[typeName] : undefined;
|
|
445
|
-
if (mock) {
|
|
446
|
-
if (typeof mock === 'function') {
|
|
447
|
-
var values = mock();
|
|
448
|
-
if (typeof values !== 'object' || values == null) {
|
|
449
|
-
throw new Error("Value returned by the mock for " + typeName + " is not an object");
|
|
450
|
-
}
|
|
451
|
-
for (var otherFieldName in values) {
|
|
452
|
-
if (otherFieldName === fieldName)
|
|
453
|
-
continue;
|
|
454
|
-
if (typeof values[otherFieldName] === 'function')
|
|
455
|
-
continue;
|
|
456
|
-
onOtherFieldsGenerated && onOtherFieldsGenerated(otherFieldName, values[otherFieldName]);
|
|
457
|
-
}
|
|
458
|
-
value = values[fieldName];
|
|
459
|
-
if (typeof value === 'function')
|
|
460
|
-
value = value();
|
|
461
|
-
}
|
|
462
|
-
else if (typeof mock === 'object' && mock != null && typeof mock[fieldName] === 'function') {
|
|
463
|
-
value = mock[fieldName]();
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
if (value !== undefined)
|
|
467
|
-
return value;
|
|
468
|
-
var type = this.getType(typeName);
|
|
469
|
-
// GraphQL 14 Compatibility
|
|
470
|
-
var interfaces = 'getInterfaces' in type ? type.getInterfaces() : [];
|
|
471
|
-
if (interfaces.length > 0) {
|
|
472
|
-
try {
|
|
473
|
-
for (var interfaces_1 = tslib.__values(interfaces), interfaces_1_1 = interfaces_1.next(); !interfaces_1_1.done; interfaces_1_1 = interfaces_1.next()) {
|
|
474
|
-
var interface_ = interfaces_1_1.value;
|
|
475
|
-
if (value)
|
|
476
|
-
break;
|
|
477
|
-
value = this.generateFieldValueFromMocks(interface_.name, fieldName, onOtherFieldsGenerated);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
481
|
-
finally {
|
|
482
|
-
try {
|
|
483
|
-
if (interfaces_1_1 && !interfaces_1_1.done && (_a = interfaces_1.return)) _a.call(interfaces_1);
|
|
484
|
-
}
|
|
485
|
-
finally { if (e_1) throw e_1.error; }
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
return value;
|
|
489
|
-
};
|
|
490
|
-
MockStore.prototype.generateKeyForType = function (typeName, onOtherFieldsGenerated) {
|
|
491
|
-
var keyFieldName = this.getKeyFieldName(typeName);
|
|
492
|
-
if (!keyFieldName)
|
|
493
|
-
return uuidv4();
|
|
494
|
-
return this.generateFieldValue(typeName, keyFieldName, onOtherFieldsGenerated);
|
|
495
|
-
};
|
|
496
|
-
MockStore.prototype.generateValueFromType = function (fieldType) {
|
|
497
|
-
var _this = this;
|
|
498
|
-
var nullableType = graphql.getNullableType(fieldType);
|
|
499
|
-
if (graphql.isScalarType(nullableType)) {
|
|
500
|
-
var mockFn = this.mocks[nullableType.name];
|
|
501
|
-
if (typeof mockFn !== 'function')
|
|
502
|
-
throw new Error("No mock defined for type \"" + nullableType.name + "\"");
|
|
503
|
-
return mockFn();
|
|
504
|
-
}
|
|
505
|
-
else if (graphql.isEnumType(nullableType)) {
|
|
506
|
-
var mockFn = this.mocks[nullableType.name];
|
|
507
|
-
if (typeof mockFn === 'function')
|
|
508
|
-
return mockFn();
|
|
509
|
-
var values = nullableType.getValues().map(function (v) { return v.value; });
|
|
510
|
-
return takeRandom(values);
|
|
511
|
-
}
|
|
512
|
-
else if (graphql.isObjectType(nullableType)) {
|
|
513
|
-
// this will create a new random ref
|
|
514
|
-
return this.insert(nullableType.name, {});
|
|
515
|
-
}
|
|
516
|
-
else if (graphql.isListType(nullableType)) {
|
|
517
|
-
return tslib.__spreadArray([], tslib.__read(new Array(randomListLength())), false).map(function () { return _this.generateValueFromType(nullableType.ofType); });
|
|
518
|
-
}
|
|
519
|
-
else if (graphql.isAbstractType(nullableType)) {
|
|
520
|
-
var mock = this.mocks[nullableType.name];
|
|
521
|
-
var typeName = void 0;
|
|
522
|
-
var values = {};
|
|
523
|
-
if (!mock) {
|
|
524
|
-
typeName = takeRandom(this.schema.getPossibleTypes(nullableType).map(function (t) { return t.name; }));
|
|
525
|
-
}
|
|
526
|
-
else if (typeof mock === 'function') {
|
|
527
|
-
var mockRes = mock();
|
|
528
|
-
if (mockRes === null)
|
|
529
|
-
return null;
|
|
530
|
-
if (!isRecord(mockRes)) {
|
|
531
|
-
throw new Error("Value returned by the mock for " + nullableType.name + " is not an object or null");
|
|
532
|
-
}
|
|
533
|
-
values = mockRes;
|
|
534
|
-
if (typeof values['__typename'] !== 'string') {
|
|
535
|
-
throw new Error("Please return a __typename in \"" + nullableType.name + "\"");
|
|
536
|
-
}
|
|
537
|
-
typeName = values['__typename'];
|
|
538
|
-
}
|
|
539
|
-
else if (typeof mock === 'object' && mock != null && typeof mock['__typename'] === 'function') {
|
|
540
|
-
var mockRes = mock['__typename']();
|
|
541
|
-
if (typeof mockRes !== 'string')
|
|
542
|
-
throw new Error("'__typename' returned by the mock for abstract type " + nullableType.name + " is not a string");
|
|
543
|
-
typeName = mockRes;
|
|
544
|
-
}
|
|
545
|
-
else {
|
|
546
|
-
throw new Error("Please return a __typename in \"" + nullableType.name + "\"");
|
|
547
|
-
}
|
|
548
|
-
var toInsert = {};
|
|
549
|
-
for (var fieldName in values) {
|
|
550
|
-
if (fieldName === '__typename')
|
|
551
|
-
continue;
|
|
552
|
-
var fieldValue = values[fieldName];
|
|
553
|
-
toInsert[fieldName] = typeof fieldValue === 'function' ? fieldValue() : fieldValue;
|
|
554
|
-
}
|
|
555
|
-
return this.insert(typeName, toInsert);
|
|
556
|
-
}
|
|
557
|
-
else {
|
|
558
|
-
throw new Error(nullableType + " not implemented");
|
|
559
|
-
}
|
|
560
|
-
};
|
|
561
|
-
MockStore.prototype.getFieldType = function (typeName, fieldName) {
|
|
562
|
-
if (fieldName === '__typename') {
|
|
563
|
-
return graphql.GraphQLString;
|
|
564
|
-
}
|
|
565
|
-
var type = this.getType(typeName);
|
|
566
|
-
var field = type.getFields()[fieldName];
|
|
567
|
-
if (!field) {
|
|
568
|
-
throw new Error(fieldName + " does not exist on type " + typeName);
|
|
569
|
-
}
|
|
570
|
-
return field.type;
|
|
571
|
-
};
|
|
572
|
-
MockStore.prototype.getType = function (typeName) {
|
|
573
|
-
var type = this.schema.getType(typeName);
|
|
574
|
-
if (!type || !(graphql.isObjectType(type) || graphql.isInterfaceType(type))) {
|
|
575
|
-
throw new Error(typeName + " does not exist on schema or is not an object or interface");
|
|
576
|
-
}
|
|
577
|
-
return type;
|
|
578
|
-
};
|
|
579
|
-
MockStore.prototype.isKeyField = function (typeName, fieldName) {
|
|
580
|
-
return this.getKeyFieldName(typeName) === fieldName;
|
|
581
|
-
};
|
|
582
|
-
MockStore.prototype.getKeyFieldName = function (typeName) {
|
|
583
|
-
var _a;
|
|
584
|
-
var typePolicyKeyField = (_a = this.typePolicies[typeName]) === null || _a === void 0 ? void 0 : _a.keyFieldName;
|
|
585
|
-
if (typePolicyKeyField !== undefined) {
|
|
586
|
-
if (typePolicyKeyField === false)
|
|
587
|
-
return null;
|
|
588
|
-
return typePolicyKeyField;
|
|
589
|
-
}
|
|
590
|
-
// How about common key field names?
|
|
591
|
-
var gqlType = this.getType(typeName);
|
|
592
|
-
for (var fieldName in gqlType.getFields()) {
|
|
593
|
-
if (defaultKeyFieldNames.includes(fieldName)) {
|
|
594
|
-
return fieldName;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
return null;
|
|
598
|
-
};
|
|
599
|
-
return MockStore;
|
|
600
|
-
}());
|
|
601
|
-
var getFieldNameInStore = function (fieldName, fieldArgs) {
|
|
602
|
-
if (!fieldArgs)
|
|
603
|
-
return fieldName;
|
|
604
|
-
if (typeof fieldArgs === 'string') {
|
|
605
|
-
return fieldName + ":" + fieldArgs;
|
|
606
|
-
}
|
|
607
|
-
// empty args
|
|
608
|
-
if (Object.keys(fieldArgs).length === 0) {
|
|
609
|
-
return fieldName;
|
|
610
|
-
}
|
|
611
|
-
return fieldName + ":" + stringify(fieldArgs);
|
|
612
|
-
};
|
|
613
|
-
function assertIsDefined(value, message) {
|
|
614
|
-
if (value !== undefined && value !== null) {
|
|
615
|
-
return;
|
|
616
|
-
}
|
|
617
|
-
throw new Error(process.env['NODE_ENV'] === 'production' ? 'Invariant failed:' : "Invariant failed: " + (message || ''));
|
|
618
|
-
}
|
|
619
|
-
/**
|
|
620
|
-
* Will create `MockStore` for the given `schema`.
|
|
621
|
-
*
|
|
622
|
-
* A `MockStore` will generate mock values for the given schem when queried.
|
|
623
|
-
*
|
|
624
|
-
* It will stores generated mocks, so that, provided with same arguments
|
|
625
|
-
* the returned values will be the same.
|
|
626
|
-
*
|
|
627
|
-
* Its API also allows to modify the stored values.
|
|
628
|
-
*
|
|
629
|
-
* Basic example:
|
|
630
|
-
* ```ts
|
|
631
|
-
* store.get('User', 1, 'name');
|
|
632
|
-
* // > "Hello World"
|
|
633
|
-
* store.set('User', 1, 'name', 'Alexandre');
|
|
634
|
-
* store.get('User', 1, 'name');
|
|
635
|
-
* // > "Alexandre"
|
|
636
|
-
* ```
|
|
637
|
-
*
|
|
638
|
-
* The storage key will correspond to the "key field"
|
|
639
|
-
* of the type. Field with name `id` or `_id` will be
|
|
640
|
-
* by default considered as the key field for the type.
|
|
641
|
-
* However, use `typePolicies` to precise the field to use
|
|
642
|
-
* as key.
|
|
643
|
-
*/
|
|
644
|
-
function createMockStore(options) {
|
|
645
|
-
return new MockStore(options);
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
// todo: add option to preserve resolver
|
|
649
|
-
/**
|
|
650
|
-
* Given a `schema` and a `MockStore`, returns an executable schema that
|
|
651
|
-
* will use the provided `MockStore` to execute queries.
|
|
652
|
-
*
|
|
653
|
-
* ```ts
|
|
654
|
-
* const schema = buildSchema(`
|
|
655
|
-
* type User {
|
|
656
|
-
* id: ID!
|
|
657
|
-
* name: String!
|
|
658
|
-
* }
|
|
659
|
-
* type Query {
|
|
660
|
-
* me: User!
|
|
661
|
-
* }
|
|
662
|
-
* `)
|
|
663
|
-
*
|
|
664
|
-
* const store = createMockStore({ schema });
|
|
665
|
-
* const mockedSchema = addMocksToSchema({ schema, store });
|
|
666
|
-
* ```
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
* If a `resolvers` parameter is passed, the query execution will use
|
|
670
|
-
* the provided `resolvers` if, one exists, instead of the default mock
|
|
671
|
-
* resolver.
|
|
672
|
-
*
|
|
673
|
-
*
|
|
674
|
-
* ```ts
|
|
675
|
-
* const schema = buildSchema(`
|
|
676
|
-
* type User {
|
|
677
|
-
* id: ID!
|
|
678
|
-
* name: String!
|
|
679
|
-
* }
|
|
680
|
-
* type Query {
|
|
681
|
-
* me: User!
|
|
682
|
-
* }
|
|
683
|
-
* type Mutation {
|
|
684
|
-
* setMyName(newName: String!): User!
|
|
685
|
-
* }
|
|
686
|
-
* `)
|
|
687
|
-
*
|
|
688
|
-
* const store = createMockStore({ schema });
|
|
689
|
-
* const mockedSchema = addMocksToSchema({
|
|
690
|
-
* schema,
|
|
691
|
-
* store,
|
|
692
|
-
* resolvers: {
|
|
693
|
-
* Mutation: {
|
|
694
|
-
* setMyName: (_, { newName }) => {
|
|
695
|
-
* const ref = store.get('Query', 'ROOT', 'viewer');
|
|
696
|
-
* store.set(ref, 'name', newName);
|
|
697
|
-
* return ref;
|
|
698
|
-
* }
|
|
699
|
-
* }
|
|
700
|
-
* }
|
|
701
|
-
* });
|
|
702
|
-
* ```
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
* `Query` and `Mutation` type will use `key` `'ROOT'`.
|
|
706
|
-
*/
|
|
707
|
-
function addMocksToSchema(_a) {
|
|
708
|
-
var _b;
|
|
709
|
-
var _this = this;
|
|
710
|
-
var schema$1 = _a.schema, maybeStore = _a.store, mocks = _a.mocks, typePolicies = _a.typePolicies, resolversOrFnResolvers = _a.resolvers, _c = _a.preserveResolvers, preserveResolvers = _c === void 0 ? false : _c;
|
|
711
|
-
if (!schema$1) {
|
|
712
|
-
throw new Error('Must provide schema to mock');
|
|
713
|
-
}
|
|
714
|
-
if (!graphql.isSchema(schema$1)) {
|
|
715
|
-
throw new Error('Value at "schema" must be of type GraphQLSchema');
|
|
716
|
-
}
|
|
717
|
-
if (mocks && !isObject(mocks)) {
|
|
718
|
-
throw new Error('mocks must be of type Object');
|
|
719
|
-
}
|
|
720
|
-
var store = maybeStore ||
|
|
721
|
-
createMockStore({
|
|
722
|
-
schema: schema$1,
|
|
723
|
-
mocks: mocks,
|
|
724
|
-
typePolicies: typePolicies,
|
|
725
|
-
});
|
|
726
|
-
var resolvers = typeof resolversOrFnResolvers === 'function' ? resolversOrFnResolvers(store) : resolversOrFnResolvers;
|
|
727
|
-
var mockResolver = function (source, args, contex, info) {
|
|
728
|
-
var defaultResolvedValue = graphql.defaultFieldResolver(source, args, contex, info);
|
|
729
|
-
// priority to default resolved value
|
|
730
|
-
if (defaultResolvedValue !== undefined)
|
|
731
|
-
return defaultResolvedValue;
|
|
732
|
-
if (isRef(source)) {
|
|
733
|
-
return store.get({
|
|
734
|
-
typeName: source.$ref.typeName,
|
|
735
|
-
key: source.$ref.key,
|
|
736
|
-
fieldName: info.fieldName,
|
|
737
|
-
fieldArgs: args,
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
// we have to handle the root mutation, root query and root subscription types
|
|
741
|
-
// differently, because no resolver is called at the root
|
|
742
|
-
if (isRootType(info.parentType, info.schema)) {
|
|
743
|
-
return store.get({
|
|
744
|
-
typeName: info.parentType.name,
|
|
745
|
-
key: 'ROOT',
|
|
746
|
-
fieldName: info.fieldName,
|
|
747
|
-
fieldArgs: args,
|
|
748
|
-
});
|
|
749
|
-
}
|
|
750
|
-
return undefined;
|
|
751
|
-
};
|
|
752
|
-
var typeResolver = function (data) {
|
|
753
|
-
if (isRef(data)) {
|
|
754
|
-
return data.$ref.typeName;
|
|
755
|
-
}
|
|
756
|
-
};
|
|
757
|
-
var mockSubscriber = function () {
|
|
758
|
-
var _a;
|
|
759
|
-
return (_a = {},
|
|
760
|
-
_a[Symbol.asyncIterator] = function () {
|
|
761
|
-
return {
|
|
762
|
-
next: function () {
|
|
763
|
-
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
764
|
-
return tslib.__generator(this, function (_a) {
|
|
765
|
-
return [2 /*return*/, {
|
|
766
|
-
done: true,
|
|
767
|
-
value: {},
|
|
768
|
-
}];
|
|
769
|
-
});
|
|
770
|
-
});
|
|
771
|
-
},
|
|
772
|
-
};
|
|
773
|
-
},
|
|
774
|
-
_a);
|
|
775
|
-
};
|
|
776
|
-
var schemaWithMocks = utils.mapSchema(schema$1, (_b = {},
|
|
777
|
-
_b[utils.MapperKind.OBJECT_FIELD] = function (fieldConfig) {
|
|
778
|
-
var newFieldConfig = tslib.__assign({}, fieldConfig);
|
|
779
|
-
var oldResolver = fieldConfig.resolve;
|
|
780
|
-
if (!preserveResolvers || !oldResolver) {
|
|
781
|
-
newFieldConfig.resolve = mockResolver;
|
|
782
|
-
}
|
|
783
|
-
else {
|
|
784
|
-
newFieldConfig.resolve = function (rootObject, args, context, info) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
785
|
-
var _a, mockedValue, resolvedValue, emptyObject;
|
|
786
|
-
return tslib.__generator(this, function (_b) {
|
|
787
|
-
switch (_b.label) {
|
|
788
|
-
case 0: return [4 /*yield*/, Promise.all([
|
|
789
|
-
mockResolver(rootObject, args, context, info),
|
|
790
|
-
oldResolver(rootObject, args, context, info),
|
|
791
|
-
])];
|
|
792
|
-
case 1:
|
|
793
|
-
_a = tslib.__read.apply(void 0, [_b.sent(), 2]), mockedValue = _a[0], resolvedValue = _a[1];
|
|
794
|
-
// In case we couldn't mock
|
|
795
|
-
if (mockedValue instanceof Error) {
|
|
796
|
-
// only if value was not resolved, populate the error.
|
|
797
|
-
if (undefined === resolvedValue) {
|
|
798
|
-
throw mockedValue;
|
|
799
|
-
}
|
|
800
|
-
return [2 /*return*/, resolvedValue];
|
|
801
|
-
}
|
|
802
|
-
if (resolvedValue instanceof Date && mockedValue instanceof Date) {
|
|
803
|
-
return [2 /*return*/, undefined !== resolvedValue ? resolvedValue : mockedValue];
|
|
804
|
-
}
|
|
805
|
-
if (isObject(mockedValue) && isObject(resolvedValue)) {
|
|
806
|
-
emptyObject = Object.create(Object.getPrototypeOf(resolvedValue));
|
|
807
|
-
return [2 /*return*/, copyOwnProps(emptyObject, resolvedValue, mockedValue)];
|
|
808
|
-
}
|
|
809
|
-
return [2 /*return*/, undefined !== resolvedValue ? resolvedValue : mockedValue];
|
|
810
|
-
}
|
|
811
|
-
});
|
|
812
|
-
}); };
|
|
813
|
-
}
|
|
814
|
-
var fieldSubscriber = fieldConfig.subscribe;
|
|
815
|
-
if (!preserveResolvers || !fieldSubscriber) {
|
|
816
|
-
newFieldConfig.subscribe = mockSubscriber;
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
newFieldConfig.subscribe = function (rootObject, args, context, info) { return tslib.__awaiter(_this, void 0, void 0, function () {
|
|
820
|
-
var _a, mockAsyncIterable, oldAsyncIterable;
|
|
821
|
-
return tslib.__generator(this, function (_b) {
|
|
822
|
-
switch (_b.label) {
|
|
823
|
-
case 0: return [4 /*yield*/, Promise.all([
|
|
824
|
-
mockSubscriber(),
|
|
825
|
-
fieldSubscriber(rootObject, args, context, info),
|
|
826
|
-
])];
|
|
827
|
-
case 1:
|
|
828
|
-
_a = tslib.__read.apply(void 0, [_b.sent(), 2]), mockAsyncIterable = _a[0], oldAsyncIterable = _a[1];
|
|
829
|
-
return [2 /*return*/, oldAsyncIterable || mockAsyncIterable];
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
}); };
|
|
833
|
-
}
|
|
834
|
-
return newFieldConfig;
|
|
835
|
-
},
|
|
836
|
-
_b[utils.MapperKind.ABSTRACT_TYPE] = function (type) {
|
|
837
|
-
if (preserveResolvers && type.resolveType != null && type.resolveType.length) {
|
|
838
|
-
return;
|
|
839
|
-
}
|
|
840
|
-
if (graphql.isUnionType(type)) {
|
|
841
|
-
return new graphql.GraphQLUnionType(tslib.__assign(tslib.__assign({}, type.toConfig()), { resolveType: typeResolver }));
|
|
842
|
-
}
|
|
843
|
-
else {
|
|
844
|
-
return new graphql.GraphQLInterfaceType(tslib.__assign(tslib.__assign({}, type.toConfig()), { resolveType: typeResolver }));
|
|
845
|
-
}
|
|
846
|
-
},
|
|
847
|
-
_b));
|
|
848
|
-
return resolvers ? schema.addResolversToSchema(schemaWithMocks, resolvers) : schemaWithMocks;
|
|
849
|
-
}
|
|
850
|
-
var isRootType = function (type, schema) {
|
|
851
|
-
var rootTypeNames = utils.getRootTypeNames(schema);
|
|
852
|
-
return rootTypeNames.has(type.name);
|
|
853
|
-
};
|
|
854
|
-
|
|
855
|
-
/**
|
|
856
|
-
* A convenience wrapper on top of addMocksToSchema. It adds your mock resolvers
|
|
857
|
-
* to your schema and returns a client that will correctly execute your query with
|
|
858
|
-
* variables. Note: when executing queries from the returned server, context and
|
|
859
|
-
* root will both equal `{}`.
|
|
860
|
-
* @param schema The schema to which to add mocks. This can also be a set of type
|
|
861
|
-
* definitions instead.
|
|
862
|
-
* @param mocks The mocks to add to the schema.
|
|
863
|
-
* @param preserveResolvers Set to `true` to prevent existing resolvers from being
|
|
864
|
-
* overwritten to provide mock data. This can be used to mock some parts of the
|
|
865
|
-
* server and not others.
|
|
866
|
-
*/
|
|
867
|
-
function mockServer(schema$1, mocks, preserveResolvers) {
|
|
868
|
-
if (preserveResolvers === void 0) { preserveResolvers = false; }
|
|
869
|
-
var mockedSchema = addMocksToSchema({
|
|
870
|
-
schema: graphql.isSchema(schema$1)
|
|
871
|
-
? schema$1
|
|
872
|
-
: schema.makeExecutableSchema({
|
|
873
|
-
typeDefs: schema$1,
|
|
874
|
-
}),
|
|
875
|
-
mocks: mocks,
|
|
876
|
-
preserveResolvers: preserveResolvers,
|
|
877
|
-
});
|
|
878
|
-
return {
|
|
879
|
-
query: function (query, vars) {
|
|
880
|
-
return graphql.graphql({
|
|
881
|
-
schema: mockedSchema,
|
|
882
|
-
source: query,
|
|
883
|
-
rootValue: {},
|
|
884
|
-
contextValue: {},
|
|
885
|
-
variableValues: vars,
|
|
886
|
-
});
|
|
887
|
-
},
|
|
888
|
-
};
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
exports.MockList = MockList;
|
|
892
|
-
exports.MockStore = MockStore;
|
|
893
|
-
exports.addMocksToSchema = addMocksToSchema;
|
|
894
|
-
exports.assertIsRef = assertIsRef;
|
|
895
|
-
exports.createMockStore = createMockStore;
|
|
896
|
-
exports.deepResolveMockList = deepResolveMockList;
|
|
897
|
-
exports.defaultMocks = defaultMocks;
|
|
898
|
-
exports.isMockList = isMockList;
|
|
899
|
-
exports.isRecord = isRecord;
|
|
900
|
-
exports.isRef = isRef;
|
|
901
|
-
exports.mockServer = mockServer;
|