You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR adds something that would come in super handy for a project I'm working on - in summary:
- You can provide a generator for a field based on its name and its parent `Type`
- You can pass arguments to the generator
- You can call another function (with arguments) on the generator result
- You can provide a locale to the `faker` package
Co-authored-by: Corentin Ardeois <[email protected]>
Allows you to define mappings for your custom scalars. Allows you to map any GraphQL Scalar to a
54
54
[casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or
55
55
function key) with optional arguments, or a or [faker](https://fakerjs.dev/api/) generator with optional arguments
56
56
57
-
Examples using **casual**
57
+
For detailed configuration options, see [GeneratorOptions](#generatoroptions-type) documentation.
58
58
59
-
**With arguments**
59
+
Examples using **casual**
60
60
61
61
```yaml
62
62
plugins:
63
63
- typescript-mock-data:
64
64
scalars:
65
-
Date: # gets translated to casual.date('YYYY-MM-DD')
66
-
generator: date
67
-
arguments: 'YYYY-MM-DD'
65
+
Date: date # gets translated to casual.date()
68
66
```
69
67
70
-
**With multiple arguments**
68
+
**With arguments**
71
69
72
70
```yaml
73
71
plugins:
74
72
- typescript-mock-data:
75
73
scalars:
76
-
PaginatedAmount: # gets translated to casual.integer(-100, 100)
77
-
generator: integer
78
-
arguments:
79
-
- -100
80
-
- 100
74
+
Date: # gets translated to casual.date('YYYY-MM-DD')
75
+
generator: date
76
+
arguments: 'YYYY-MM-DD'
81
77
```
82
78
83
-
**Shorthand if you don't have arguments**
79
+
Examples using **faker**
84
80
85
81
```yaml
86
82
plugins:
87
83
- typescript-mock-data:
88
84
scalars:
89
-
Date: date # gets translated to casual.date()
85
+
Date: date.past# gets translated to faker.date.past()
90
86
```
91
87
92
-
Examples using **faker**
93
-
94
88
**With arguments**
95
89
96
90
```yaml
@@ -102,28 +96,6 @@ plugins:
102
96
arguments: 10
103
97
```
104
98
105
-
**With multiple arguments**
106
-
107
-
```yaml
108
-
plugins:
109
-
- typescript-mock-data:
110
-
scalars:
111
-
Description: # gets translated to faker.lorem.paragraphs(3, '\n')
112
-
generator: lorem.paragraphs
113
-
arguments:
114
-
- 3
115
-
- '\n'
116
-
```
117
-
118
-
**Shorthand if you don't have arguments**
119
-
120
-
```yaml
121
-
plugins:
122
-
- typescript-mock-data:
123
-
scalars:
124
-
Date: date.past # gets translated to faker.date.past()
125
-
```
126
-
127
99
**Custom value generator**
128
100
129
101
```yaml
@@ -181,11 +153,155 @@ When disabled, underscores will be retained for type names when the case is chan
181
153
182
154
When enabled, values will be generated dynamically when the mock function is called rather than statically when the mock function is generated. The values are generated consistently from a [casual seed](https://github.com/boo1ean/casual#seeding) that can be manually configured using the generated `seedMocks(seed: number)` function, as shown in [this test](https://github.com/JimmyPaolini/graphql-codegen-typescript-mock-data/blob/dynamic-mode/tests/dynamicValues/spec.ts#L13).
This setting allows you to add specific generation to a field for a given type. For example if you have a type called `User` and a field called `birthDate` you can override any generated value there as follows:
159
+
160
+
```yaml
161
+
plugins:
162
+
- typescript-mock-data:
163
+
scalars:
164
+
Date: date.future
165
+
fieldGeneration:
166
+
User:
167
+
birthDate: date.past
168
+
```
169
+
170
+
Note that even if `birthDate` is a scalar of `Date` type, its value will still be overridden.
171
+
172
+
If you want to use a specific generator for **all** fields of a given name, you can declare it under a property called `_all`:
In the above example all resolvers with the name `email` will use the `internet.email` generator. However since we specified a specific email for `AdminUser` that will take precedence over the `_all` generated value.
187
+
188
+
For detailed configuration options, see [GeneratorOptions](#generatoroptions-type) documentation.
Select a library to generate mock values. The default is [casual](https://github.com/boo1ean/casual), Other options include [faker](https://github.com/faker-js/faker).
187
193
casual dependents on Node API and cannot be executed in a browser. faker is useful when you want to use a mock function with the dynamicValues option enabled in the browser.
188
194
195
+
### `GeneratorOptions` type
196
+
197
+
This type is used in `scalars` and `fieldGeneration` options.
198
+
199
+
Examples using **casual**
200
+
201
+
**Shorthand if you don't have arguments**
202
+
203
+
```yaml
204
+
fieldName: date # gets translated to casual.date()
205
+
```
206
+
207
+
**With arguments**
208
+
209
+
```yaml
210
+
fieldName: # gets translated to casual.date('YYYY-MM-DD')
211
+
generator: date
212
+
arguments: 'YYYY-MM-DD'
213
+
```
214
+
215
+
**With multiple arguments**
216
+
217
+
```yaml
218
+
fieldName: # gets translated to casual.integer(-100, 100)
219
+
generator: integer
220
+
arguments:
221
+
- -100
222
+
- 100
223
+
```
224
+
225
+
**With extra function call**
226
+
227
+
```yaml
228
+
fieldName: # gets translated to casual.integer.toFixed()
229
+
generator: integer
230
+
extra:
231
+
function: toFixed
232
+
```
233
+
234
+
**With extra function call arguments**
235
+
236
+
```yaml
237
+
fieldName: # gets translated to casual.integer.toFixed(3)
238
+
generator: integer
239
+
extra:
240
+
function: toFixed
241
+
arguments: 3
242
+
```
243
+
244
+
Examples using **faker**
245
+
246
+
**With arguments**
247
+
248
+
```yaml
249
+
plugins:
250
+
- typescript-mock-data:
251
+
scalars:
252
+
Date: # gets translated to faker.date.past(10)
253
+
generator: date.past
254
+
arguments: 10
255
+
```
256
+
257
+
**With multiple arguments**
258
+
259
+
```yaml
260
+
plugins:
261
+
- typescript-mock-data:
262
+
scalars:
263
+
Description: # gets translated to faker.lorem.paragraphs(3, '\n')
264
+
generator: lorem.paragraphs
265
+
arguments:
266
+
- 3
267
+
- '\n'
268
+
```
269
+
270
+
**Shorthand if you don't have arguments**
271
+
272
+
```yaml
273
+
plugins:
274
+
- typescript-mock-data:
275
+
scalars:
276
+
Date: date.past # gets translated to faker.date.past()
277
+
```
278
+
279
+
**With extra function call**
280
+
281
+
```yaml
282
+
fieldName: # gets translated to casual.date().toLocaleDateString()
283
+
generator: date
284
+
extra:
285
+
function: toLocaleDateString
286
+
```
287
+
288
+
**With extra function call arguments**
289
+
290
+
```yaml
291
+
fieldName: # gets translated to casual.date().toLocaleDateString('en_GB)
0 commit comments