|
1 | 1 | import mockMutationFn from './mutation'; |
2 | 2 | import mockQueryFn from './query'; |
3 | | -import { contextSet } from '../utils'; |
| 3 | +import { contextSet, reduceKeys } from '../utils'; |
| 4 | + |
| 5 | +const addRootMock = (mocks, rootType, mockFn, db, options) => |
| 6 | + contextSet(mocks, rootType.name, () => |
| 7 | + mockRootType(rootType._fields, mockFn, db, options)); |
| 8 | + |
| 9 | +const mockRootType = (fields = {}, mockFn, db, options) => |
| 10 | + reduceKeys(fields, (mocks, field) => |
| 11 | + contextSet(mocks, field, mockFn(db, options)), {}); |
4 | 12 |
|
5 | 13 | export function createMocksForSchema(schema, db, options) { |
6 | | - let { _mutationType, _queryType } = schema; |
| 14 | + let mocks = {}; |
7 | 15 |
|
8 | | - return { |
9 | | - RootMutationType: () => |
10 | | - mockRootType(_mutationType._fields, mockMutationFn, db, options), |
11 | | - RootQueryType: () => |
12 | | - mockRootType(_queryType._fields, mockQueryFn, db, options) |
13 | | - }; |
14 | | -} |
| 16 | + addRootMock(mocks, schema._queryType, mockQueryFn, db, options); |
| 17 | + addRootMock(mocks, schema._mutationType, mockMutationFn, db, options); |
15 | 18 |
|
16 | | -const mockRootType = (rootTypes = {}, mockFn, db, options) => |
17 | | - Object.keys(rootTypes).reduce((mocks, rootType) => |
18 | | - contextSet(mocks, rootType, mockFn(db, options)), {}); |
| 19 | + return mocks; |
| 20 | +} |
0 commit comments