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

feat: Allow enums to be remapped like scalars #184

Merged
merged 14 commits into from
Apr 10, 2024
Merged

Conversation

kitten
Copy link
Member

@kitten kitten commented Apr 10, 2024

Resolves #180

Summary

This allows enums to be remapped like scalars. Specifically, if a user has a more qualified type for enums, they should be allowed to completely remap it.

Because Omit<Schema['types'], keyof Scalars> is potentially expensive for TypeScript to traverse, this PR avoids this pattern and instead refactors the code that resolves the types for enums. Specifically, mapEnum now outputs its type onto the enumValues property rather than type.

This PR also removes some redundant checks throughout the touched code path.

In practice, the following is now possible:

enum TestEnum {
  value = 'VALUE',
  test = 'TEST',
}

const graphql = initGraphQLTada<{
  introspection: someIntrospection;
  scalars: {
    // enum Test { VALUE, TEST }
    Test: TestEnum;
  };
}>();

In the future, we could add an additional check to graphql.scalar that checks the remapped type against the enumValues type to make sure they're compatible.

Set of changes

  • Refactor getInputObjectTypeRec and _getVariablesRec in variables.ts to move obj<> squashing
  • Remove SCALAR and ENUM markers from processed schema outputs
  • Remove redundant unknown | never output from processed schema outputs
  • Slim down introspection generics once again
  • Allow scalars to be an object of readonly properties
  • Output enum type onto enumValues and modify variables.ts and selection.ts to check type then enumValues

Copy link

changeset-bot bot commented Apr 10, 2024

🦋 Changeset detected

Latest commit: 9314abd

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kitten kitten merged commit ae59fc2 into main Apr 10, 2024
3 checks passed
@kitten kitten deleted the feat/enum-remapping branch April 10, 2024 12:33
@github-actions github-actions bot mentioned this pull request Apr 10, 2024
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.

RFC: Allow enums to be remapped like scalars
2 participants