@graphql-mesh/plugin-mock 0.105.14 → 0.105.15
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/cjs/index.js +23 -10
- package/esm/index.js +23 -10
- package/package.json +3 -3
package/cjs/index.js
CHANGED
|
@@ -13,7 +13,13 @@ function useMock(config) {
|
|
|
13
13
|
// eslint-disable-next-line no-new-func
|
|
14
14
|
const configIf = config != null && 'if' in config ? new Function(`return ${config.if}`)() : true;
|
|
15
15
|
if (configIf) {
|
|
16
|
+
let mockStore;
|
|
16
17
|
return {
|
|
18
|
+
onContextBuilding({ extendContext }) {
|
|
19
|
+
extendContext({
|
|
20
|
+
mockStore,
|
|
21
|
+
});
|
|
22
|
+
},
|
|
17
23
|
onSchemaChange({ schema, replaceSchema }) {
|
|
18
24
|
if (mockedSchemas.has(schema)) {
|
|
19
25
|
return;
|
|
@@ -48,7 +54,8 @@ function useMock(config) {
|
|
|
48
54
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
49
55
|
resolvers[typeName][fieldName] = fakerFn;
|
|
50
56
|
}
|
|
51
|
-
else if (fieldConfig.custom
|
|
57
|
+
else if (typeof fieldConfig.custom === 'string' &&
|
|
58
|
+
fieldConfig.custom.includes('#')) {
|
|
52
59
|
const exportedVal$ = (0, utils_1.loadFromModuleExportExpression)(fieldConfig.custom, {
|
|
53
60
|
cwd: config.baseDir,
|
|
54
61
|
defaultExportName: 'default',
|
|
@@ -56,13 +63,19 @@ function useMock(config) {
|
|
|
56
63
|
});
|
|
57
64
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
58
65
|
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
59
|
-
context = context || {};
|
|
60
|
-
context.mockStore = store;
|
|
61
66
|
return exportedVal$.then(exportedVal => typeof exportedVal === 'function'
|
|
62
67
|
? exportedVal(root, args, context, info)
|
|
63
68
|
: exportedVal);
|
|
64
69
|
};
|
|
65
70
|
}
|
|
71
|
+
else if (typeof fieldConfig.custom === 'function') {
|
|
72
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
73
|
+
resolvers[typeName][fieldName] = fieldConfig.custom;
|
|
74
|
+
}
|
|
75
|
+
else if (fieldConfig.custom != null) {
|
|
76
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
77
|
+
resolvers[typeName][fieldName] = () => fieldConfig.custom;
|
|
78
|
+
}
|
|
66
79
|
else if ('length' in fieldConfig) {
|
|
67
80
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
68
81
|
resolvers[typeName][fieldName] = () => new Array(fieldConfig.length).fill({});
|
|
@@ -87,10 +100,10 @@ function useMock(config) {
|
|
|
87
100
|
updateStoreFactories.forEach(({ updateStoreConfig, keyFactory, valueFactory }) => {
|
|
88
101
|
const key = keyFactory(resolverData);
|
|
89
102
|
const value = valueFactory(resolverData);
|
|
90
|
-
|
|
103
|
+
mockStore.set(updateStoreConfig.type, key, updateStoreConfig.fieldName, value);
|
|
91
104
|
});
|
|
92
105
|
const key = getFromStoreKeyFactory(resolverData);
|
|
93
|
-
return
|
|
106
|
+
return mockStore.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
94
107
|
};
|
|
95
108
|
}
|
|
96
109
|
else if ('store' in fieldConfig) {
|
|
@@ -98,7 +111,7 @@ function useMock(config) {
|
|
|
98
111
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
99
112
|
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
100
113
|
const key = keyFactory({ root, args, context, info, env: process.env });
|
|
101
|
-
return
|
|
114
|
+
return mockStore.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
102
115
|
};
|
|
103
116
|
}
|
|
104
117
|
}
|
|
@@ -121,14 +134,14 @@ function useMock(config) {
|
|
|
121
134
|
importFn: config.importFn,
|
|
122
135
|
});
|
|
123
136
|
mocks[typeName] = () => {
|
|
124
|
-
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(
|
|
137
|
+
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(mockStore) : exportedVal);
|
|
125
138
|
};
|
|
126
139
|
}
|
|
127
140
|
}
|
|
128
141
|
}
|
|
129
142
|
}
|
|
130
143
|
}
|
|
131
|
-
|
|
144
|
+
mockStore = (0, mock_1.createMockStore)({ schema, mocks });
|
|
132
145
|
if (config?.initializeStore) {
|
|
133
146
|
const initializeStoreFn$ = (0, utils_1.loadFromModuleExportExpression)(config.initializeStore, {
|
|
134
147
|
cwd: config.baseDir,
|
|
@@ -136,11 +149,11 @@ function useMock(config) {
|
|
|
136
149
|
importFn: config.importFn,
|
|
137
150
|
});
|
|
138
151
|
// eslint-disable-next-line no-void
|
|
139
|
-
void initializeStoreFn$.then(fn => fn(
|
|
152
|
+
void initializeStoreFn$.then(fn => fn(mockStore));
|
|
140
153
|
}
|
|
141
154
|
const mockedSchema = (0, mock_1.addMocksToSchema)({
|
|
142
155
|
schema,
|
|
143
|
-
store,
|
|
156
|
+
store: mockStore,
|
|
144
157
|
mocks,
|
|
145
158
|
resolvers,
|
|
146
159
|
preserveResolvers: config?.preserveResolvers,
|
package/esm/index.js
CHANGED
|
@@ -9,7 +9,13 @@ export default function useMock(config) {
|
|
|
9
9
|
// eslint-disable-next-line no-new-func
|
|
10
10
|
const configIf = config != null && 'if' in config ? new Function(`return ${config.if}`)() : true;
|
|
11
11
|
if (configIf) {
|
|
12
|
+
let mockStore;
|
|
12
13
|
return {
|
|
14
|
+
onContextBuilding({ extendContext }) {
|
|
15
|
+
extendContext({
|
|
16
|
+
mockStore,
|
|
17
|
+
});
|
|
18
|
+
},
|
|
13
19
|
onSchemaChange({ schema, replaceSchema }) {
|
|
14
20
|
if (mockedSchemas.has(schema)) {
|
|
15
21
|
return;
|
|
@@ -44,7 +50,8 @@ export default function useMock(config) {
|
|
|
44
50
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
45
51
|
resolvers[typeName][fieldName] = fakerFn;
|
|
46
52
|
}
|
|
47
|
-
else if (fieldConfig.custom
|
|
53
|
+
else if (typeof fieldConfig.custom === 'string' &&
|
|
54
|
+
fieldConfig.custom.includes('#')) {
|
|
48
55
|
const exportedVal$ = loadFromModuleExportExpression(fieldConfig.custom, {
|
|
49
56
|
cwd: config.baseDir,
|
|
50
57
|
defaultExportName: 'default',
|
|
@@ -52,13 +59,19 @@ export default function useMock(config) {
|
|
|
52
59
|
});
|
|
53
60
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
54
61
|
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
55
|
-
context = context || {};
|
|
56
|
-
context.mockStore = store;
|
|
57
62
|
return exportedVal$.then(exportedVal => typeof exportedVal === 'function'
|
|
58
63
|
? exportedVal(root, args, context, info)
|
|
59
64
|
: exportedVal);
|
|
60
65
|
};
|
|
61
66
|
}
|
|
67
|
+
else if (typeof fieldConfig.custom === 'function') {
|
|
68
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
69
|
+
resolvers[typeName][fieldName] = fieldConfig.custom;
|
|
70
|
+
}
|
|
71
|
+
else if (fieldConfig.custom != null) {
|
|
72
|
+
resolvers[typeName] = resolvers[typeName] || {};
|
|
73
|
+
resolvers[typeName][fieldName] = () => fieldConfig.custom;
|
|
74
|
+
}
|
|
62
75
|
else if ('length' in fieldConfig) {
|
|
63
76
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
64
77
|
resolvers[typeName][fieldName] = () => new Array(fieldConfig.length).fill({});
|
|
@@ -83,10 +96,10 @@ export default function useMock(config) {
|
|
|
83
96
|
updateStoreFactories.forEach(({ updateStoreConfig, keyFactory, valueFactory }) => {
|
|
84
97
|
const key = keyFactory(resolverData);
|
|
85
98
|
const value = valueFactory(resolverData);
|
|
86
|
-
|
|
99
|
+
mockStore.set(updateStoreConfig.type, key, updateStoreConfig.fieldName, value);
|
|
87
100
|
});
|
|
88
101
|
const key = getFromStoreKeyFactory(resolverData);
|
|
89
|
-
return
|
|
102
|
+
return mockStore.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
90
103
|
};
|
|
91
104
|
}
|
|
92
105
|
else if ('store' in fieldConfig) {
|
|
@@ -94,7 +107,7 @@ export default function useMock(config) {
|
|
|
94
107
|
resolvers[typeName] = resolvers[typeName] || {};
|
|
95
108
|
resolvers[typeName][fieldName] = (root, args, context, info) => {
|
|
96
109
|
const key = keyFactory({ root, args, context, info, env: process.env });
|
|
97
|
-
return
|
|
110
|
+
return mockStore.get(fieldConfig.store.type, key, fieldConfig.store.fieldName);
|
|
98
111
|
};
|
|
99
112
|
}
|
|
100
113
|
}
|
|
@@ -117,14 +130,14 @@ export default function useMock(config) {
|
|
|
117
130
|
importFn: config.importFn,
|
|
118
131
|
});
|
|
119
132
|
mocks[typeName] = () => {
|
|
120
|
-
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(
|
|
133
|
+
return exportedVal$.then(exportedVal => typeof exportedVal === 'function' ? exportedVal(mockStore) : exportedVal);
|
|
121
134
|
};
|
|
122
135
|
}
|
|
123
136
|
}
|
|
124
137
|
}
|
|
125
138
|
}
|
|
126
139
|
}
|
|
127
|
-
|
|
140
|
+
mockStore = createMockStore({ schema, mocks });
|
|
128
141
|
if (config?.initializeStore) {
|
|
129
142
|
const initializeStoreFn$ = loadFromModuleExportExpression(config.initializeStore, {
|
|
130
143
|
cwd: config.baseDir,
|
|
@@ -132,11 +145,11 @@ export default function useMock(config) {
|
|
|
132
145
|
importFn: config.importFn,
|
|
133
146
|
});
|
|
134
147
|
// eslint-disable-next-line no-void
|
|
135
|
-
void initializeStoreFn$.then(fn => fn(
|
|
148
|
+
void initializeStoreFn$.then(fn => fn(mockStore));
|
|
136
149
|
}
|
|
137
150
|
const mockedSchema = addMocksToSchema({
|
|
138
151
|
schema,
|
|
139
|
-
store,
|
|
152
|
+
store: mockStore,
|
|
140
153
|
mocks,
|
|
141
154
|
resolvers,
|
|
142
155
|
preserveResolvers: config?.preserveResolvers,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/plugin-mock",
|
|
3
|
-
"version": "0.105.
|
|
3
|
+
"version": "0.105.15",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "*"
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@graphql-mesh/cross-helpers": "^0.4.10",
|
|
10
10
|
"@graphql-mesh/string-interpolation": "^0.5.9",
|
|
11
|
-
"@graphql-mesh/types": "^0.104.
|
|
12
|
-
"@graphql-mesh/utils": "^0.104.
|
|
11
|
+
"@graphql-mesh/types": "^0.104.14",
|
|
12
|
+
"@graphql-mesh/utils": "^0.104.14",
|
|
13
13
|
"@graphql-tools/mock": "^9.0.3",
|
|
14
14
|
"@graphql-tools/schema": "^10.0.5",
|
|
15
15
|
"@graphql-tools/utils": "^10.8.0",
|