Skip to content

Commit 169334f

Browse files
authored
Merge pull request #56 from mizdra/fix-optional-type
Allow missing fields of nested field
2 parents 3d51008 + 32d1065 commit 169334f

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"editor.defaultFormatter": "esbenp.prettier-vscode",
44
"editor.formatOnSave": true,
55
"editor.codeActionsOnSave": {
6-
"source.fixAll.eslint": true
6+
"source.fixAll.eslint": "explicit"
77
},
88
"typescript.preferences.importModuleSpecifier": "relative",
99
"javascript.preferences.importModuleSpecifier": "relative",

e2e/index.e2e.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,26 @@ describe('defineTypeFactory', () => {
359359
books: readonly [{ readonly id: 'Book-0'; readonly title: 'ゆゆ式'; readonly author: undefined }];
360360
}>();
361361
});
362+
it('allow missing fields of nested field', async () => {
363+
const BookFactory = defineBookFactory({
364+
defaultFields: {
365+
id: 'Book-0',
366+
title: 'ゆゆ式',
367+
author: {
368+
id: 'Author-0',
369+
// missing fields: __typename, name
370+
},
371+
},
372+
});
373+
const book = await BookFactory.build();
374+
expectTypeOf(book).toEqualTypeOf<{
375+
id: string;
376+
title: string;
377+
author: {
378+
id: string;
379+
};
380+
}>();
381+
});
362382
it('creates fields with sequential id', async () => {
363383
const BookFactory = defineBookFactory({
364384
defaultFields: {

src/__snapshots__/code-generator.test.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import type { Maybe, Book, Author } from './types';
1212
1313
export * from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
1414
export type OptionalBook = {
15-
id: string;
16-
title: string;
17-
author: OptionalAuthor;
15+
id?: string | undefined;
16+
title?: string | undefined;
17+
author?: OptionalAuthor | undefined;
1818
};
1919
2020
const BookFieldNames = ['id', 'title', 'author'] as const;
@@ -57,9 +57,9 @@ export function defineBookFactory<
5757
}
5858
5959
export type OptionalAuthor = {
60-
id: string;
61-
name: string;
62-
books: Book[];
60+
id?: string | undefined;
61+
name?: string | undefined;
62+
books?: Book[] | undefined;
6363
};
6464
6565
const AuthorFieldNames = ['id', 'name', 'books'] as const;

src/code-generator.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ describe('generateOptionalTypeDefinitionCode', () => {
1010
const typeInfo: TypeInfo = {
1111
name: 'Book',
1212
fields: [
13-
{ name: 'id', typeString: 'string' },
14-
{ name: 'title', typeString: 'string', comment: transformComment('The book title') },
13+
{ name: 'id', typeString: 'string | undefined' },
14+
{ name: 'title', typeString: 'string | undefined', comment: transformComment('The book title') },
1515
],
1616
comment: transformComment('The book'),
1717
};
1818
const actual = generateOptionalTypeDefinitionCode(typeInfo);
1919
expect(actual).toMatchInlineSnapshot(`
2020
"/** The book */
2121
export type OptionalBook = {
22-
id: string;
22+
id?: string | undefined;
2323
/** The book title */
24-
title: string;
24+
title?: string | undefined;
2525
};
2626
"
2727
`);
@@ -38,17 +38,17 @@ describe('generateCode', () => {
3838
{
3939
name: 'Book',
4040
fields: [
41-
{ name: 'id', typeString: 'string' },
42-
{ name: 'title', typeString: 'string' },
43-
{ name: 'author', typeString: 'OptionalAuthor' },
41+
{ name: 'id', typeString: 'string | undefined' },
42+
{ name: 'title', typeString: 'string | undefined' },
43+
{ name: 'author', typeString: 'OptionalAuthor | undefined' },
4444
],
4545
},
4646
{
4747
name: 'Author',
4848
fields: [
49-
{ name: 'id', typeString: 'string' },
50-
{ name: 'name', typeString: 'string' },
51-
{ name: 'books', typeString: 'Book[]' },
49+
{ name: 'id', typeString: 'string | undefined' },
50+
{ name: 'name', typeString: 'string | undefined' },
51+
{ name: 'books', typeString: 'Book[] | undefined' },
5252
],
5353
},
5454
];

src/code-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function generateOptionalTypeDefinitionCode(typeInfo: TypeInfo): string {
2424
const joinedPropDefinitions = fields
2525
.map((field) => {
2626
const comment = field.comment ? ` ${field.comment}` : '';
27-
return `${comment} ${field.name}: ${field.typeString};`;
27+
return `${comment} ${field.name}?: ${field.typeString};`;
2828
})
2929
.join('\n');
3030
return `

0 commit comments

Comments
 (0)