Skip to content

Commit ffd2942

Browse files
authored
Add maybeValueDefault configuration option (#67)
* Add maybeValueDefault * Default inputMaybeValueDefault to maybeValueDefault * Update documentation
1 parent bb6a863 commit ffd2942

File tree

5 files changed

+149
-11
lines changed

5 files changed

+149
-11
lines changed

changelog.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## Unreleased
44

5-
- Add inputMaybeValueDefault option to allow default input values other than null
5+
- Add `maybeValueDefault` option to customize the nullable fields' default value
6+
- Add `inputMaybeValueDefault` option to customize the nullable inputs' fields' default value
67

78
## 1.0.0 - 2022-05-07
89

docs/docs/configuration/schema.mdx

+61-4
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,69 @@ export function createUserMock(
317317

318318
</details>
319319

320+
## `config.maybeValueDefault`
321+
322+
By default, nullable fields are initialized as "null".
323+
324+
You can customize this default value through `maybeValueDefault`:
325+
326+
```yml
327+
overwrite: true
328+
schema: ./schema.graphql
329+
generates:
330+
./types.ts:
331+
plugins:
332+
- typescript
333+
- graphql-codegen-factories/schema
334+
config:
335+
# highlight-start
336+
maybeValueDefault: undefined
337+
# highlight-end
338+
```
339+
340+
<details>
341+
<summary>Example</summary>
342+
343+
<div className="codeBlocks">
344+
345+
```graphql title="schema.graphql"
346+
type Post {
347+
title: String
348+
}
349+
350+
input PostInput {
351+
title: String
352+
}
353+
```
354+
355+
```typescript title="types.ts"
356+
export function createPost(props: Partial<Post> = {}): Post {
357+
return {
358+
// highlight-start
359+
title: undefined,
360+
// highlight-end
361+
...props,
362+
};
363+
}
364+
365+
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
366+
return {
367+
// highlight-start
368+
title: undefined,
369+
// highlight-end
370+
...props,
371+
};
372+
}
373+
```
374+
375+
</div>
376+
</details>
377+
320378
## `config.inputMaybeValueDefault`
321379

322-
By default, this plugin will always generate a null value for input types that are nullable.
323-
This can be problematic when you customized the inputMaybeValue.
324-
You can customize the default value by configuring `inputMaybeValueDefault`:
380+
By default, inputs' nullable fields are initialized as `config.maybeValueDefault` ("null" by default).
381+
382+
You can customize this default value through `inputMaybeValueDefault`:
325383

326384
```yml
327385
overwrite: true
@@ -332,7 +390,6 @@ generates:
332390
- typescript
333391
- graphql-codegen-factories/schema
334392
config:
335-
inputMaybeValue: T | undefined
336393
# highlight-start
337394
inputMaybeValueDefault: undefined
338395
# highlight-end

packages/graphql-codegen-factories/src/schema/FactoriesSchemaVisitor.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface FactoriesSchemaVisitorRawConfig
5151
// but it is injected automatically while this one is provided by the user
5252
importTypesNamespace?: string;
5353

54+
maybeValueDefault?: string;
5455
inputMaybeValueDefault?: string;
5556
}
5657

@@ -61,7 +62,8 @@ export interface FactoriesSchemaVisitorParsedConfig
6162
namespacedImportName: string | null;
6263
typesPath?: string;
6364
importTypesNamespace?: string;
64-
inputMaybeValueDefault: string | "null";
65+
maybeValueDefault: string;
66+
inputMaybeValueDefault: string;
6567
}
6668

6769
interface VisitedTypeNode {
@@ -108,6 +110,11 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
108110
>;
109111

110112
constructor(schema: GraphQLSchema, config: FactoriesSchemaVisitorRawConfig) {
113+
const maybeValueDefault = getConfigValue(config.maybeValueDefault, "null");
114+
const inputMaybeValueDefault = getConfigValue(
115+
config.inputMaybeValueDefault,
116+
maybeValueDefault
117+
);
111118
const parsedConfig = {
112119
enumsAsTypes: getConfigValue(config.enumsAsTypes, false),
113120
scalarDefaults: getConfigValue(config.scalarDefaults, {}),
@@ -120,10 +127,8 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
120127
config.importTypesNamespace,
121128
undefined
122129
),
123-
inputMaybeValueDefault: getConfigValue(
124-
config.inputMaybeValueDefault,
125-
"null"
126-
),
130+
maybeValueDefault,
131+
inputMaybeValueDefault,
127132
} as FactoriesSchemaVisitorParsedConfig;
128133

129134
if (parsedConfig.typesPath && parsedConfig.namespacedImportName == null) {
@@ -295,7 +300,7 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
295300
}
296301

297302
FieldDefinition(node: UnvisitedFieldDefinitionNode): string {
298-
return this.convertField(node, "null");
303+
return this.convertField(node, this.config.maybeValueDefault);
299304
}
300305

301306
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string {

packages/graphql-codegen-factories/src/schema/__tests__/__snapshots__/plugin.ts.snap

+42
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,48 @@ Object {
149149
}
150150
`;
151151
152+
exports[`plugin should customize the maybe value default 1`] = `
153+
Object {
154+
"content": "export function createPostMock(props: Partial<Post> = {}): Post {
155+
return {
156+
__typename: \\"Post\\",
157+
title: undefined,
158+
...props,
159+
};
160+
}
161+
162+
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
163+
return {
164+
title: undefined,
165+
...props,
166+
};
167+
}
168+
",
169+
"prepend": Array [],
170+
}
171+
`;
172+
173+
exports[`plugin should customize the maybe value default and input maybe value default independently 1`] = `
174+
Object {
175+
"content": "export function createPostMock(props: Partial<Post> = {}): Post {
176+
return {
177+
__typename: \\"Post\\",
178+
title: undefined,
179+
...props,
180+
};
181+
}
182+
183+
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
184+
return {
185+
title: null,
186+
...props,
187+
};
188+
}
189+
",
190+
"prepend": Array [],
191+
}
192+
`;
193+
152194
exports[`plugin should import types from other file 1`] = `
153195
Object {
154196
"content": "export function createUserMock(props: Partial<SharedTypes.User> = {}): SharedTypes.User {

packages/graphql-codegen-factories/src/schema/__tests__/plugin.ts

+33
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,39 @@ describe("plugin", () => {
117117
expect(output).toMatchSnapshot();
118118
});
119119

120+
it("should customize the maybe value default", async () => {
121+
const schema = buildSchema(`
122+
type Post {
123+
title: String
124+
}
125+
input PostInput {
126+
title: String
127+
}
128+
`);
129+
130+
const output = await plugin(schema, [], {
131+
maybeValueDefault: "undefined",
132+
});
133+
expect(output).toMatchSnapshot();
134+
});
135+
136+
it("should customize the maybe value default and input maybe value default independently", async () => {
137+
const schema = buildSchema(`
138+
type Post {
139+
title: String
140+
}
141+
input PostInput {
142+
title: String
143+
}
144+
`);
145+
146+
const output = await plugin(schema, [], {
147+
maybeValueDefault: "undefined",
148+
inputMaybeValueDefault: "null",
149+
});
150+
expect(output).toMatchSnapshot();
151+
});
152+
120153
it("should customize the input maybe value default", async () => {
121154
const schema = buildSchema(`
122155
input PostInput {

0 commit comments

Comments
 (0)