Skip to content
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

Generate object literals for enums #64

Closed
jasonkuhrt opened this issue Nov 1, 2021 · 7 comments
Closed

Generate object literals for enums #64

jasonkuhrt opened this issue Nov 1, 2021 · 7 comments

Comments

@jasonkuhrt
Copy link

We're currently rolling out this pattern.

export * from '@genql/client'
import type { PlanetscaleRegion as PlanetscaleRegion_ } from '@genql/client'

export type PlanetscaleRegion = PlanetscaleRegion_

// TODO get object literals for enums from Genql core.
export const PlanetscaleRegion = {
  us_east: 'us_east',
  us_west: 'us_west',
  ap_northeast: 'ap_northeast',
  ap_south: 'ap_south',
  ap_southeast: 'ap_southeast',
  eu_west: 'eu_west',
} as const

This is so that frontend code can create enum members and more (such as create zod schema types based on the enum for frontend form validation).

It would be great to have Genql ship (generate) these object literals by default.

@remorses
Copy link
Owner

remorses commented Nov 1, 2021

I am gonna use a different name for the new constant object, is mapToEnumName a good name?

export const mapToPlanetscaleRegion = {
  us_east: 'us_east',
  us_west: 'us_west',
  ap_northeast: 'ap_northeast',
  ap_south: 'ap_south',
  ap_southeast: 'ap_southeast',
  eu_west: 'eu_west',
} as const

@jasonkuhrt
Copy link
Author

I think I get that, its a map of identifiers to enum names (the values). Not sure its my favourite but subjective somewhat.

Some considerations:

  • If we just match the type name then it means there's only one name to "think about". In type position it is a union and in term position it is an object (and getting the union is trivial like this: Object.values(<EnumName>).
  • Prisma takes this approach fwiw (just some precedent/thinking in this area to maybe borrow from)

@jasonkuhrt
Copy link
Author

If we do go with a name that is different than the type name, do we want to centralize this but co-locating with the domain category or centralizing on the function category?

mapToEnumName    -- group by function
EnumNameMap      -- group by domain

@remorses
Copy link
Owner

remorses commented Nov 1, 2021

I think group by function is better: this way the name would start with lower case letter, which makes it different from a type name that is usually capitalized

@jasonkuhrt
Copy link
Author

@remorses good enough for us! Left some subjective thoughts above about naming but mostly important here is having the data period so whatever you think is best ultimately!

@jasonkuhrt
Copy link
Author

jasonkuhrt commented Nov 1, 2021

If you can allow a way to customize the naming pattern in the CLI/config file that would be ideal :)

Customize examples:

${EnumName}           // Our personal preference
staticText${EnumName}
static_text_${snakeCase(EnumName)}

I guess anything requiring transformations would be relegated to a config file, not on the CLI.

@remorses
Copy link
Owner

remorses commented Nov 1, 2021

You can now import enumPlanetscaleRegion and do enumPlanetscaleRegion.us_west

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

No branches or pull requests

2 participants