Skip to content

Commit

Permalink
Merge pull request #35 from mizdra/fix-object-type-field
Browse files Browse the repository at this point in the history
Generate optional type definitions
  • Loading branch information
mizdra authored Sep 24, 2023
2 parents a4f4253 + b8e40f9 commit c7749b1
Show file tree
Hide file tree
Showing 19 changed files with 1,016 additions and 1,370 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
},
},
],
'@typescript-eslint/naming-convention': 'off',
},
},
],
Expand Down
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "e2e",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "e2e"]
}
]
}
53 changes: 40 additions & 13 deletions e2e/index.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
defineUnionTest_Member1Factory,
defineEnumTest_TypeFactory,
defineCustomScalarTest_TypeFactory,
defineNullableListTest_TypeFactory,
OptionalAuthor,
defineNamingConventionTest_RenamedTypeFactory,
defineNullableTest_TypeFactory,
} from './__generated__/fabbrica.js';
import { Author } from './__generated__/types.js';
import { oneOf } from './test/util.js';

describe('integration test', () => {
Expand Down Expand Up @@ -92,17 +93,28 @@ describe('integration test', () => {
});

describe('GraphQL features test', () => {
it('nullable list', async () => {
const TypeFactory = defineNullableListTest_TypeFactory({
it('nullable', async () => {
const TypeFactory = defineNullableTest_TypeFactory({
defaultFields: {
list: ['item1', 'item2', null],
field1: null,
field2: ['item', null],
field3: { field: 'field' },
field4: [{ field: 'field' }, null],
},
});
const type = await TypeFactory.build();
expect(type).toStrictEqual({
list: ['item1', 'item2', null],
});
expectTypeOf(type).toEqualTypeOf<{ list: (string | null)[] }>();
field1: null,
field2: ['item', null],
field3: { field: 'field' },
field4: [{ field: 'field' }, null],
});
expectTypeOf(type).toEqualTypeOf<{
field1: null;
field2: (string | null)[];
field3: { field: string };
field4: ({ field: string } | null)[];
}>();
});
it('interface', async () => {
const TypeWithInterfaceField = defineInterfaceTest_TypeWithInterfaceFieldFactory({
Expand Down Expand Up @@ -178,8 +190,7 @@ describe('GraphQL features test', () => {
defineCustomScalarTest_TypeFactory({
defaultFields: {
scalar1: new Date('2021-01-01T00:00:00.000Z'),
// FIXME: The type of `scalar2` should be `{ field: string } | undefined`.
// But the type is `{ field: string | undefined } | undefined`.
// @ts-expect-error -- not optional
scalar2: { field: undefined },
},
});
Expand All @@ -197,7 +208,23 @@ describe('GraphQL features test', () => {
// });
// expectTypeOf(foo).toEqualTypeOf<{ field: string }>();
});
it.todo('naming convention');
});

describe('GraphQL Code Generator features test', () => {
it('namingConvention', async () => {
const RenamedTypeFactory = defineNamingConventionTest_RenamedTypeFactory({
defaultFields: {
field: 'field',
},
});
const type = await RenamedTypeFactory.build();
expect(type).toStrictEqual({
field: 'field',
});
expectTypeOf(type).toEqualTypeOf<{ field: string }>();
});
it.todo('typesPrefix');
it.todo('typesSuffix');
});

describe('defineTypeFactory', () => {
Expand Down Expand Up @@ -439,8 +466,8 @@ describe('defineTypeFactory', () => {
bookCount: number;
};
function defineAuthorFactoryWithTransientFields<
_DefaultFieldsResolver extends DefaultFieldsResolver<Author & AuthorTransientFields>,
_Traits extends Traits<Author, AuthorTransientFields>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalAuthor & AuthorTransientFields>,
_Traits extends Traits<OptionalAuthor, AuthorTransientFields>,
>(
options: AuthorFactoryDefineOptions<AuthorTransientFields, _DefaultFieldsResolver, _Traits>,
): AuthorFactoryInterface<AuthorTransientFields, _DefaultFieldsResolver, _Traits> {
Expand Down
27 changes: 22 additions & 5 deletions e2e/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ type Image {
height: Int
}

# NullableListTest
type NullableListTest_Type {
list: [String]!
# NullableTest
type NullableTest_Type {
field1: String
field2: [String]
field3: NullableTest_SubType
field4: [NullableTest_SubType]
}
type NullableTest_SubType {
field: String!
}

# InterfaceTest
Expand Down Expand Up @@ -73,7 +79,18 @@ input InputTest_Foo {
}

# NamingConventionTest
# TODO: Support naming convention
# type NamingConventionTest_Type {
type NamingConventionTest_Type {
field: String!
}

# TypesPrefixTest
# TODO: Add test for typesPrefix
# type TypesPrefixTest_Type {
# field: String!
# }

# TypesSuffixTest
# TODO: Add test for typesSuffix
# type TypesSuffixTest_Type {
# field: String!
# }
Loading

0 comments on commit c7749b1

Please sign in to comment.