Skip to content

Commit 8805c1a

Browse files
emabardeois
andauthored
Add per field generation (#106)
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]>
1 parent 7be3994 commit 8805c1a

File tree

8 files changed

+1774
-107
lines changed

8 files changed

+1774
-107
lines changed

README.md

Lines changed: 154 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,49 +48,43 @@ keep all GraphQL names as-is. Available case functions in `change-case-all` are
4848
`localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst`
4949
[See more](https://github.com/btxtiger/change-case-all)
5050

51-
### scalars (`{ [Scalar: string]: ScalarDefinition }`, defaultValue: `undefined`)
51+
### scalars (`{ [Scalar: string]: GeneratorOptions }`, defaultValue: `undefined`)
5252

5353
Allows you to define mappings for your custom scalars. Allows you to map any GraphQL Scalar to a
5454
[casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or
5555
function key) with optional arguments, or a or [faker](https://fakerjs.dev/api/) generator with optional arguments
5656

57-
Examples using **casual**
57+
For detailed configuration options, see [GeneratorOptions](#generatoroptions-type) documentation.
5858

59-
**With arguments**
59+
Examples using **casual**
6060

6161
```yaml
6262
plugins:
6363
- typescript-mock-data:
6464
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()
6866
```
6967
70-
**With multiple arguments**
68+
**With arguments**
7169
7270
```yaml
7371
plugins:
7472
- typescript-mock-data:
7573
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'
8177
```
8278
83-
**Shorthand if you don't have arguments**
79+
Examples using **faker**
8480
8581
```yaml
8682
plugins:
8783
- typescript-mock-data:
8884
scalars:
89-
Date: date # gets translated to casual.date()
85+
Date: date.past # gets translated to faker.date.past()
9086
```
9187
92-
Examples using **faker**
93-
9488
**With arguments**
9589
9690
```yaml
@@ -102,28 +96,6 @@ plugins:
10296
arguments: 10
10397
```
10498
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-
12799
**Custom value generator**
128100
129101
```yaml
@@ -181,11 +153,155 @@ When disabled, underscores will be retained for type names when the case is chan
181153
182154
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).
183155
156+
### fieldGeneration (`{ [typeName: string]: { [fieldName: string]: GeneratorOptions } }`, defaultValue: `undefined`)
157+
158+
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`:
173+
174+
```yaml
175+
plugins:
176+
- typescript-mock-data:
177+
scalars:
178+
Date: date.future
179+
fieldGeneration:
180+
_all:
181+
email: internet.email
182+
AdminUser:
183+
184+
```
185+
186+
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.
189+
184190
### generateLibrary (`'casual' | 'faker'`, defaultValue: `'casual'`)
185191

186192
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).
187193
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.
188194

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)
292+
generator: date
293+
extra:
294+
function: toLocaleDateString
295+
arguments: 'en_GB'
296+
```
297+
298+
**Custom value generator**
299+
300+
```yaml
301+
# gets translated as is
302+
fieldName: arrayBufferGenerator()
303+
```
304+
189305
## Examples of usage
190306

191307
**codegen.yml**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"sideEffects": false,
6363
"scripts": {
6464
"build": "tsc -m esnext --outDir dist/esnext && tsc -m commonjs --outDir dist/commonjs",
65-
"test": "TZ=UTC jest",
65+
"test": "TZ=UTC LANG=en_US.UTF8 jest",
6666
"lint": "eslint 'src/**/*.{js,ts,tsx}' --quiet --fix && tsc --noEmit",
6767
"prettify": "prettier --config ./.prettierrc.js --write",
6868
"auto:version": "yarn version --`auto version` --message 'Bump version to: %s [skip ci]'",

0 commit comments

Comments
 (0)