-
Notifications
You must be signed in to change notification settings - Fork 332
/
index.ts
180 lines (176 loc) · 7.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import faker from 'faker';
import type { GraphQLFieldResolver, GraphQLResolveInfo, GraphQLSchema } from 'graphql';
import { execute } from 'graphql';
import { mocks as graphqlScalarsMocks } from 'graphql-scalars';
import { getInterpolatedStringFactory } from '@graphql-mesh/string-interpolation';
import type { ImportFn, MeshPlugin, MeshPluginOptions, YamlConfig } from '@graphql-mesh/types';
import { loadFromModuleExportExpression } from '@graphql-mesh/utils';
import type { IMocks } from '@graphql-tools/mock';
import { addMocksToSchema, createMockStore } from '@graphql-tools/mock';
const mockedSchemas = new WeakSet<GraphQLSchema>();
export default function useMock(
config: YamlConfig.MockingConfig & {
baseDir?: string;
importFn?: ImportFn;
},
): MeshPlugin<{}> {
// eslint-disable-next-line no-new-func
const configIf = config != null && 'if' in config ? new Function(`return ${config.if}`)() : true;
if (configIf) {
return {
onSchemaChange({ schema, replaceSchema }) {
if (mockedSchemas.has(schema)) {
return;
}
const mocks: IMocks = {
...graphqlScalarsMocks,
};
const resolvers: any = {};
const typeMap = schema.getTypeMap();
for (const typeName in typeMap) {
const type = typeMap[typeName];
const examples = type.extensions.examples as any[];
if (examples?.length) {
mocks[typeName] = () => examples[Math.floor(Math.random() * examples.length)];
}
}
if (config?.mocks?.length) {
for (const fieldConfig of config.mocks) {
const fieldConfigIf = 'if' in fieldConfig ? fieldConfig.if : true;
if (fieldConfigIf) {
const [typeName, fieldName] = fieldConfig.apply.split('.');
if (fieldName) {
if (fieldConfig.faker) {
let fakerFn: Function; // eslint-disable-line
const [service, method] = fieldConfig.faker.split('.');
if (service in faker) {
fakerFn = () => (faker as any)[service][method]();
} else {
fakerFn = () => faker.fake(fieldConfig.faker || '');
}
resolvers[typeName] = resolvers[typeName] || {};
resolvers[typeName][fieldName] = fakerFn;
} else if (fieldConfig.custom) {
const exportedVal$ = loadFromModuleExportExpression<any>(fieldConfig.custom, {
cwd: config.baseDir,
defaultExportName: 'default',
importFn: config.importFn,
});
resolvers[typeName] = resolvers[typeName] || {};
resolvers[typeName][fieldName] = (
root: any,
args: any,
context: any,
info: GraphQLResolveInfo,
) => {
context = context || {};
context.mockStore = store;
return exportedVal$.then(exportedVal =>
typeof exportedVal === 'function'
? exportedVal(root, args, context, info)
: exportedVal,
);
};
} else if ('length' in fieldConfig) {
resolvers[typeName] = resolvers[typeName] || {};
resolvers[typeName][fieldName] = () => new Array(fieldConfig.length).fill({});
} else if ('updateStore' in fieldConfig) {
const getFromStoreKeyFactory = getInterpolatedStringFactory(
fieldConfig.store.key,
);
const updateStoreFactories = fieldConfig.updateStore.map(updateStoreConfig => ({
updateStoreConfig,
keyFactory: getInterpolatedStringFactory(updateStoreConfig.key),
valueFactory: getInterpolatedStringFactory(updateStoreConfig.value),
}));
resolvers[typeName] = resolvers[typeName] || {};
resolvers[typeName][fieldName] = (
root: any,
args: any,
context: any,
info: GraphQLResolveInfo,
) => {
const resolverData = {
root,
args,
context,
info,
random: Date.now().toString(),
env: process.env,
};
updateStoreFactories.forEach(
({ updateStoreConfig, keyFactory, valueFactory }) => {
const key = keyFactory(resolverData);
const value = valueFactory(resolverData);
store.set(updateStoreConfig.type, key, updateStoreConfig.fieldName, value);
},
);
const key = getFromStoreKeyFactory(resolverData);
return store.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
};
} else if ('store' in fieldConfig) {
const keyFactory = getInterpolatedStringFactory(fieldConfig.store.key);
resolvers[typeName] = resolvers[typeName] || {};
resolvers[typeName][fieldName] = (
root: any,
args: any,
context: any,
info: GraphQLResolveInfo,
) => {
const key = keyFactory({ root, args, context, info, env: process.env });
return store.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
};
}
} else {
if (fieldConfig.faker) {
let fakerFn: GraphQLFieldResolver<any, any, { [argName: string]: any }>;
const [service, method] = fieldConfig.faker.split('.');
if (service in faker) {
fakerFn = () => (faker as any)[service][method]();
} else {
fakerFn = () => faker.fake(fieldConfig.faker || '');
}
mocks[typeName] = fakerFn;
} else if (fieldConfig.custom) {
const exportedVal$ = loadFromModuleExportExpression<any>(fieldConfig.custom, {
cwd: config.baseDir,
defaultExportName: 'default',
importFn: config.importFn,
});
mocks[typeName] = () => {
return exportedVal$.then(exportedVal =>
typeof exportedVal === 'function' ? exportedVal(store) : exportedVal,
);
};
}
}
}
}
}
const store = createMockStore({ schema, mocks });
if (config?.initializeStore) {
const initializeStoreFn$ = loadFromModuleExportExpression(config.initializeStore, {
cwd: config.baseDir,
defaultExportName: 'default',
importFn: config.importFn,
});
// eslint-disable-next-line no-void
void initializeStoreFn$.then(fn => fn(store));
}
const mockedSchema = addMocksToSchema({
schema,
store,
mocks,
resolvers,
preserveResolvers: config?.preserveResolvers,
});
mockedSchemas.add(mockedSchema);
replaceSchema(mockedSchema);
},
onExecute({ setExecuteFn }) {
setExecuteFn(execute);
},
};
}
return {};
}