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

Allow for tree-shaking the Kind enum #4268

Closed
wants to merge 2 commits into from
Closed

Conversation

JoviDeCroock
Copy link
Member

@JoviDeCroock JoviDeCroock commented Oct 29, 2024

Resolves #4253

This enables tree-shaking Kind - I opted to only do this with Kind as the other enums are either tiny or included by default if you are using parseSchema.

@JoviDeCroock JoviDeCroock requested a review from a team as a code owner October 29, 2024 13:45
@JoviDeCroock JoviDeCroock added the PR: polish 💅 PR doesn't change public API or any observed behaviour label Oct 29, 2024
Copy link

Hi @JoviDeCroock, I'm @github-actions bot happy to help you with this PR 👋

Supported commands

Please post this commands in separate comments and only one per comment:

  • @github-actions run-benchmark - Run benchmark comparing base and merge commits for this PR
  • @github-actions publish-pr-on-npm - Build package from this PR and publish it on NPM

@JoviDeCroock JoviDeCroock force-pushed the tree-shake-kind branch 2 times, most recently from 060bd56 to af9932c Compare October 29, 2024 13:52
@jasonkuhrt
Copy link

I closed my duplicate here #4270.

Choose a reason for hiding this comment

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

A name of kinds_ would keep it co-located with the other module that imports from this fwiw. Not sure what's consistent for this repo.

Choose a reason for hiding this comment

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

Another approach that works I think is:

import * as kinds_ from './kinds_.js'; // eslint-disable-line

 export * as Kind from './kinds_.js';

 /**
  * The set of allowed kind values for AST nodes.
  */
 export type Kind = (typeof kinds_)[keyof typeof kinds_];

src/language/kinds.ts Outdated Show resolved Hide resolved
src/language/kinds.ts Outdated Show resolved Hide resolved
@JoviDeCroock
Copy link
Member Author

@jasonkuhrt looks like not all TS versions support this

@Urigo
Copy link
Contributor

Urigo commented Oct 29, 2024

maybe v17 is a good opportunity to drop support of old Typescript and Node versions?

@JoviDeCroock
Copy link
Member Author

I think it's fair to cut https://github.com/graphql/graphql-js/blob/main/integrationTests/ts/package.json#L11-L22 down to i.e. 4.9 - recent but that might introduce some issues during migration - @yaacovCR has also expressed their concerns regarding ghosted exports which this also does so we might need to settle that first #4254 (comment)

Copy link

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

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

While writing this message #4254 (comment) I realized that we have an opportunity at the type level to improve the enum DX further.

Sometimes it is useful to be able to access the enum at the type level just like it would be at the term level e.g. Kind.FIELD // type level!.

As posted in that referenced message, here is a link demonstrating how to achieve that: https://www.typescriptlang.org/dev/bug-workbench/?#code/PTAEAEDMEsBsFMB2BDAtvAXKA1tRATAfQDoAXAZwCh4APABwHsAnU0AYwcXNeVAF5QARmr1mrUgE868ULwHCRjFu07dQAI36gATIrGhJ0jVt2UQEGAhTosuAmSq0lrAFSzyoANJ58oSEwZUUAByYmA7ImIAK3JgymhUZwMpGQBvWQAaYwBfPwCg0PCfEhi4vWVDGW8CfkpQAB9ZOsb1SjbzKDgkNEwDAHcGB3jE-XTq31z-QJCwiOjYto4uVnxocjpkUjYACy0ACgiMcYBKfgA+HB9F1R4tceJkSkrZO58H6+XjAXvW581vt7qIA

The difference is that:

  1. We define each enum member type next to its term const variable.
  2. When we define the Kind type we do so by importing the types from the module defining the members.

AFAICT this suggestion has no downside. Meanwhile its upside is increased symmetry between the term and type level and easier type level logic when type level access to specific enums are desired.

Comment on lines +49 to +92
| typeof NAME
| typeof DOCUMENT
| typeof OPERATION_DEFINITION
| typeof VARIABLE_DEFINITION
| typeof SELECTION_SET
| typeof FIELD
| typeof ARGUMENT
| typeof FRAGMENT_ARGUMENT
| typeof FRAGMENT_SPREAD
| typeof INLINE_FRAGMENT
| typeof FRAGMENT_DEFINITION
| typeof VARIABLE
| typeof INT
| typeof FLOAT
| typeof STRING
| typeof BOOLEAN
| typeof NULL
| typeof ENUM
| typeof LIST
| typeof OBJECT
| typeof OBJECT_FIELD
| typeof DIRECTIVE
| typeof NAMED_TYPE
| typeof LIST_TYPE
| typeof NON_NULL_TYPE
| typeof SCHEMA_DEFINITION
| typeof OPERATION_TYPE_DEFINITION
| typeof SCALAR_TYPE_DEFINITION
| typeof OBJECT_TYPE_DEFINITION
| typeof FIELD_DEFINITION
| typeof INPUT_VALUE_DEFINITION
| typeof INTERFACE_TYPE_DEFINITION
| typeof UNION_TYPE_DEFINITION
| typeof ENUM_TYPE_DEFINITION
| typeof ENUM_VALUE_DEFINITION
| typeof INPUT_OBJECT_TYPE_DEFINITION
| typeof DIRECTIVE_DEFINITION
| typeof SCHEMA_EXTENSION
| typeof SCALAR_TYPE_EXTENSION
| typeof OBJECT_TYPE_EXTENSION
| typeof INTERFACE_TYPE_EXTENSION
| typeof UNION_TYPE_EXTENSION
| typeof ENUM_TYPE_EXTENSION
| typeof INPUT_OBJECT_TYPE_EXTENSION;

Choose a reason for hiding this comment

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

Suggested change
| typeof NAME
| typeof DOCUMENT
| typeof OPERATION_DEFINITION
| typeof VARIABLE_DEFINITION
| typeof SELECTION_SET
| typeof FIELD
| typeof ARGUMENT
| typeof FRAGMENT_ARGUMENT
| typeof FRAGMENT_SPREAD
| typeof INLINE_FRAGMENT
| typeof FRAGMENT_DEFINITION
| typeof VARIABLE
| typeof INT
| typeof FLOAT
| typeof STRING
| typeof BOOLEAN
| typeof NULL
| typeof ENUM
| typeof LIST
| typeof OBJECT
| typeof OBJECT_FIELD
| typeof DIRECTIVE
| typeof NAMED_TYPE
| typeof LIST_TYPE
| typeof NON_NULL_TYPE
| typeof SCHEMA_DEFINITION
| typeof OPERATION_TYPE_DEFINITION
| typeof SCALAR_TYPE_DEFINITION
| typeof OBJECT_TYPE_DEFINITION
| typeof FIELD_DEFINITION
| typeof INPUT_VALUE_DEFINITION
| typeof INTERFACE_TYPE_DEFINITION
| typeof UNION_TYPE_DEFINITION
| typeof ENUM_TYPE_DEFINITION
| typeof ENUM_VALUE_DEFINITION
| typeof INPUT_OBJECT_TYPE_DEFINITION
| typeof DIRECTIVE_DEFINITION
| typeof SCHEMA_EXTENSION
| typeof SCALAR_TYPE_EXTENSION
| typeof OBJECT_TYPE_EXTENSION
| typeof INTERFACE_TYPE_EXTENSION
| typeof UNION_TYPE_EXTENSION
| typeof ENUM_TYPE_EXTENSION
| typeof INPUT_OBJECT_TYPE_EXTENSION;
| NAME
| DOCUMENT
| ...

Comment on lines +5 to +7
export const DOCUMENT = 'Document';
export const OPERATION_DEFINITION = 'OperationDefinition';
export const VARIABLE_DEFINITION = 'VariableDefinition';

Choose a reason for hiding this comment

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

Suggested change
export const DOCUMENT = 'Document';
export const OPERATION_DEFINITION = 'OperationDefinition';
export const VARIABLE_DEFINITION = 'VariableDefinition';
export const DOCUMENT = 'Document';
export type DOCUMENT = typeof DOCUMENT
export const OPERATION_DEFINITION = 'OperationDefinition';
export type OPERATION_DEFINITION = typeof OPERATION_DEFINITION
export const VARIABLE_DEFINITION = 'VariableDefinition';
// ...

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I did not do these is mainly that it would cause a huge pain with our eslint rule no-redeclare

Choose a reason for hiding this comment

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

Would aslant complain about that? If so though, could we just ignore with an ignore directive at the top of the file?

Base automatically changed from get-rid-of-enums to main October 30, 2024 17:24
Copy link

netlify bot commented Oct 30, 2024

Deploy Preview for compassionate-pike-271cb3 ready!

Name Link
🔨 Latest commit c225a72
🔍 Latest deploy log https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/672272e7565eef0008b7387a
😎 Deploy Preview https://deploy-preview-4268--compassionate-pike-271cb3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

JoviDeCroock and others added 2 commits October 30, 2024 18:54
Co-authored-by: Jason Kuhrt <jasonkuhrt@me.com>
@JoviDeCroock
Copy link
Member Author

Going to close this as I won't have time to fine tune this. If someone wants to take this over feel free to, I might get back to it in the future.

@JoviDeCroock JoviDeCroock deleted the tree-shake-kind branch November 6, 2024 05:48
@jasonkuhrt
Copy link

@JoviDeCroock Sure thing I'll take a swing, thanks for clearing the way up to this point!

@jasonkuhrt
Copy link

@JoviDeCroock @Urigo I've got something ready for review here #4270.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: polish 💅 PR doesn't change public API or any observed behaviour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants