diff --git a/e2e/schema.graphql b/e2e/1-basic-schema.graphql similarity index 86% rename from e2e/schema.graphql rename to e2e/1-basic-schema.graphql index c17b9fb..c8a1a5d 100644 --- a/e2e/schema.graphql +++ b/e2e/1-basic-schema.graphql @@ -80,17 +80,9 @@ input InputTest_Foo { # NamingConventionTest type NamingConventionTest_Type { + field1: String! + field2: SubType! +} +type SubType { field: String! } - -# TypesPrefixTest -# TODO: Add test for typesPrefix -# type TypesPrefixTest_Type { -# field: String! -# } - -# TypesSuffixTest -# TODO: Add test for typesSuffix -# type TypesSuffixTest_Type { -# field: String! -# } diff --git a/e2e/2-typesPrefix-schema.graphql b/e2e/2-typesPrefix-schema.graphql new file mode 100644 index 0000000..61e23cb --- /dev/null +++ b/e2e/2-typesPrefix-schema.graphql @@ -0,0 +1,8 @@ +# TypesPrefixTest +type Type { + field1: String! + field2: SubType! +} +type SubType { + field: String! +} diff --git a/e2e/3-typesSuffix-schema.graphql b/e2e/3-typesSuffix-schema.graphql new file mode 100644 index 0000000..314b3f8 --- /dev/null +++ b/e2e/3-typesSuffix-schema.graphql @@ -0,0 +1,8 @@ +# TypesSuffixTest +type Type { + field1: String! + field2: SubType! +} +type SubType { + field: String! +} diff --git a/e2e/codegen.ts b/e2e/codegen.ts index ccd2262..1d5c8ef 100644 --- a/e2e/codegen.ts +++ b/e2e/codegen.ts @@ -1,31 +1,72 @@ import { CodegenConfig } from '@graphql-codegen/cli'; +const defaultTypeScriptPluginConfig = { + nonOptionalTypename: true, + enumsAsTypes: true, + avoidOptionals: true, + skipTypename: true, +}; +const defaultFabbricaPluginConfig = { + typesFile: './types', + skipTypename: true, +}; + const config: CodegenConfig = { - schema: './schema.graphql', - config: { - namingConvention: { - typeNames: './my-naming-fn.js', - }, - }, generates: { - '__generated__/types.ts': { + '__generated__/1-basic/types.ts': { + schema: './1-basic-schema.graphql', plugins: ['typescript'], config: { - nonOptionalTypename: true, - enumsAsTypes: true, - avoidOptionals: true, - skipTypename: true, + ...defaultTypeScriptPluginConfig, scalars: { CustomScalarTest_CustomScalar1: 'Date', CustomScalarTest_CustomScalar2: '{ field: string }', }, + namingConvention: { + typeNames: './my-naming-fn.js', + }, + }, + }, + './__generated__/1-basic/fabbrica.ts': { + schema: './1-basic-schema.graphql', + plugins: ['@mizdra/graphql-fabbrica'], + config: { + ...defaultFabbricaPluginConfig, + namingConvention: { + typeNames: './my-naming-fn.js', + }, + }, + }, + '__generated__/2-typesPrefix/types.ts': { + schema: './2-typesPrefix-schema.graphql', + plugins: ['typescript'], + config: { + ...defaultTypeScriptPluginConfig, + typesPrefix: 'Prefix', + }, + }, + './__generated__/2-typesPrefix/fabbrica.ts': { + schema: './2-typesPrefix-schema.graphql', + plugins: ['@mizdra/graphql-fabbrica'], + config: { + ...defaultFabbricaPluginConfig, + typesPrefix: 'Prefix', + }, + }, + '__generated__/3-typesSuffix/types.ts': { + schema: './3-typesSuffix-schema.graphql', + plugins: ['typescript'], + config: { + ...defaultTypeScriptPluginConfig, + typesSuffix: 'Suffix', }, }, - './__generated__/fabbrica.ts': { + './__generated__/3-typesSuffix/fabbrica.ts': { + schema: './3-typesSuffix-schema.graphql', plugins: ['@mizdra/graphql-fabbrica'], config: { - typesFile: './types', - skipTypename: true, + ...defaultFabbricaPluginConfig, + typesSuffix: 'Suffix', }, }, }, diff --git a/e2e/index.e2e.ts b/e2e/index.e2e.ts index 1a9342c..14389c2 100644 --- a/e2e/index.e2e.ts +++ b/e2e/index.e2e.ts @@ -20,8 +20,10 @@ import { OptionalAuthor, defineNamingConventionTest_RenamedTypeFactory, defineNullableTest_TypeFactory, -} from './__generated__/fabbrica.js'; +} from './__generated__/1-basic/fabbrica.js'; import { oneOf } from './test/util.js'; +import { definePrefixTypeFactory } from './__generated__/2-typesPrefix/fabbrica.js'; +import { defineTypeSuffixFactory } from './__generated__/3-typesSuffix/fabbrica.js'; describe('integration test', () => { it('circular dependent type', async () => { @@ -214,17 +216,45 @@ describe('GraphQL Code Generator features test', () => { it('namingConvention', async () => { const RenamedTypeFactory = defineNamingConventionTest_RenamedTypeFactory({ defaultFields: { - field: 'field', + field1: 'field1', + field2: { field: 'field' }, }, }); const type = await RenamedTypeFactory.build(); expect(type).toStrictEqual({ - field: 'field', + field1: 'field1', + field2: { field: 'field' }, + }); + expectTypeOf(type).toEqualTypeOf<{ field1: string; field2: { field: string } }>(); + }); + it('typesPrefix', async () => { + const TypePrefix = definePrefixTypeFactory({ + defaultFields: { + field1: 'field1', + field2: { field: 'field' }, + }, + }); + const type = await TypePrefix.build(); + expect(type).toStrictEqual({ + field1: 'field1', + field2: { field: 'field' }, + }); + expectTypeOf(type).toEqualTypeOf<{ field1: string; field2: { field: string } }>(); + }); + it('typesSuffix', async () => { + const TypeSuffix = defineTypeSuffixFactory({ + defaultFields: { + field1: 'field1', + field2: { field: 'field' }, + }, + }); + const type = await TypeSuffix.build(); + expect(type).toStrictEqual({ + field1: 'field1', + field2: { field: 'field' }, }); - expectTypeOf(type).toEqualTypeOf<{ field: string }>(); + expectTypeOf(type).toEqualTypeOf<{ field1: string; field2: { field: string } }>(); }); - it.todo('typesPrefix'); - it.todo('typesSuffix'); }); describe('defineTypeFactory', () => { diff --git a/src/visitor.ts b/src/visitor.ts index c54c517..23e782d 100644 --- a/src/visitor.ts +++ b/src/visitor.ts @@ -41,7 +41,7 @@ export const createTypeInfoVisitor = ( } else if (node.kind === Kind.LIST_TYPE) { return `Maybe<${parseTypeNode(node.type)}[]>`; } else { - return `Maybe`; + return `Maybe`; } }