From 45846f47f61b9357b82c04f844cdfcf552a5c3d7 Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Wed, 3 Nov 2021 15:41:36 -0400 Subject: [PATCH] avoid using enums in favor of frozen object literals --- src/jsutils/ValueTypes.ts | 3 + src/language/ast.ts | 117 ++++++++++++++------------- src/language/directiveLocation.ts | 47 ++++++----- src/language/kinds.ts | 101 ++++++++++++----------- src/language/tokenKind.ts | 55 +++++++------ src/type/introspection.ts | 24 +++--- src/utilities/buildClientSchema.ts | 2 - src/utilities/findBreakingChanges.ts | 61 +++++++------- 8 files changed, 221 insertions(+), 189 deletions(-) create mode 100644 src/jsutils/ValueTypes.ts diff --git a/src/jsutils/ValueTypes.ts b/src/jsutils/ValueTypes.ts new file mode 100644 index 0000000000..d1e34d9442 --- /dev/null +++ b/src/jsutils/ValueTypes.ts @@ -0,0 +1,3 @@ +/** Used to extract values from enum-like objects. */ +export type ValueTypes = + TObj[keyof TObj]; diff --git a/src/language/ast.ts b/src/language/ast.ts index 0b47c6a599..0954096532 100644 --- a/src/language/ast.ts +++ b/src/language/ast.ts @@ -1,3 +1,5 @@ +import type { ValueTypes } from '../jsutils/ValueTypes'; + import type { Kind } from './kinds'; import type { Source } from './source'; import type { TokenKind } from './tokenKind'; @@ -309,7 +311,7 @@ export function isNode(maybeNode: any): maybeNode is ASTNode { /** Name */ export interface NameNode { - readonly kind: Kind.NAME; + readonly kind: typeof Kind.NAME; readonly loc?: Location | undefined; readonly value: string; } @@ -317,7 +319,7 @@ export interface NameNode { /** Document */ export interface DocumentNode { - readonly kind: Kind.DOCUMENT; + readonly kind: typeof Kind.DOCUMENT; readonly loc?: Location | undefined; readonly definitions: ReadonlyArray; } @@ -332,7 +334,7 @@ export type ExecutableDefinitionNode = | FragmentDefinitionNode; export interface OperationDefinitionNode { - readonly kind: Kind.OPERATION_DEFINITION; + readonly kind: typeof Kind.OPERATION_DEFINITION; readonly loc?: Location | undefined; readonly operation: OperationTypeNode; readonly name?: NameNode | undefined; @@ -343,14 +345,17 @@ export interface OperationDefinitionNode { readonly selectionSet: SelectionSetNode; } -export enum OperationTypeNode { - QUERY = 'query', - MUTATION = 'mutation', - SUBSCRIPTION = 'subscription', -} +export const OperationTypeNode = Object.freeze({ + QUERY: 'query', + MUTATION: 'mutation', + SUBSCRIPTION: 'subscription', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type OperationTypeNode = ValueTypes; export interface VariableDefinitionNode { - readonly kind: Kind.VARIABLE_DEFINITION; + readonly kind: typeof Kind.VARIABLE_DEFINITION; readonly loc?: Location | undefined; readonly variable: VariableNode; readonly type: TypeNode; @@ -359,13 +364,13 @@ export interface VariableDefinitionNode { } export interface VariableNode { - readonly kind: Kind.VARIABLE; + readonly kind: typeof Kind.VARIABLE; readonly loc?: Location | undefined; readonly name: NameNode; } export interface SelectionSetNode { - kind: Kind.SELECTION_SET; + kind: typeof Kind.SELECTION_SET; loc?: Location | undefined; selections: ReadonlyArray; } @@ -373,7 +378,7 @@ export interface SelectionSetNode { export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode; export interface FieldNode { - readonly kind: Kind.FIELD; + readonly kind: typeof Kind.FIELD; readonly loc?: Location | undefined; readonly alias?: NameNode | undefined; readonly name: NameNode; @@ -391,32 +396,32 @@ export type NullabilityAssertionNode = | ListNullabilityOperatorNode; export interface ListNullabilityOperatorNode { - readonly kind: Kind.LIST_NULLABILITY_OPERATOR; + readonly kind: typeof Kind.LIST_NULLABILITY_OPERATOR; readonly loc?: Location | undefined; readonly nullabilityAssertion?: NullabilityAssertionNode | undefined; } export interface NonNullAssertionNode { - readonly kind: Kind.NON_NULL_ASSERTION; + readonly kind: typeof Kind.NON_NULL_ASSERTION; readonly loc?: Location | undefined; readonly nullabilityAssertion?: ListNullabilityOperatorNode | undefined; } export interface ErrorBoundaryNode { - readonly kind: Kind.ERROR_BOUNDARY; + readonly kind: typeof Kind.ERROR_BOUNDARY; readonly loc?: Location | undefined; readonly nullabilityAssertion?: ListNullabilityOperatorNode | undefined; } export interface ArgumentNode { - readonly kind: Kind.ARGUMENT; + readonly kind: typeof Kind.ARGUMENT; readonly loc?: Location | undefined; readonly name: NameNode; readonly value: ValueNode; } export interface ConstArgumentNode { - readonly kind: Kind.ARGUMENT; + readonly kind: typeof Kind.ARGUMENT; readonly loc?: Location | undefined; readonly name: NameNode; readonly value: ConstValueNode; @@ -425,14 +430,14 @@ export interface ConstArgumentNode { /** Fragments */ export interface FragmentSpreadNode { - readonly kind: Kind.FRAGMENT_SPREAD; + readonly kind: typeof Kind.FRAGMENT_SPREAD; readonly loc?: Location | undefined; readonly name: NameNode; readonly directives?: ReadonlyArray | undefined; } export interface InlineFragmentNode { - readonly kind: Kind.INLINE_FRAGMENT; + readonly kind: typeof Kind.INLINE_FRAGMENT; readonly loc?: Location | undefined; readonly typeCondition?: NamedTypeNode | undefined; readonly directives?: ReadonlyArray | undefined; @@ -440,7 +445,7 @@ export interface InlineFragmentNode { } export interface FragmentDefinitionNode { - readonly kind: Kind.FRAGMENT_DEFINITION; + readonly kind: typeof Kind.FRAGMENT_DEFINITION; readonly loc?: Location | undefined; readonly name: NameNode; /** @deprecated variableDefinitions will be removed in v17.0.0 */ @@ -476,74 +481,74 @@ export type ConstValueNode = | ConstObjectValueNode; export interface IntValueNode { - readonly kind: Kind.INT; + readonly kind: typeof Kind.INT; readonly loc?: Location | undefined; readonly value: string; } export interface FloatValueNode { - readonly kind: Kind.FLOAT; + readonly kind: typeof Kind.FLOAT; readonly loc?: Location | undefined; readonly value: string; } export interface StringValueNode { - readonly kind: Kind.STRING; + readonly kind: typeof Kind.STRING; readonly loc?: Location | undefined; readonly value: string; readonly block?: boolean | undefined; } export interface BooleanValueNode { - readonly kind: Kind.BOOLEAN; + readonly kind: typeof Kind.BOOLEAN; readonly loc?: Location | undefined; readonly value: boolean; } export interface NullValueNode { - readonly kind: Kind.NULL; + readonly kind: typeof Kind.NULL; readonly loc?: Location | undefined; } export interface EnumValueNode { - readonly kind: Kind.ENUM; + readonly kind: typeof Kind.ENUM; readonly loc?: Location | undefined; readonly value: string; } export interface ListValueNode { - readonly kind: Kind.LIST; + readonly kind: typeof Kind.LIST; readonly loc?: Location | undefined; readonly values: ReadonlyArray; } export interface ConstListValueNode { - readonly kind: Kind.LIST; + readonly kind: typeof Kind.LIST; readonly loc?: Location | undefined; readonly values: ReadonlyArray; } export interface ObjectValueNode { - readonly kind: Kind.OBJECT; + readonly kind: typeof Kind.OBJECT; readonly loc?: Location | undefined; readonly fields: ReadonlyArray; } export interface ConstObjectValueNode { - readonly kind: Kind.OBJECT; + readonly kind: typeof Kind.OBJECT; readonly loc?: Location | undefined; readonly fields: ReadonlyArray; } export interface ObjectFieldNode { - readonly kind: Kind.OBJECT_FIELD; + readonly kind: typeof Kind.OBJECT_FIELD; readonly loc?: Location | undefined; readonly name: NameNode; readonly value: ValueNode; } export interface ConstObjectFieldNode { - readonly kind: Kind.OBJECT_FIELD; + readonly kind: typeof Kind.OBJECT_FIELD; readonly loc?: Location | undefined; readonly name: NameNode; readonly value: ConstValueNode; @@ -552,14 +557,14 @@ export interface ConstObjectFieldNode { /** Directives */ export interface DirectiveNode { - readonly kind: Kind.DIRECTIVE; + readonly kind: typeof Kind.DIRECTIVE; readonly loc?: Location | undefined; readonly name: NameNode; readonly arguments?: ReadonlyArray | undefined; } export interface ConstDirectiveNode { - readonly kind: Kind.DIRECTIVE; + readonly kind: typeof Kind.DIRECTIVE; readonly loc?: Location | undefined; readonly name: NameNode; readonly arguments?: ReadonlyArray | undefined; @@ -570,19 +575,19 @@ export interface ConstDirectiveNode { export type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode; export interface NamedTypeNode { - readonly kind: Kind.NAMED_TYPE; + readonly kind: typeof Kind.NAMED_TYPE; readonly loc?: Location | undefined; readonly name: NameNode; } export interface ListTypeNode { - readonly kind: Kind.LIST_TYPE; + readonly kind: typeof Kind.LIST_TYPE; readonly loc?: Location | undefined; readonly type: TypeNode; } export interface NonNullTypeNode { - readonly kind: Kind.NON_NULL_TYPE; + readonly kind: typeof Kind.NON_NULL_TYPE; readonly loc?: Location | undefined; readonly type: NamedTypeNode | ListTypeNode; } @@ -595,7 +600,7 @@ export type TypeSystemDefinitionNode = | DirectiveDefinitionNode; export interface SchemaDefinitionNode { - readonly kind: Kind.SCHEMA_DEFINITION; + readonly kind: typeof Kind.SCHEMA_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly directives?: ReadonlyArray | undefined; @@ -603,7 +608,7 @@ export interface SchemaDefinitionNode { } export interface OperationTypeDefinitionNode { - readonly kind: Kind.OPERATION_TYPE_DEFINITION; + readonly kind: typeof Kind.OPERATION_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly operation: OperationTypeNode; readonly type: NamedTypeNode; @@ -620,7 +625,7 @@ export type TypeDefinitionNode = | InputObjectTypeDefinitionNode; export interface ScalarTypeDefinitionNode { - readonly kind: Kind.SCALAR_TYPE_DEFINITION; + readonly kind: typeof Kind.SCALAR_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -628,7 +633,7 @@ export interface ScalarTypeDefinitionNode { } export interface ObjectTypeDefinitionNode { - readonly kind: Kind.OBJECT_TYPE_DEFINITION; + readonly kind: typeof Kind.OBJECT_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -638,7 +643,7 @@ export interface ObjectTypeDefinitionNode { } export interface FieldDefinitionNode { - readonly kind: Kind.FIELD_DEFINITION; + readonly kind: typeof Kind.FIELD_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -648,7 +653,7 @@ export interface FieldDefinitionNode { } export interface InputValueDefinitionNode { - readonly kind: Kind.INPUT_VALUE_DEFINITION; + readonly kind: typeof Kind.INPUT_VALUE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -658,7 +663,7 @@ export interface InputValueDefinitionNode { } export interface InterfaceTypeDefinitionNode { - readonly kind: Kind.INTERFACE_TYPE_DEFINITION; + readonly kind: typeof Kind.INTERFACE_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -668,7 +673,7 @@ export interface InterfaceTypeDefinitionNode { } export interface UnionTypeDefinitionNode { - readonly kind: Kind.UNION_TYPE_DEFINITION; + readonly kind: typeof Kind.UNION_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -677,7 +682,7 @@ export interface UnionTypeDefinitionNode { } export interface EnumTypeDefinitionNode { - readonly kind: Kind.ENUM_TYPE_DEFINITION; + readonly kind: typeof Kind.ENUM_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -686,7 +691,7 @@ export interface EnumTypeDefinitionNode { } export interface EnumValueDefinitionNode { - readonly kind: Kind.ENUM_VALUE_DEFINITION; + readonly kind: typeof Kind.ENUM_VALUE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -694,7 +699,7 @@ export interface EnumValueDefinitionNode { } export interface InputObjectTypeDefinitionNode { - readonly kind: Kind.INPUT_OBJECT_TYPE_DEFINITION; + readonly kind: typeof Kind.INPUT_OBJECT_TYPE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -705,7 +710,7 @@ export interface InputObjectTypeDefinitionNode { /** Directive Definitions */ export interface DirectiveDefinitionNode { - readonly kind: Kind.DIRECTIVE_DEFINITION; + readonly kind: typeof Kind.DIRECTIVE_DEFINITION; readonly loc?: Location | undefined; readonly description?: StringValueNode | undefined; readonly name: NameNode; @@ -719,7 +724,7 @@ export interface DirectiveDefinitionNode { export type TypeSystemExtensionNode = SchemaExtensionNode | TypeExtensionNode; export interface SchemaExtensionNode { - readonly kind: Kind.SCHEMA_EXTENSION; + readonly kind: typeof Kind.SCHEMA_EXTENSION; readonly loc?: Location | undefined; readonly directives?: ReadonlyArray | undefined; readonly operationTypes?: @@ -738,14 +743,14 @@ export type TypeExtensionNode = | InputObjectTypeExtensionNode; export interface ScalarTypeExtensionNode { - readonly kind: Kind.SCALAR_TYPE_EXTENSION; + readonly kind: typeof Kind.SCALAR_TYPE_EXTENSION; readonly loc?: Location | undefined; readonly name: NameNode; readonly directives?: ReadonlyArray | undefined; } export interface ObjectTypeExtensionNode { - readonly kind: Kind.OBJECT_TYPE_EXTENSION; + readonly kind: typeof Kind.OBJECT_TYPE_EXTENSION; readonly loc?: Location | undefined; readonly name: NameNode; readonly interfaces?: ReadonlyArray | undefined; @@ -754,7 +759,7 @@ export interface ObjectTypeExtensionNode { } export interface InterfaceTypeExtensionNode { - readonly kind: Kind.INTERFACE_TYPE_EXTENSION; + readonly kind: typeof Kind.INTERFACE_TYPE_EXTENSION; readonly loc?: Location | undefined; readonly name: NameNode; readonly interfaces?: ReadonlyArray | undefined; @@ -763,7 +768,7 @@ export interface InterfaceTypeExtensionNode { } export interface UnionTypeExtensionNode { - readonly kind: Kind.UNION_TYPE_EXTENSION; + readonly kind: typeof Kind.UNION_TYPE_EXTENSION; readonly loc?: Location | undefined; readonly name: NameNode; readonly directives?: ReadonlyArray | undefined; @@ -771,7 +776,7 @@ export interface UnionTypeExtensionNode { } export interface EnumTypeExtensionNode { - readonly kind: Kind.ENUM_TYPE_EXTENSION; + readonly kind: typeof Kind.ENUM_TYPE_EXTENSION; readonly loc?: Location | undefined; readonly name: NameNode; readonly directives?: ReadonlyArray | undefined; @@ -779,7 +784,7 @@ export interface EnumTypeExtensionNode { } export interface InputObjectTypeExtensionNode { - readonly kind: Kind.INPUT_OBJECT_TYPE_EXTENSION; + readonly kind: typeof Kind.INPUT_OBJECT_TYPE_EXTENSION; readonly loc?: Location | undefined; readonly name: NameNode; readonly directives?: ReadonlyArray | undefined; diff --git a/src/language/directiveLocation.ts b/src/language/directiveLocation.ts index b10214d47f..cdc8f23ce1 100644 --- a/src/language/directiveLocation.ts +++ b/src/language/directiveLocation.ts @@ -1,26 +1,31 @@ +import type { ValueTypes } from '../jsutils/ValueTypes'; + /** * The set of allowed directive location values. */ -export enum DirectiveLocation { +export const DirectiveLocation = Object.freeze({ /** Request Definitions */ - QUERY = 'QUERY', - MUTATION = 'MUTATION', - SUBSCRIPTION = 'SUBSCRIPTION', - FIELD = 'FIELD', - FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION', - FRAGMENT_SPREAD = 'FRAGMENT_SPREAD', - INLINE_FRAGMENT = 'INLINE_FRAGMENT', - VARIABLE_DEFINITION = 'VARIABLE_DEFINITION', + QUERY: 'QUERY', + MUTATION: 'MUTATION', + SUBSCRIPTION: 'SUBSCRIPTION', + FIELD: 'FIELD', + FRAGMENT_DEFINITION: 'FRAGMENT_DEFINITION', + FRAGMENT_SPREAD: 'FRAGMENT_SPREAD', + INLINE_FRAGMENT: 'INLINE_FRAGMENT', + VARIABLE_DEFINITION: 'VARIABLE_DEFINITION', /** Type System Definitions */ - SCHEMA = 'SCHEMA', - SCALAR = 'SCALAR', - OBJECT = 'OBJECT', - FIELD_DEFINITION = 'FIELD_DEFINITION', - ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION', - INTERFACE = 'INTERFACE', - UNION = 'UNION', - ENUM = 'ENUM', - ENUM_VALUE = 'ENUM_VALUE', - INPUT_OBJECT = 'INPUT_OBJECT', - INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION', -} + SCHEMA: 'SCHEMA', + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + FIELD_DEFINITION: 'FIELD_DEFINITION', + ARGUMENT_DEFINITION: 'ARGUMENT_DEFINITION', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + ENUM_VALUE: 'ENUM_VALUE', + INPUT_OBJECT: 'INPUT_OBJECT', + INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type DirectiveLocation = ValueTypes; diff --git a/src/language/kinds.ts b/src/language/kinds.ts index d606c12cbe..1911605c7d 100644 --- a/src/language/kinds.ts +++ b/src/language/kinds.ts @@ -1,74 +1,79 @@ +import type { ValueTypes } from '../jsutils/ValueTypes'; + /** * The set of allowed kind values for AST nodes. */ -export enum Kind { +export const Kind = Object.freeze({ /** Name */ - NAME = 'Name', + NAME: 'Name', /** Document */ - DOCUMENT = 'Document', - OPERATION_DEFINITION = 'OperationDefinition', - VARIABLE_DEFINITION = 'VariableDefinition', - SELECTION_SET = 'SelectionSet', - FIELD = 'Field', - ARGUMENT = 'Argument', + DOCUMENT: 'Document', + OPERATION_DEFINITION: 'OperationDefinition', + VARIABLE_DEFINITION: 'VariableDefinition', + SELECTION_SET: 'SelectionSet', + FIELD: 'Field', + ARGUMENT: 'Argument', /** Nullability Modifiers */ - LIST_NULLABILITY_OPERATOR = 'ListNullabilityOperator', - NON_NULL_ASSERTION = 'NonNullAssertion', - ERROR_BOUNDARY = 'ErrorBoundary', + LIST_NULLABILITY_OPERATOR: 'ListNullabilityOperator', + NON_NULL_ASSERTION: 'NonNullAssertion', + ERROR_BOUNDARY: 'ErrorBoundary', /** Fragments */ - FRAGMENT_SPREAD = 'FragmentSpread', - INLINE_FRAGMENT = 'InlineFragment', - FRAGMENT_DEFINITION = 'FragmentDefinition', + FRAGMENT_SPREAD: 'FragmentSpread', + INLINE_FRAGMENT: 'InlineFragment', + FRAGMENT_DEFINITION: 'FragmentDefinition', /** Values */ - VARIABLE = 'Variable', - INT = 'IntValue', - FLOAT = 'FloatValue', - STRING = 'StringValue', - BOOLEAN = 'BooleanValue', - NULL = 'NullValue', - ENUM = 'EnumValue', - LIST = 'ListValue', - OBJECT = 'ObjectValue', - OBJECT_FIELD = 'ObjectField', + VARIABLE: 'Variable', + INT: 'IntValue', + FLOAT: 'FloatValue', + STRING: 'StringValue', + BOOLEAN: 'BooleanValue', + NULL: 'NullValue', + ENUM: 'EnumValue', + LIST: 'ListValue', + OBJECT: 'ObjectValue', + OBJECT_FIELD: 'ObjectField', /** Directives */ - DIRECTIVE = 'Directive', + DIRECTIVE: 'Directive', /** Types */ - NAMED_TYPE = 'NamedType', - LIST_TYPE = 'ListType', - NON_NULL_TYPE = 'NonNullType', + NAMED_TYPE: 'NamedType', + LIST_TYPE: 'ListType', + NON_NULL_TYPE: 'NonNullType', /** Type System Definitions */ - SCHEMA_DEFINITION = 'SchemaDefinition', - OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition', + SCHEMA_DEFINITION: 'SchemaDefinition', + OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition', /** Type Definitions */ - SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition', - OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition', - FIELD_DEFINITION = 'FieldDefinition', - INPUT_VALUE_DEFINITION = 'InputValueDefinition', - INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition', - UNION_TYPE_DEFINITION = 'UnionTypeDefinition', - ENUM_TYPE_DEFINITION = 'EnumTypeDefinition', - ENUM_VALUE_DEFINITION = 'EnumValueDefinition', - INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition', + SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition', + OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition', + FIELD_DEFINITION: 'FieldDefinition', + INPUT_VALUE_DEFINITION: 'InputValueDefinition', + INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition', + UNION_TYPE_DEFINITION: 'UnionTypeDefinition', + ENUM_TYPE_DEFINITION: 'EnumTypeDefinition', + ENUM_VALUE_DEFINITION: 'EnumValueDefinition', + INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition', /** Directive Definitions */ - DIRECTIVE_DEFINITION = 'DirectiveDefinition', + DIRECTIVE_DEFINITION: 'DirectiveDefinition', /** Type System Extensions */ - SCHEMA_EXTENSION = 'SchemaExtension', + SCHEMA_EXTENSION: 'SchemaExtension', /** Type Extensions */ - SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension', - OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension', - INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension', - UNION_TYPE_EXTENSION = 'UnionTypeExtension', - ENUM_TYPE_EXTENSION = 'EnumTypeExtension', - INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension', -} + SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension', + OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension', + INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension', + UNION_TYPE_EXTENSION: 'UnionTypeExtension', + ENUM_TYPE_EXTENSION: 'EnumTypeExtension', + INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type Kind = ValueTypes; diff --git a/src/language/tokenKind.ts b/src/language/tokenKind.ts index 1bba933d50..cf0efbd020 100644 --- a/src/language/tokenKind.ts +++ b/src/language/tokenKind.ts @@ -1,29 +1,34 @@ +import type { ValueTypes } from '../jsutils/ValueTypes'; + /** * An exported enum describing the different kinds of tokens that the * lexer emits. */ -export enum TokenKind { - SOF = '', - EOF = '', - BANG = '!', - QUESTION_MARK = '?', - DOLLAR = '$', - AMP = '&', - PAREN_L = '(', - PAREN_R = ')', - SPREAD = '...', - COLON = ':', - EQUALS = '=', - AT = '@', - BRACKET_L = '[', - BRACKET_R = ']', - BRACE_L = '{', - PIPE = '|', - BRACE_R = '}', - NAME = 'Name', - INT = 'Int', - FLOAT = 'Float', - STRING = 'String', - BLOCK_STRING = 'BlockString', - COMMENT = 'Comment', -} +export const TokenKind = Object.freeze({ + SOF: '', + EOF: '', + BANG: '!', + QUESTION_MARK: '?', + DOLLAR: '$', + AMP: '&', + PAREN_L: '(', + PAREN_R: ')', + SPREAD: '...', + COLON: ':', + EQUALS: '=', + AT: '@', + BRACKET_L: '[', + BRACKET_R: ']', + BRACE_L: '{', + PIPE: '|', + BRACE_R: '}', + NAME: 'Name', + INT: 'Int', + FLOAT: 'Float', + STRING: 'String', + BLOCK_STRING: 'BlockString', + COMMENT: 'Comment', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type TokenKind = ValueTypes; diff --git a/src/type/introspection.ts b/src/type/introspection.ts index e5fce6f241..c2b76fdb58 100644 --- a/src/type/introspection.ts +++ b/src/type/introspection.ts @@ -1,5 +1,6 @@ import { inspect } from '../jsutils/inspect'; import { invariant } from '../jsutils/invariant'; +import type { ValueTypes } from '../jsutils/ValueTypes'; import { DirectiveLocation } from '../language/directiveLocation'; import { print } from '../language/printer'; @@ -435,16 +436,19 @@ export const __EnumValue: GraphQLObjectType = new GraphQLObjectType({ } as GraphQLFieldConfigMap), }); -export enum TypeKind { - SCALAR = 'SCALAR', - OBJECT = 'OBJECT', - INTERFACE = 'INTERFACE', - UNION = 'UNION', - ENUM = 'ENUM', - INPUT_OBJECT = 'INPUT_OBJECT', - LIST = 'LIST', - NON_NULL = 'NON_NULL', -} +export const TypeKind = Object.freeze({ + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + INPUT_OBJECT: 'INPUT_OBJECT', + LIST: 'LIST', + NON_NULL: 'NON_NULL', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type TypeKind = ValueTypes; export const __TypeKind: GraphQLEnumType = new GraphQLEnumType({ name: '__TypeKind', diff --git a/src/utilities/buildClientSchema.ts b/src/utilities/buildClientSchema.ts index 8418c6ce51..04926cfeda 100644 --- a/src/utilities/buildClientSchema.ts +++ b/src/utilities/buildClientSchema.ts @@ -175,8 +175,6 @@ export function buildClientSchema( function buildType(type: IntrospectionType): GraphQLNamedType { // eslint-disable-next-line @typescript-eslint/prefer-optional-chain if (type != null && type.name != null && type.kind != null) { - // FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17 - // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check switch (type.kind) { case TypeKind.SCALAR: return buildScalarDef(type); diff --git a/src/utilities/findBreakingChanges.ts b/src/utilities/findBreakingChanges.ts index 0bf0d453b4..f430f873d2 100644 --- a/src/utilities/findBreakingChanges.ts +++ b/src/utilities/findBreakingChanges.ts @@ -1,6 +1,7 @@ import { inspect } from '../jsutils/inspect'; import { invariant } from '../jsutils/invariant'; import { keyMap } from '../jsutils/keyMap'; +import type { ValueTypes } from '../jsutils/ValueTypes'; import { print } from '../language/printer'; @@ -34,33 +35,39 @@ import type { GraphQLSchema } from '../type/schema'; import { astFromValue } from './astFromValue'; import { sortValueNode } from './sortValueNode'; -export enum BreakingChangeType { - TYPE_REMOVED = 'TYPE_REMOVED', - TYPE_CHANGED_KIND = 'TYPE_CHANGED_KIND', - TYPE_REMOVED_FROM_UNION = 'TYPE_REMOVED_FROM_UNION', - VALUE_REMOVED_FROM_ENUM = 'VALUE_REMOVED_FROM_ENUM', - REQUIRED_INPUT_FIELD_ADDED = 'REQUIRED_INPUT_FIELD_ADDED', - IMPLEMENTED_INTERFACE_REMOVED = 'IMPLEMENTED_INTERFACE_REMOVED', - FIELD_REMOVED = 'FIELD_REMOVED', - FIELD_CHANGED_KIND = 'FIELD_CHANGED_KIND', - REQUIRED_ARG_ADDED = 'REQUIRED_ARG_ADDED', - ARG_REMOVED = 'ARG_REMOVED', - ARG_CHANGED_KIND = 'ARG_CHANGED_KIND', - DIRECTIVE_REMOVED = 'DIRECTIVE_REMOVED', - DIRECTIVE_ARG_REMOVED = 'DIRECTIVE_ARG_REMOVED', - REQUIRED_DIRECTIVE_ARG_ADDED = 'REQUIRED_DIRECTIVE_ARG_ADDED', - DIRECTIVE_REPEATABLE_REMOVED = 'DIRECTIVE_REPEATABLE_REMOVED', - DIRECTIVE_LOCATION_REMOVED = 'DIRECTIVE_LOCATION_REMOVED', -} - -export enum DangerousChangeType { - VALUE_ADDED_TO_ENUM = 'VALUE_ADDED_TO_ENUM', - TYPE_ADDED_TO_UNION = 'TYPE_ADDED_TO_UNION', - OPTIONAL_INPUT_FIELD_ADDED = 'OPTIONAL_INPUT_FIELD_ADDED', - OPTIONAL_ARG_ADDED = 'OPTIONAL_ARG_ADDED', - IMPLEMENTED_INTERFACE_ADDED = 'IMPLEMENTED_INTERFACE_ADDED', - ARG_DEFAULT_VALUE_CHANGE = 'ARG_DEFAULT_VALUE_CHANGE', -} +export const BreakingChangeType = Object.freeze({ + TYPE_REMOVED: 'TYPE_REMOVED', + TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND', + TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION', + VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM', + REQUIRED_INPUT_FIELD_ADDED: 'REQUIRED_INPUT_FIELD_ADDED', + IMPLEMENTED_INTERFACE_REMOVED: 'IMPLEMENTED_INTERFACE_REMOVED', + FIELD_REMOVED: 'FIELD_REMOVED', + FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND', + REQUIRED_ARG_ADDED: 'REQUIRED_ARG_ADDED', + ARG_REMOVED: 'ARG_REMOVED', + ARG_CHANGED_KIND: 'ARG_CHANGED_KIND', + DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED', + DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED', + REQUIRED_DIRECTIVE_ARG_ADDED: 'REQUIRED_DIRECTIVE_ARG_ADDED', + DIRECTIVE_REPEATABLE_REMOVED: 'DIRECTIVE_REPEATABLE_REMOVED', + DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type BreakingChangeType = ValueTypes; + +export const DangerousChangeType = Object.freeze({ + VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM', + TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION', + OPTIONAL_INPUT_FIELD_ADDED: 'OPTIONAL_INPUT_FIELD_ADDED', + OPTIONAL_ARG_ADDED: 'OPTIONAL_ARG_ADDED', + IMPLEMENTED_INTERFACE_ADDED: 'IMPLEMENTED_INTERFACE_ADDED', + ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE', +} as const); + +// eslint-disable-next-line @typescript-eslint/no-redeclare +export type DangerousChangeType = ValueTypes; export interface BreakingChange { type: BreakingChangeType;