Skip to content

Commit

Permalink
feat(packages/cli): add enumStyle config option (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Apr 23, 2024
1 parent f824c48 commit eab8c7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-laws-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gqty/cli': minor
---

Add `enumStyle` config option.
1 change: 1 addition & 0 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const defaultConfig: SetRequired<
destination: './src/gqty/index.ts',
subscriptions: false,
javascriptOutput: false,
enumStyle: 'enum',
enumsAsStrings: false,
enumsAsConst: false,
preImport: '',
Expand Down
71 changes: 34 additions & 37 deletions packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,27 @@ export interface GenerateOptions {
*/
react?: boolean;
/**
* Generated enum styles.
*
* 1. assertion: Enums will be generated as `const assertion` style objects.
* 2. const: Enums will be prefixed with the `const` keyword.
* 3. enum: Enums will be generated as TypeScript enums.
* 4. string: Enums will be generated as string unions.
*
* @default "enum"
*/
enumStyle?: 'assertion' | 'const' | 'enum' | 'string';
/**
* @deprecated Use `enumStyle` instead.
*
* Define enums as string types instead of enums objects
*
* @default false
*/
enumsAsStrings?: boolean;
/**
* @deprecated Use `enumStyle` instead.
*
* Define enums as const types instead of enums objects
*
* @default false
Expand Down Expand Up @@ -110,16 +125,21 @@ export interface TransformSchemaOptions {
export async function generate(
schema: GraphQLSchema,
{
endpoint,
enumsAsConst,
enumsAsStrings,
introspection,
javascriptOutput,
preImport,
react,
scalarTypes,
subscriptions,
transformSchema,
enumStyle = enumsAsConst
? 'assertion'
: enumsAsStrings
? 'string'
: defaultConfig.enumStyle,
introspection = defaultConfig.introspections,
endpoint = introspection?.endpoint ?? defaultConfig.endpoint,
javascriptOutput: isJavascriptOutput = defaultConfig.javascriptOutput,
preImport = defaultConfig.preImport,
react = defaultConfig.react,
scalarTypes = defaultConfig.scalarTypes,
subscriptions = defaultConfig.subscriptions,
transformSchema = defaultConfig.transformSchema,
}: GQtyConfig = {},
{ ignoreArgs }: TransformSchemaOptions = {}
): Promise<{
Expand All @@ -130,39 +150,15 @@ export async function generate(
scalarsEnumsHash: ScalarsEnumsHash;
isJavascriptOutput: boolean;
}> {
const isJavascriptOutput = javascriptOutput ?? defaultConfig.javascriptOutput;

if (isJavascriptOutput) {
if (enumsAsStrings) {
if (enumStyle !== 'string' && enumStyle !== defaultConfig.enumStyle) {
console.warn(
`"enumsAsStrings" is automatically set as "true" with "javascriptOutput" enabled.`
`"enumStyle" must be string unions with "javascriptOutput" enabled.`
);
}
enumsAsStrings = true;
} else {
enumsAsStrings ??= false;
}

if (isJavascriptOutput) {
if (enumsAsConst) {
console.warn(
`"enumsAsConst" is automatically set as "false" with "javascriptOutput" enabled.`
);
}
enumsAsConst = false;
enumStyle = 'string';
}
enumsAsConst ??= enumsAsConst ?? defaultConfig.enumsAsConst;

scalarTypes ||= scalarTypes || defaultConfig.scalarTypes;
endpoint ||=
endpoint ||
introspection?.endpoint ||
defaultConfig.endpoint ||
defaultConfig.introspection.endpoint;

react ??= defaultConfig.react;
preImport ??= defaultConfig.preImport;
subscriptions ??= defaultConfig.subscriptions;

const { format } = formatPrettier({
parser: 'typescript',
Expand All @@ -189,13 +185,14 @@ export async function generate(
plugins: [
{
typescript: {
constEnums: enumStyle === 'const',
onlyOperationTypes: true,
declarationKind: 'interface',
addUnderscoreToArgsType: true,
scalars: scalarTypes,
namingConvention: 'keep',
enumsAsTypes: enumsAsStrings,
enumsAsConst: enumsAsConst,
enumsAsTypes: enumStyle === 'string',
enumsAsConst: enumStyle === 'assertion',
} satisfies deps.typescriptPlugin.TypeScriptPluginConfig,
},
],
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ test('completely invalid config', () => {
"destination": "./src/gqty/index.ts",
"subscriptions": false,
"javascriptOutput": false,
"enumStyle": "enum",
"enumsAsStrings": false,
"enumsAsConst": false,
"preImport": ""
Expand Down

0 comments on commit eab8c7a

Please sign in to comment.