Skip to content

fix(default-value): add flag to utilize generated enum types for default values #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

sutt0n
Copy link
Contributor

@sutt0n sutt0n commented Apr 29, 2024

The latest of this wonderful plugin has unfortunately broken default values for us and this adds the options, useEnumTypeAsDefaultValue to utilize the actual code-generated enum instead of the stringified version and I've exposed the namingConvention configuration map -- specifically to target enumValues. I'm unsure if others are having this issue as it's very new, and this could very well be an edge-case; furthermore, that's why I've added it behind a flag.

Related PR: #529

The Problem

For example, if one were to have the following enum:

export enum GuyFieriQuotes {
  DinersDriveInsAndDives = "DINERS_DRIVEINS_AND_DIVES",
  Funkalicious = "FUNKALICIOUS",
  WelcomeToFlavortown = "WELCOME_TO_FLAVORTOWN"
}

export const GuyFieriQuotesSchema = z.nativeEnum(GuyFieriQuotes);

We then have the generated schema:

export function FlavortownInputSchema(): z.ZodObject<Properties<FlavortownInput>> {
  return {
    quote: GuyFieriQuotesSchema.default("WELCOME_TO_FLAVORTOWN"),
  };
}

This would throw the following type errors:

Argument of type '"WELCOME_TO_FLAVORTOWN"' is not assignable to parameter of type 'GuyFieriQuotesSchema'.

Output of Solution

Adding this fix/feature generates this output:

export function FlavortownInputSchema(): z.ZodObject<Properties<FlavortownInput>> {
  return {
    quote: GuyFieriQuotesSchema.default(GuyFieriQuotes.WelcomeToFlavortown),
  };
}

Edit: Addresses #640

@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch from 7bcabf9 to f974993 Compare April 29, 2024 21:22
@sutt0n
Copy link
Contributor Author

sutt0n commented Apr 29, 2024

Force push resolved a merge conflict with upstream pnpm-lock.yml.

@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch 2 times, most recently from ae94242 to 853f91b Compare May 1, 2024 16:24
@sutt0n
Copy link
Contributor Author

sutt0n commented May 8, 2024

@Code-Hex Any chance we can get this reviewed at your earliest convenience, please and thank you?

@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch from 853f91b to 6eee9eb Compare May 8, 2024 16:43
@Code-Hex
Copy link
Owner

Code-Hex commented Jun 6, 2024

@sutt0n I'm sorry for delay!

I check this PR

@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch from 6eee9eb to 7233abf Compare June 6, 2024 15:34
@sutt0n
Copy link
Contributor Author

sutt0n commented Jun 6, 2024

@Code-Hex No worries! I see you've updated your pnpm version -- should I do the same, as well? I'm presuming to 9.x?

@sutt0n
Copy link
Contributor Author

sutt0n commented Jun 7, 2024

@Code-Hex The most updated push is the correct one that both builds and passes lint successfully. I had a problem after rebasing from upstream, and fixed the lint errors after resolving said problem. 🙂

@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch from 4ce0ae4 to 2c7acd4 Compare June 13, 2024 14:33
@Code-Hex
Copy link
Owner

@sutt0n Thanks! I will check this morning 🙏

Copy link
Owner

@Code-Hex Code-Hex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sutt0n If you can fix the areas I commented on and also add valibot support, I'll merge it in!

useEnumTypeAsDefaultValue?: boolean
/**
* @description Uses the full path of the enum type as the default value instead of the stringified value.
* @default { enumValues: "change-case-all#pascalCase" }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to use it with just this, so I think it's better to describe the URL using @link
https://the-guild.dev/graphql/codegen/docs/config-reference/naming-convention

JSDoc: https://jsdoc.app/tags-inline-link

Btw, I think it would be confusing if the user uses transformUnderscore or typeNames as this feature is not applicable. is it possible to support transformUnderscore and typeNames? or write a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Code-Hex I don't believe that Valibot supports default values for enum/"picklist" types at the moment, it seems like a new library. I don't see anything on the documentation https://valibot.dev/guides/enums/ around enums/"picklists" supporting default values, nor the API for enums: https://valibot.dev/api/enum/

I'll disregard Valibot for now, as it's not possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Code-Hex I've also added a comment for transformUnderscore and typeNames.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Code-Hex I've rebased against main and this is ready for review now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sutt0n Thanks! I agree ignore Valibot for now.
I'm sorry, but could you please add what you added to your comment in the README? I'll merge it once, so if you could make a PR for me, that would be great!

@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch from 2c7acd4 to be5a7b7 Compare June 20, 2024 00:05
@sutt0n sutt0n requested a review from Code-Hex June 20, 2024 00:15
@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch 2 times, most recently from fecbe8e to ff1296d Compare June 20, 2024 00:29
@sutt0n sutt0n force-pushed the moist-feat/enum-type-as-default-value branch from ff1296d to c4102fa Compare July 5, 2024 15:36
@Code-Hex
Copy link
Owner

Code-Hex commented Jul 7, 2024

I will merge this PR (#723) instead of this PR to resolve conflict.

Please just respond to this correspondence
#636 (comment)

@Code-Hex Code-Hex closed this Jul 7, 2024
@sutt0n sutt0n deleted the moist-feat/enum-type-as-default-value branch September 17, 2024 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants