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

RFC: Allow enums to be remapped like scalars #180

Closed
Maximaximum opened this issue Apr 9, 2024 · 4 comments · Fixed by #184
Closed

RFC: Allow enums to be remapped like scalars #180

Maximaximum opened this issue Apr 9, 2024 · 4 comments · Fixed by #184
Labels
future 🔮 An enhancement or feature proposal that will be addressed after the next release

Comments

@Maximaximum
Copy link

Summary

It would be great if there was a configuration option to make gql.tada emit typescript enums instead of union types for graphql enums.

There is a huge difference between these, one of them is that a typescript enum is an object that gets it into the transpiled js code, unlike a union type.

Moreover, this would imrpove compatibility of tada.gql with other existing codegen libs, namely @graphql-codegen. This, in turn, would ease the adoption and migration process for existing projects. In my particular case, the lack of this feature is an actual blocker for migrating my existing projects to gql.tada.

Proposed Solution

Let’s say we have a following piece of graphql schema:

query {
  applicant {
    status: APPLICANT_STATUS
  }
}

Schema interpolation should return something like this:

export enum APPLICANT_STATUS {
  ONLINE: "ONLINE",
  OFFLINE: "OFFLINE",
  BLOCKED: "BLOCKED"
}

export type Applicant {
  status: APPLICANT_STATUS;
}

Whereas currently it returns

export enum APPLICANT_STATUS {
  ONLINE: "ONLINE",
  OFFLINE: "OFFLINE",
  BLOCKED: "BLOCKED"
}

export type Applicant {
  status: "ONLINE" | "OFFLINE" | "BLOCKED";
}

Requirements

@Maximaximum Maximaximum added the future 🔮 An enhancement or feature proposal that will be addressed after the next release label Apr 9, 2024
@Maximaximum
Copy link
Author

FWIW, I’m aware we were having this issue, which has been resolved #44

However, it does not address the issue I’m describing here

@kitten
Copy link
Member

kitten commented Apr 9, 2024

This requires explicit codegen for each enum, and doesn't really achieve much. In fact, it introduces more problems that people aren't often aware of in TypeScript.

enums are value-unique and reference-incompatible in values, so once they're generated and used, they often must be used.

const enums on the other hand are still often value-unique, but at least reference-compatible in terms of the values they contain.

Basically to put it short:

Moreover, this would imrpove compatibility of tada.gql with other existing codegen libs, namely @graphql-codegen.

This is not an explicit goal of the project and there are a lot of “default” options that would need to change in GCG to be more compatible with gql.tada. Instead, we often take the approach of doing the right thing and generating correct & compatible types first, by default.


Related Threads:

@sitharus
Copy link

How about the ability to use existing enums, similar to the scalar mapping? I have a fork with the feature as my work needs it to integrate gql.tada in to a legacy codebase - without this the integration is impossible. If this is compatible with the project I can tidy up the code and make a PR. It's a pretty small change.

It does require manually mapping every enum, but that's kind of the point as we want to get away from the legacy eventually.

@kitten kitten changed the title RFC: Emit enums from graphql schema Enums instead of union types RFC: Allow enums to be remapped like scalars Apr 10, 2024
@kitten
Copy link
Member

kitten commented Apr 10, 2024

@sitharus: That's an excellent idea! 💯

I just pushed a PR to implement that, so no worries. I realised that it's a minimal change with Omit<Schema['types'], keyof Scalars> in introspection.tsaddIntrospectionScalars type, but I believe that may have a hidden cost for TypeScript’s type checker for huge schemas.
The linked PR refactors the types a little, so we can differentiate between generated enum value types and the scalar types. This should also allow us to check enumValues against re-mapped types in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future 🔮 An enhancement or feature proposal that will be addressed after the next release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants