1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var graphql_1 = require("graphql");
|
3 | var uuid = require("uuid");
|
4 | var makeExecutableSchema_1 = require("./makeExecutableSchema");
|
5 |
|
6 | function mockServer(schema, mocks, preserveResolvers) {
|
7 | if (preserveResolvers === void 0) { preserveResolvers = false; }
|
8 | var mySchema;
|
9 | if (!(schema instanceof graphql_1.GraphQLSchema)) {
|
10 |
|
11 | mySchema = makeExecutableSchema_1.buildSchemaFromTypeDefinitions(schema);
|
12 | }
|
13 | else {
|
14 | mySchema = schema;
|
15 | }
|
16 | addMockFunctionsToSchema({ schema: mySchema, mocks: mocks, preserveResolvers: preserveResolvers });
|
17 | return { query: function (query, vars) { return graphql_1.graphql(mySchema, query, {}, {}, vars); } };
|
18 | }
|
19 | exports.mockServer = mockServer;
|
20 | var defaultMockMap = new Map();
|
21 | defaultMockMap.set('Int', function () { return Math.round(Math.random() * 200) - 100; });
|
22 | defaultMockMap.set('Float', function () { return Math.random() * 200 - 100; });
|
23 | defaultMockMap.set('String', function () { return 'Hello World'; });
|
24 | defaultMockMap.set('Boolean', function () { return Math.random() > 0.5; });
|
25 | defaultMockMap.set('ID', function () { return uuid.v4(); });
|
26 |
|
27 |
|
28 |
|
29 | function addMockFunctionsToSchema(_a) {
|
30 | var schema = _a.schema, _b = _a.mocks, mocks = _b === void 0 ? {} : _b, _c = _a.preserveResolvers, preserveResolvers = _c === void 0 ? false : _c;
|
31 | if (!schema) {
|
32 | throw new Error('Must provide schema to mock');
|
33 | }
|
34 | if (!(schema instanceof graphql_1.GraphQLSchema)) {
|
35 | throw new Error('Value at "schema" must be of type GraphQLSchema');
|
36 | }
|
37 | if (!isObject(mocks)) {
|
38 | throw new Error('mocks must be of type Object');
|
39 | }
|
40 |
|
41 | var mockFunctionMap = new Map();
|
42 | Object.keys(mocks).forEach(function (typeName) {
|
43 | mockFunctionMap.set(typeName, mocks[typeName]);
|
44 | });
|
45 | mockFunctionMap.forEach(function (mockFunction, mockTypeName) {
|
46 | if (typeof mockFunction !== 'function') {
|
47 | throw new Error("mockFunctionMap[" + mockTypeName + "] must be a function");
|
48 | }
|
49 | });
|
50 | var mockType = function (type, typeName, fieldName) {
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | return function (root, args, context, info) {
|
59 |
|
60 | var fieldType = graphql_1.getNullableType(type);
|
61 | var namedFieldType = graphql_1.getNamedType(fieldType);
|
62 | if (root && typeof root[fieldName] !== 'undefined') {
|
63 | var result = void 0;
|
64 |
|
65 | if (typeof root[fieldName] === 'function') {
|
66 | result = root[fieldName](root, args, context, info);
|
67 | if (result instanceof MockList) {
|
68 | result = result.mock(root, args, context, info, fieldType, mockType);
|
69 | }
|
70 | }
|
71 | else {
|
72 | result = root[fieldName];
|
73 | }
|
74 |
|
75 |
|
76 | if (mockFunctionMap.has(namedFieldType.name)) {
|
77 | result = mergeMocks(mockFunctionMap
|
78 | .get(namedFieldType.name)
|
79 | .bind(null, root, args, context, info), result);
|
80 | }
|
81 | return result;
|
82 | }
|
83 | if (fieldType instanceof graphql_1.GraphQLList ||
|
84 | fieldType instanceof graphql_1.GraphQLNonNull) {
|
85 | return [
|
86 | mockType(fieldType.ofType)(root, args, context, info),
|
87 | mockType(fieldType.ofType)(root, args, context, info),
|
88 | ];
|
89 | }
|
90 | if (mockFunctionMap.has(fieldType.name) &&
|
91 | !(fieldType instanceof graphql_1.GraphQLUnionType ||
|
92 | fieldType instanceof graphql_1.GraphQLInterfaceType)) {
|
93 |
|
94 | return mockFunctionMap.get(fieldType.name)(root, args, context, info);
|
95 | }
|
96 | if (fieldType instanceof graphql_1.GraphQLObjectType) {
|
97 |
|
98 | return {};
|
99 | }
|
100 |
|
101 |
|
102 | if (fieldType instanceof graphql_1.GraphQLUnionType ||
|
103 | fieldType instanceof graphql_1.GraphQLInterfaceType) {
|
104 | var implementationType = void 0;
|
105 | if (mockFunctionMap.has(fieldType.name)) {
|
106 | var interfaceMockObj = mockFunctionMap.get(fieldType.name)(root, args, context, info);
|
107 | if (!interfaceMockObj || !interfaceMockObj.__typename) {
|
108 | return Error("Please return a __typename in \"" + fieldType.name + "\"");
|
109 | }
|
110 | implementationType = schema.getType(interfaceMockObj.__typename);
|
111 | }
|
112 | else {
|
113 | var possibleTypes = schema.getPossibleTypes(fieldType);
|
114 | implementationType = getRandomElement(possibleTypes);
|
115 | }
|
116 | return Object.assign({ __typename: implementationType }, mockType(implementationType)(root, args, context, info));
|
117 | }
|
118 | if (fieldType instanceof graphql_1.GraphQLEnumType) {
|
119 | return getRandomElement(fieldType.getValues()).value;
|
120 | }
|
121 | if (defaultMockMap.has(fieldType.name)) {
|
122 | return defaultMockMap.get(fieldType.name)(root, args, context, info);
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | return Error("No mock defined for type \"" + fieldType.name + "\"");
|
128 | };
|
129 | };
|
130 | makeExecutableSchema_1.forEachField(schema, function (field, typeName, fieldName) {
|
131 | assignResolveType(field.type, preserveResolvers);
|
132 | var mockResolver;
|
133 |
|
134 |
|
135 |
|
136 | var isOnQueryType = schema.getQueryType() && schema.getQueryType().name === typeName;
|
137 | var isOnMutationType = schema.getMutationType() && schema.getMutationType().name === typeName;
|
138 | if (isOnQueryType || isOnMutationType) {
|
139 | if (mockFunctionMap.has(typeName)) {
|
140 | var rootMock_1 = mockFunctionMap.get(typeName);
|
141 |
|
142 | if (typeof rootMock_1(undefined, {}, {}, {})[fieldName] === 'function') {
|
143 | mockResolver = function (root, args, context, info) {
|
144 | var updatedRoot = root || {};
|
145 | updatedRoot[fieldName] = rootMock_1(root, args, context, info)[fieldName];
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | return mockType(field.type, typeName, fieldName)(updatedRoot, args, context, info);
|
152 | };
|
153 | }
|
154 | }
|
155 | }
|
156 | if (!mockResolver) {
|
157 | mockResolver = mockType(field.type, typeName, fieldName);
|
158 | }
|
159 | if (!preserveResolvers || !field.resolve) {
|
160 | field.resolve = mockResolver;
|
161 | }
|
162 | else {
|
163 | var oldResolver_1 = field.resolve;
|
164 | field.resolve = function (rootObject, args, context, info) {
|
165 | return Promise.all([
|
166 | mockResolver(rootObject, args, context, info),
|
167 | oldResolver_1(rootObject, args, context, info),
|
168 | ]).then(function (values) {
|
169 | var mockedValue = values[0], resolvedValue = values[1];
|
170 |
|
171 | if (mockedValue instanceof Error) {
|
172 |
|
173 | if (undefined === resolvedValue) {
|
174 | throw mockedValue;
|
175 | }
|
176 | return resolvedValue;
|
177 | }
|
178 | if (resolvedValue instanceof Date && mockedValue instanceof Date) {
|
179 | return undefined !== resolvedValue ? resolvedValue : mockedValue;
|
180 | }
|
181 | if (isObject(mockedValue) && isObject(resolvedValue)) {
|
182 |
|
183 |
|
184 | var emptyObject = Object.create(Object.getPrototypeOf(resolvedValue));
|
185 | return copyOwnProps(emptyObject, resolvedValue, mockedValue);
|
186 | }
|
187 | return undefined !== resolvedValue ? resolvedValue : mockedValue;
|
188 | });
|
189 | };
|
190 | }
|
191 | });
|
192 | }
|
193 | exports.addMockFunctionsToSchema = addMockFunctionsToSchema;
|
194 | function isObject(thing) {
|
195 | return thing === Object(thing) && !Array.isArray(thing);
|
196 | }
|
197 |
|
198 | function getRandomElement(ary) {
|
199 | var sample = Math.floor(Math.random() * ary.length);
|
200 | return ary[sample];
|
201 | }
|
202 | function mergeObjects(a, b) {
|
203 | return Object.assign(a, b);
|
204 | }
|
205 | function copyOwnPropsIfNotPresent(target, source) {
|
206 | Object.getOwnPropertyNames(source).forEach(function (prop) {
|
207 | if (!Object.getOwnPropertyDescriptor(target, prop)) {
|
208 | Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop));
|
209 | }
|
210 | });
|
211 | }
|
212 | function copyOwnProps(target) {
|
213 | var sources = [];
|
214 | for (var _i = 1; _i < arguments.length; _i++) {
|
215 | sources[_i - 1] = arguments[_i];
|
216 | }
|
217 | sources.forEach(function (source) {
|
218 | var chain = source;
|
219 | while (chain) {
|
220 | copyOwnPropsIfNotPresent(target, chain);
|
221 | chain = Object.getPrototypeOf(chain);
|
222 | }
|
223 | });
|
224 | return target;
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function mergeMocks(genericMockFunction, customMock) {
|
231 | if (Array.isArray(customMock)) {
|
232 | return customMock.map(function (el) { return mergeMocks(genericMockFunction, el); });
|
233 | }
|
234 | if (isObject(customMock)) {
|
235 | return mergeObjects(genericMockFunction(), customMock);
|
236 | }
|
237 | return customMock;
|
238 | }
|
239 | function getResolveType(namedFieldType) {
|
240 | if (namedFieldType instanceof graphql_1.GraphQLInterfaceType ||
|
241 | namedFieldType instanceof graphql_1.GraphQLUnionType) {
|
242 | return namedFieldType.resolveType;
|
243 | }
|
244 | else {
|
245 | return undefined;
|
246 | }
|
247 | }
|
248 | function assignResolveType(type, preserveResolvers) {
|
249 | var fieldType = graphql_1.getNullableType(type);
|
250 | var namedFieldType = graphql_1.getNamedType(fieldType);
|
251 | var oldResolveType = getResolveType(namedFieldType);
|
252 | if (preserveResolvers && oldResolveType && oldResolveType.length) {
|
253 | return;
|
254 | }
|
255 | if (namedFieldType instanceof graphql_1.GraphQLUnionType ||
|
256 | namedFieldType instanceof graphql_1.GraphQLInterfaceType) {
|
257 |
|
258 |
|
259 | namedFieldType.resolveType = function (data, context, info) {
|
260 | return info.schema.getType(data.__typename);
|
261 | };
|
262 | }
|
263 | }
|
264 | var MockList = (function () {
|
265 |
|
266 | function MockList(len, wrappedFunction) {
|
267 | this.len = len;
|
268 | if (typeof wrappedFunction !== 'undefined') {
|
269 | if (typeof wrappedFunction !== 'function') {
|
270 | throw new Error('Second argument to MockList must be a function or undefined');
|
271 | }
|
272 | this.wrappedFunction = wrappedFunction;
|
273 | }
|
274 | }
|
275 | MockList.prototype.mock = function (root, args, context, info, fieldType, mockTypeFunc) {
|
276 | var arr;
|
277 | if (Array.isArray(this.len)) {
|
278 | arr = new Array(this.randint(this.len[0], this.len[1]));
|
279 | }
|
280 | else {
|
281 | arr = new Array(this.len);
|
282 | }
|
283 | for (var i = 0; i < arr.length; i++) {
|
284 | if (typeof this.wrappedFunction === 'function') {
|
285 | var res = this.wrappedFunction(root, args, context, info);
|
286 | if (res instanceof MockList) {
|
287 | var nullableType = graphql_1.getNullableType(fieldType.ofType);
|
288 | arr[i] = res.mock(root, args, context, info, nullableType, mockTypeFunc);
|
289 | }
|
290 | else {
|
291 | arr[i] = res;
|
292 | }
|
293 | }
|
294 | else {
|
295 | arr[i] = mockTypeFunc(fieldType.ofType)(root, args, context, info);
|
296 | }
|
297 | }
|
298 | return arr;
|
299 | };
|
300 | MockList.prototype.randint = function (low, high) {
|
301 | return Math.floor(Math.random() * (high - low + 1) + low);
|
302 | };
|
303 | return MockList;
|
304 | }());
|
305 | exports.MockList = MockList;
|
306 |
|
\ | No newline at end of file |