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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,35 @@ const config: CodegenConfig = {
module.exports = config;
```

### `nonOptionalDefaultFields`

type: `boolean`, default: `false`

Make it mandatory to pass all fields to `defaultFields`. This is useful to force the `defaultFields` to be updated when new fields are added to the schema.

```ts
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './schema.graphql',
generates: {
'__generated__/types.ts': {
plugins: ['typescript'],
config: {
// ...
},
},
'./__generated__/fabbrica.ts': {
plugins: ['@mizdra/graphql-fabbrica'],
config: {
// ...
nonOptionalDefaultFields: true,
},
},
},
};
module.exports = config;
```

### `namingConvention`

type: `NamingConvention`, default: `change-case-all#pascalCase`
Expand Down
4 changes: 2 additions & 2 deletions e2e/1-basic-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type NamingConventionTest_SubType {
field: String!
}

# NonOptionalFields
type NonOptionalFields_OptionalFieldsType {
# NonOptionalDefaultFields
type NonOptionalDefaultFields_OptionalDefaultFieldsType {
field1: String!
field2: String!
}
2 changes: 1 addition & 1 deletion e2e/4-non-optional-fields-schema.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type NonOptionalFields_NonOptionalFieldsType {
type NonOptionalDefaultFields_NonOptionalDefaultFieldsType {
field1: String!
field2: String!
}
2 changes: 1 addition & 1 deletion e2e/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const config: CodegenConfig = {
plugins: ['@mizdra/graphql-fabbrica'],
config: {
...defaultFabbricaPluginConfig,
nonOptionalFields: true,
nonOptionalDefaultFields: true,
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions e2e/index.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import {
defineNamingConventionTest_RenamedTypeFactory,
defineNullableTest_TypeFactory,
defineInputTest_InputFactory,
defineNonOptionalFields_OptionalFieldsTypeFactory,
defineNonOptionalDefaultFields_OptionalDefaultFieldsTypeFactory,
} 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';
import { defineNonOptionalFields_NonOptionalFieldsTypeFactory } from './__generated__/4-non-optional-fields/fabbrica.js';
import { defineNonOptionalDefaultFields_NonOptionalDefaultFieldsTypeFactory } from './__generated__/4-non-optional-fields/fabbrica.js';

describe('integration test', () => {
it('circular dependent type', async () => {
Expand Down Expand Up @@ -410,18 +410,18 @@ describe('defineTypeFactory', () => {
expect(firstNameResolver).toHaveBeenCalledTimes(1);
expect(lastNameResolver).toHaveBeenCalledTimes(1);
});
describe('nonOptionalFields', () => {
it('requires to pass all fields if nonOptionalFields is false', async () => {
defineNonOptionalFields_NonOptionalFieldsTypeFactory({
describe('nonOptionalDefaultFields', () => {
it('requires to pass all fields if nonOptionalDefaultFields is false', async () => {
defineNonOptionalDefaultFields_NonOptionalDefaultFieldsTypeFactory({
// @ts-expect-error -- expects error
defaultFields: {
field1: 'field1',
// field2: 'field2',
},
});
});
it('requires to pass all fields if nonOptionalFields is true', async () => {
const TypeFactory = defineNonOptionalFields_OptionalFieldsTypeFactory({
it('requires to pass all fields if nonOptionalDefaultFields is true', async () => {
const TypeFactory = defineNonOptionalDefaultFields_OptionalDefaultFieldsTypeFactory({
defaultFields: {
field1: 'field1',
// field2: 'field2',
Expand Down
2 changes: 1 addition & 1 deletion src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function generateFieldNamesDefinitionCode(typeInfo: TypeInfo): string {
function generateTypeFactoryCode(config: Config, typeInfo: TypeInfo): string {
const { name } = typeInfo;
function wrapRequired(str: string) {
return config.nonOptionalFields ? `Required<${str}>` : str;
return config.nonOptionalDefaultFields ? `Required<${str}>` : str;
}
return `
export type ${name}FactoryDefineOptions<
Expand Down
10 changes: 6 additions & 4 deletions src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ describe('validateConfig', () => {
'`options.skipIsAbstractType` must be a boolean',
);
});
it('nonOptionalFields', () => {
expect(() => validateConfig({ typesFile: './types', nonOptionalFields: oneOf([true, false]) })).not.toThrow();
it('nonOptionalDefaultFields', () => {
expect(() =>
validateConfig({ typesFile: './types', nonOptionalDefaultFields: oneOf([true, false]) }),
).not.toThrow();
expect(() => validateConfig({ typesFile: './types' })).not.toThrow();
expect(() => validateConfig({ typesFile: './types', nonOptionalFields: 1 })).toThrow(
'`options.nonOptionalFields` must be a boolean',
expect(() => validateConfig({ typesFile: './types', nonOptionalDefaultFields: 1 })).toThrow(
'`options.nonOptionalDefaultFields` must be a boolean',
);
});
it('typesPrefix', () => {
Expand Down
10 changes: 5 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type RawConfig = {
typesFile: string;
skipTypename?: RawTypesConfig['skipTypename'];
skipIsAbstractType?: boolean | undefined;
nonOptionalFields?: boolean | undefined;
nonOptionalDefaultFields?: boolean | undefined;
namingConvention?: RawTypesConfig['namingConvention'];
typesPrefix?: RawTypesConfig['typesPrefix'];
typesSuffix?: RawTypesConfig['typesSuffix'];
Expand All @@ -15,7 +15,7 @@ export type Config = {
typesFile: string;
skipTypename: Exclude<RawTypesConfig['skipTypename'], undefined>;
skipIsAbstractType: boolean;
nonOptionalFields: boolean;
nonOptionalDefaultFields: boolean;
typesPrefix: Exclude<RawTypesConfig['typesPrefix'], undefined>;
typesSuffix: Exclude<RawTypesConfig['typesSuffix'], undefined>;
convert: ConvertFn;
Expand All @@ -38,8 +38,8 @@ export function validateConfig(rawConfig: unknown): asserts rawConfig is RawConf
if ('skipIsAbstractType' in rawConfig && typeof rawConfig['skipIsAbstractType'] !== 'boolean') {
throw new Error('`options.skipIsAbstractType` must be a boolean');
}
if ('nonOptionalFields' in rawConfig && typeof rawConfig['nonOptionalFields'] !== 'boolean') {
throw new Error('`options.nonOptionalFields` must be a boolean');
if ('nonOptionalDefaultFields' in rawConfig && typeof rawConfig['nonOptionalDefaultFields'] !== 'boolean') {
throw new Error('`options.nonOptionalDefaultFields` must be a boolean');
}
if ('typesPrefix' in rawConfig && typeof rawConfig['typesPrefix'] !== 'string') {
throw new Error('`options.typesPrefix` must be a string');
Expand All @@ -54,7 +54,7 @@ export function normalizeConfig(rawConfig: RawConfig): Config {
typesFile: rawConfig.typesFile,
skipTypename: rawConfig.skipTypename ?? false,
skipIsAbstractType: rawConfig.skipIsAbstractType ?? true,
nonOptionalFields: rawConfig.nonOptionalFields ?? false,
nonOptionalDefaultFields: rawConfig.nonOptionalDefaultFields ?? false,
typesPrefix: rawConfig.typesPrefix ?? '',
typesSuffix: rawConfig.typesSuffix ?? '',
convert: rawConfig.namingConvention
Expand Down
2 changes: 1 addition & 1 deletion src/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function fakeConfig(args: Partial<Config> = {}): Config {
typesFile: './types',
skipTypename: true,
skipIsAbstractType: true,
nonOptionalFields: false,
nonOptionalDefaultFields: false,
typesPrefix: '',
typesSuffix: '',
convert: convertFactory({}),
Expand Down