@graphql-mesh/plugin-mock 0.0.1-alpha-20220810133859-6f028675c
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/index.d.ts +3 -0
- package/index.js +152 -0
- package/index.mjs +148 -0
- package/package.json +44 -0
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
4
|
+
|
|
5
|
+
const faker = _interopDefault(require('faker'));
|
|
6
|
+
const graphqlScalars = require('graphql-scalars');
|
|
7
|
+
const mock = require('@graphql-tools/mock');
|
|
8
|
+
const utils = require('@graphql-mesh/utils');
|
|
9
|
+
const stringInterpolation = require('@graphql-mesh/string-interpolation');
|
|
10
|
+
const graphql = require('graphql');
|
|
11
|
+
|
|
12
|
+
const mockedSchemas = new WeakSet();
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
14
|
+
function useMock(config) {
|
|
15
|
+
const configIf = config != null && 'if' in config ? config.if : true;
|
|
16
|
+
if (configIf) {
|
|
17
|
+
return {
|
|
18
|
+
onSchemaChange({ schema, replaceSchema }) {
|
|
19
|
+
var _a;
|
|
20
|
+
if (mockedSchemas.has(schema)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const mocks = {
|
|
24
|
+
...graphqlScalars.mocks,
|
|
25
|
+
};
|
|
26
|
+
const resolvers = {};
|
|
27
|
+
const typeMap = schema.getTypeMap();
|
|
28
|
+
for (const typeName in typeMap) {
|
|
29
|
+
const type = typeMap[typeName];
|
|
30
|
+
const examples = type.extensions.examples;
|
|
31
|
+
if (examples === null || examples === void 0 ? void 0 : examples.length) {
|
|
32
|
+
mocks[typeName] = () => examples[Math.floor(Math.random() * examples.length)];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if ((_a = config === null || config === void 0 ? void 0 : config.mocks) === null || _a === void 0 ? void 0 : _a.length) {
|
|
36
|
+
for (const fieldConfig of config.mocks) {
|
|
37
|
+
const fieldConfigIf = 'if' in fieldConfig ? fieldConfig.if : true;
|
|
38
|
+
if (fieldConfigIf) {
|
|
39
|
+
const [typeName, fieldName] = fieldConfig.apply.split('.');
|
|
40
|
+
if (fieldName) {
|
|
41
|
+
if (fieldConfig.faker) {
|
|
42
|
+
let fakerFn; // eslint-disable-line
|
|
43
|
+
const [service, method] = fieldConfig.faker.split('.');
|
|
44
|
+
if (service in faker) {
|
|
45
|
+
fakerFn = () => faker[service][method]();
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
fakerFn = () => faker.fake(fieldConfig.faker || '');
|
|
49
|
+
}
|
|
50
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
51
|
+
resolvers[typeName][fieldName] = fakerFn;
|
|
52
|
+
}
|
|
53
|
+
else if (fieldConfig.custom) {
|
|
54
|
+
const exportedVal$ = utils.loadFromModuleExportExpression(fieldConfig.custom, {
|
|
55
|
+
cwd: config.baseDir,
|
|
56
|
+
defaultExportName: 'default',
|
|
57
|
+
importFn: config.importFn,
|
|
58
|
+
});
|
|
59
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
60
|
+
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
61
|
+
context = context || {};
|
|
62
|
+
context.mockStore = store;
|
|
63
|
+
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(root, args, context, info) : exportedVal);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else if ('length' in fieldConfig) {
|
|
67
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
68
|
+
resolvers[typeName][fieldName] = () => new Array(fieldConfig.length).fill({});
|
|
69
|
+
}
|
|
70
|
+
else if ('updateStore' in fieldConfig) {
|
|
71
|
+
const getFromStoreKeyFactory = stringInterpolation.getInterpolatedStringFactory(fieldConfig.store.key);
|
|
72
|
+
const updateStoreFactories = fieldConfig.updateStore.map(updateStoreConfig => ({
|
|
73
|
+
updateStoreConfig,
|
|
74
|
+
keyFactory: stringInterpolation.getInterpolatedStringFactory(updateStoreConfig.key),
|
|
75
|
+
valueFactory: stringInterpolation.getInterpolatedStringFactory(updateStoreConfig.value),
|
|
76
|
+
}));
|
|
77
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
78
|
+
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
79
|
+
const resolverData = { root, args, context, info, random: Date.now().toString(), env: process.env };
|
|
80
|
+
updateStoreFactories.forEach(({ updateStoreConfig, keyFactory, valueFactory }) => {
|
|
81
|
+
const key = keyFactory(resolverData);
|
|
82
|
+
const value = valueFactory(resolverData);
|
|
83
|
+
store.set(updateStoreConfig.type, key, updateStoreConfig.fieldName, value);
|
|
84
|
+
});
|
|
85
|
+
const key = getFromStoreKeyFactory(resolverData);
|
|
86
|
+
return store.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
else if ('store' in fieldConfig) {
|
|
90
|
+
const keyFactory = stringInterpolation.getInterpolatedStringFactory(fieldConfig.store.key);
|
|
91
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
92
|
+
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
93
|
+
const key = keyFactory({ root, args, context, info, env: process.env });
|
|
94
|
+
return store.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
if (fieldConfig.faker) {
|
|
100
|
+
let fakerFn;
|
|
101
|
+
const [service, method] = fieldConfig.faker.split('.');
|
|
102
|
+
if (service in faker) {
|
|
103
|
+
fakerFn = () => faker[service][method]();
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
fakerFn = () => faker.fake(fieldConfig.faker || '');
|
|
107
|
+
}
|
|
108
|
+
mocks[typeName] = fakerFn;
|
|
109
|
+
}
|
|
110
|
+
else if (fieldConfig.custom) {
|
|
111
|
+
const exportedVal$ = utils.loadFromModuleExportExpression(fieldConfig.custom, {
|
|
112
|
+
cwd: config.baseDir,
|
|
113
|
+
defaultExportName: 'default',
|
|
114
|
+
importFn: config.importFn,
|
|
115
|
+
});
|
|
116
|
+
mocks[typeName] = () => {
|
|
117
|
+
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(store) : exportedVal);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const store = mock.createMockStore({ schema, mocks });
|
|
125
|
+
if (config === null || config === void 0 ? void 0 : config.initializeStore) {
|
|
126
|
+
const initializeStoreFn$ = utils.loadFromModuleExportExpression(config.initializeStore, {
|
|
127
|
+
cwd: config.baseDir,
|
|
128
|
+
defaultExportName: 'default',
|
|
129
|
+
importFn: config.importFn,
|
|
130
|
+
});
|
|
131
|
+
// eslint-disable-next-line no-void
|
|
132
|
+
void initializeStoreFn$.then(fn => fn(store));
|
|
133
|
+
}
|
|
134
|
+
const mockedSchema = mock.addMocksToSchema({
|
|
135
|
+
schema,
|
|
136
|
+
store,
|
|
137
|
+
mocks,
|
|
138
|
+
resolvers,
|
|
139
|
+
preserveResolvers: config === null || config === void 0 ? void 0 : config.preserveResolvers,
|
|
140
|
+
});
|
|
141
|
+
mockedSchemas.add(mockedSchema);
|
|
142
|
+
replaceSchema(mockedSchema);
|
|
143
|
+
},
|
|
144
|
+
onExecute({ setExecuteFn }) {
|
|
145
|
+
setExecuteFn(graphql.execute);
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return {};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
module.exports = useMock;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import faker from 'faker';
|
|
2
|
+
import { mocks } from 'graphql-scalars';
|
|
3
|
+
import { createMockStore, addMocksToSchema } from '@graphql-tools/mock';
|
|
4
|
+
import { loadFromModuleExportExpression } from '@graphql-mesh/utils';
|
|
5
|
+
import { getInterpolatedStringFactory } from '@graphql-mesh/string-interpolation';
|
|
6
|
+
import { execute } from 'graphql';
|
|
7
|
+
|
|
8
|
+
const mockedSchemas = new WeakSet();
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
10
|
+
function useMock(config) {
|
|
11
|
+
const configIf = config != null && 'if' in config ? config.if : true;
|
|
12
|
+
if (configIf) {
|
|
13
|
+
return {
|
|
14
|
+
onSchemaChange({ schema, replaceSchema }) {
|
|
15
|
+
var _a;
|
|
16
|
+
if (mockedSchemas.has(schema)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const mocks$1 = {
|
|
20
|
+
...mocks,
|
|
21
|
+
};
|
|
22
|
+
const resolvers = {};
|
|
23
|
+
const typeMap = schema.getTypeMap();
|
|
24
|
+
for (const typeName in typeMap) {
|
|
25
|
+
const type = typeMap[typeName];
|
|
26
|
+
const examples = type.extensions.examples;
|
|
27
|
+
if (examples === null || examples === void 0 ? void 0 : examples.length) {
|
|
28
|
+
mocks$1[typeName] = () => examples[Math.floor(Math.random() * examples.length)];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if ((_a = config === null || config === void 0 ? void 0 : config.mocks) === null || _a === void 0 ? void 0 : _a.length) {
|
|
32
|
+
for (const fieldConfig of config.mocks) {
|
|
33
|
+
const fieldConfigIf = 'if' in fieldConfig ? fieldConfig.if : true;
|
|
34
|
+
if (fieldConfigIf) {
|
|
35
|
+
const [typeName, fieldName] = fieldConfig.apply.split('.');
|
|
36
|
+
if (fieldName) {
|
|
37
|
+
if (fieldConfig.faker) {
|
|
38
|
+
let fakerFn; // eslint-disable-line
|
|
39
|
+
const [service, method] = fieldConfig.faker.split('.');
|
|
40
|
+
if (service in faker) {
|
|
41
|
+
fakerFn = () => faker[service][method]();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
fakerFn = () => faker.fake(fieldConfig.faker || '');
|
|
45
|
+
}
|
|
46
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
47
|
+
resolvers[typeName][fieldName] = fakerFn;
|
|
48
|
+
}
|
|
49
|
+
else if (fieldConfig.custom) {
|
|
50
|
+
const exportedVal$ = loadFromModuleExportExpression(fieldConfig.custom, {
|
|
51
|
+
cwd: config.baseDir,
|
|
52
|
+
defaultExportName: 'default',
|
|
53
|
+
importFn: config.importFn,
|
|
54
|
+
});
|
|
55
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
56
|
+
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
57
|
+
context = context || {};
|
|
58
|
+
context.mockStore = store;
|
|
59
|
+
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(root, args, context, info) : exportedVal);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
else if ('length' in fieldConfig) {
|
|
63
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
64
|
+
resolvers[typeName][fieldName] = () => new Array(fieldConfig.length).fill({});
|
|
65
|
+
}
|
|
66
|
+
else if ('updateStore' in fieldConfig) {
|
|
67
|
+
const getFromStoreKeyFactory = getInterpolatedStringFactory(fieldConfig.store.key);
|
|
68
|
+
const updateStoreFactories = fieldConfig.updateStore.map(updateStoreConfig => ({
|
|
69
|
+
updateStoreConfig,
|
|
70
|
+
keyFactory: getInterpolatedStringFactory(updateStoreConfig.key),
|
|
71
|
+
valueFactory: getInterpolatedStringFactory(updateStoreConfig.value),
|
|
72
|
+
}));
|
|
73
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
74
|
+
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
75
|
+
const resolverData = { root, args, context, info, random: Date.now().toString(), env: process.env };
|
|
76
|
+
updateStoreFactories.forEach(({ updateStoreConfig, keyFactory, valueFactory }) => {
|
|
77
|
+
const key = keyFactory(resolverData);
|
|
78
|
+
const value = valueFactory(resolverData);
|
|
79
|
+
store.set(updateStoreConfig.type, key, updateStoreConfig.fieldName, value);
|
|
80
|
+
});
|
|
81
|
+
const key = getFromStoreKeyFactory(resolverData);
|
|
82
|
+
return store.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
else if ('store' in fieldConfig) {
|
|
86
|
+
const keyFactory = getInterpolatedStringFactory(fieldConfig.store.key);
|
|
87
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
88
|
+
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
89
|
+
const key = keyFactory({ root, args, context, info, env: process.env });
|
|
90
|
+
return store.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
if (fieldConfig.faker) {
|
|
96
|
+
let fakerFn;
|
|
97
|
+
const [service, method] = fieldConfig.faker.split('.');
|
|
98
|
+
if (service in faker) {
|
|
99
|
+
fakerFn = () => faker[service][method]();
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
fakerFn = () => faker.fake(fieldConfig.faker || '');
|
|
103
|
+
}
|
|
104
|
+
mocks$1[typeName] = fakerFn;
|
|
105
|
+
}
|
|
106
|
+
else if (fieldConfig.custom) {
|
|
107
|
+
const exportedVal$ = loadFromModuleExportExpression(fieldConfig.custom, {
|
|
108
|
+
cwd: config.baseDir,
|
|
109
|
+
defaultExportName: 'default',
|
|
110
|
+
importFn: config.importFn,
|
|
111
|
+
});
|
|
112
|
+
mocks$1[typeName] = () => {
|
|
113
|
+
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(store) : exportedVal);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const store = createMockStore({ schema, mocks: mocks$1 });
|
|
121
|
+
if (config === null || config === void 0 ? void 0 : config.initializeStore) {
|
|
122
|
+
const initializeStoreFn$ = loadFromModuleExportExpression(config.initializeStore, {
|
|
123
|
+
cwd: config.baseDir,
|
|
124
|
+
defaultExportName: 'default',
|
|
125
|
+
importFn: config.importFn,
|
|
126
|
+
});
|
|
127
|
+
// eslint-disable-next-line no-void
|
|
128
|
+
void initializeStoreFn$.then(fn => fn(store));
|
|
129
|
+
}
|
|
130
|
+
const mockedSchema = addMocksToSchema({
|
|
131
|
+
schema,
|
|
132
|
+
store,
|
|
133
|
+
mocks: mocks$1,
|
|
134
|
+
resolvers,
|
|
135
|
+
preserveResolvers: config === null || config === void 0 ? void 0 : config.preserveResolvers,
|
|
136
|
+
});
|
|
137
|
+
mockedSchemas.add(mockedSchema);
|
|
138
|
+
replaceSchema(mockedSchema);
|
|
139
|
+
},
|
|
140
|
+
onExecute({ setExecuteFn }) {
|
|
141
|
+
setExecuteFn(execute);
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export default useMock;
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-mesh/plugin-mock",
|
|
3
|
+
"version": "0.0.1-alpha-20220810133859-6f028675c",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"graphql": "*"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@envelop/core": "^2.3.2",
|
|
10
|
+
"@graphql-mesh/cross-helpers": "0.2.1-alpha-20220810133859-6f028675c",
|
|
11
|
+
"@graphql-mesh/string-interpolation": "0.3.0",
|
|
12
|
+
"@graphql-mesh/types": "0.78.7-alpha-20220810133859-6f028675c",
|
|
13
|
+
"@graphql-mesh/utils": "0.37.8-alpha-20220810133859-6f028675c",
|
|
14
|
+
"@graphql-tools/mock": "8.7.2",
|
|
15
|
+
"@graphql-tools/schema": "9.0.0",
|
|
16
|
+
"@graphql-tools/utils": "8.9.1",
|
|
17
|
+
"faker": "5.5.3",
|
|
18
|
+
"graphql-scalars": "1.17.0",
|
|
19
|
+
"tslib": "^2.4.0"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "Urigo/graphql-mesh",
|
|
24
|
+
"directory": "packages/plugins/mock"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"main": "index.js",
|
|
28
|
+
"module": "index.mjs",
|
|
29
|
+
"typings": "index.d.ts",
|
|
30
|
+
"typescript": {
|
|
31
|
+
"definition": "index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"require": "./index.js",
|
|
36
|
+
"import": "./index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"./*": {
|
|
39
|
+
"require": "./*.js",
|
|
40
|
+
"import": "./*.mjs"
|
|
41
|
+
},
|
|
42
|
+
"./package.json": "./package.json"
|
|
43
|
+
}
|
|
44
|
+
}
|