Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"typescript.preferences.importModuleSpecifier": "relative",
"javascript.preferences.importModuleSpecifier": "relative",
Expand Down
20 changes: 20 additions & 0 deletions e2e/index.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,26 @@ describe('defineTypeFactory', () => {
books: readonly [{ readonly id: 'Book-0'; readonly title: 'ゆゆ式'; readonly author: undefined }];
}>();
});
it('allow missing fields of nested field', async () => {
const BookFactory = defineBookFactory({
defaultFields: {
id: 'Book-0',
title: 'ゆゆ式',
author: {
id: 'Author-0',
// missing fields: __typename, name
},
},
});
const book = await BookFactory.build();
expectTypeOf(book).toEqualTypeOf<{
id: string;
title: string;
author: {
id: string;
};
}>();
});
it('creates fields with sequential id', async () => {
const BookFactory = defineBookFactory({
defaultFields: {
Expand Down
12 changes: 6 additions & 6 deletions src/__snapshots__/code-generator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import type { Maybe, Book, Author } from './types';

export * from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
export type OptionalBook = {
id: string;
title: string;
author: OptionalAuthor;
id?: string | undefined;
title?: string | undefined;
author?: OptionalAuthor | undefined;
};

const BookFieldNames = ['id', 'title', 'author'] as const;
Expand Down Expand Up @@ -57,9 +57,9 @@ export function defineBookFactory<
}

export type OptionalAuthor = {
id: string;
name: string;
books: Book[];
id?: string | undefined;
name?: string | undefined;
books?: Book[] | undefined;
};

const AuthorFieldNames = ['id', 'name', 'books'] as const;
Expand Down
20 changes: 10 additions & 10 deletions src/code-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ describe('generateOptionalTypeDefinitionCode', () => {
const typeInfo: TypeInfo = {
name: 'Book',
fields: [
{ name: 'id', typeString: 'string' },
{ name: 'title', typeString: 'string', comment: transformComment('The book title') },
{ name: 'id', typeString: 'string | undefined' },
{ name: 'title', typeString: 'string | undefined', comment: transformComment('The book title') },
],
comment: transformComment('The book'),
};
const actual = generateOptionalTypeDefinitionCode(typeInfo);
expect(actual).toMatchInlineSnapshot(`
"/** The book */
export type OptionalBook = {
id: string;
id?: string | undefined;
/** The book title */
title: string;
title?: string | undefined;
};
"
`);
Expand All @@ -38,17 +38,17 @@ describe('generateCode', () => {
{
name: 'Book',
fields: [
{ name: 'id', typeString: 'string' },
{ name: 'title', typeString: 'string' },
{ name: 'author', typeString: 'OptionalAuthor' },
{ name: 'id', typeString: 'string | undefined' },
{ name: 'title', typeString: 'string | undefined' },
{ name: 'author', typeString: 'OptionalAuthor | undefined' },
],
},
{
name: 'Author',
fields: [
{ name: 'id', typeString: 'string' },
{ name: 'name', typeString: 'string' },
{ name: 'books', typeString: 'Book[]' },
{ name: 'id', typeString: 'string | undefined' },
{ name: 'name', typeString: 'string | undefined' },
{ name: 'books', typeString: 'Book[] | undefined' },
],
},
];
Expand Down
2 changes: 1 addition & 1 deletion src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function generateOptionalTypeDefinitionCode(typeInfo: TypeInfo): string {
const joinedPropDefinitions = fields
.map((field) => {
const comment = field.comment ? ` ${field.comment}` : '';
return `${comment} ${field.name}: ${field.typeString};`;
return `${comment} ${field.name}?: ${field.typeString};`;
})
.join('\n');
return `
Expand Down