-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
Hi @JoviDeCroock, I'm @github-actions bot happy to help you with this PR 👋 Supported commandsPlease post this commands in separate comments and only one per comment:
|
060bd56
to
af9932c
Compare
I closed my duplicate here #4270. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_];
@jasonkuhrt looks like not all TS versions support this |
maybe v17 is a good opportunity to drop support of old Typescript and Node versions? |
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) |
There was a problem hiding this 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:
- We define each enum member type next to its term
const
variable. - 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.
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 | |
| ... |
export const DOCUMENT = 'Document'; | ||
export const OPERATION_DEFINITION = 'OperationDefinition'; | ||
export const VARIABLE_DEFINITION = 'VariableDefinition'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'; | |
// ... |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
af9932c
to
f45208a
Compare
✅ Deploy Preview for compassionate-pike-271cb3 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Co-authored-by: Jason Kuhrt <[email protected]>
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 Sure thing I'll take a swing, thanks for clearing the way up to this point! |
@JoviDeCroock @Urigo I've got something ready for review here #4270. |
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 usingparseSchema
.