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
16 changes: 4 additions & 12 deletions e2e/schema.graphql → e2e/1-basic-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
# }
8 changes: 8 additions & 0 deletions e2e/2-typesPrefix-schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TypesPrefixTest
type Type {
field1: String!
field2: SubType!
}
type SubType {
field: String!
}
8 changes: 8 additions & 0 deletions e2e/3-typesSuffix-schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TypesSuffixTest
type Type {
field1: String!
field2: SubType!
}
type SubType {
field: String!
}
69 changes: 55 additions & 14 deletions e2e/codegen.ts
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
Expand Down
42 changes: 36 additions & 6 deletions e2e/index.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const createTypeInfoVisitor = (
} else if (node.kind === Kind.LIST_TYPE) {
return `Maybe<${parseTypeNode(node.type)}[]>`;
} else {
return `Maybe<Optional${node.name.value}>`;
return `Maybe<Optional${convertName(node.name.value, config)}>`;
}
}

Expand Down