From 9488e136bfc701cf216f00cd5d2b024f4488a906 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Mon, 9 Sep 2024 14:11:13 +0300 Subject: [PATCH] fix(IntrospectionType): properly type using TypeKind enum --- src/utilities/buildClientSchema.ts | 38 ++++++++++++-------------- src/utilities/getIntrospectionQuery.ts | 18 ++++++------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/utilities/buildClientSchema.ts b/src/utilities/buildClientSchema.ts index 16d7ecfe18..9e2005de93 100644 --- a/src/utilities/buildClientSchema.ts +++ b/src/utilities/buildClientSchema.ts @@ -178,28 +178,26 @@ export function buildClientSchema( // Given a type's introspection result, construct the correct // GraphQLType instance. 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); - case TypeKind.OBJECT: - return buildObjectDef(type); - case TypeKind.INTERFACE: - return buildInterfaceDef(type); - case TypeKind.UNION: - return buildUnionDef(type); - case TypeKind.ENUM: - return buildEnumDef(type); - case TypeKind.INPUT_OBJECT: - return buildInputObjectDef(type); - } + switch (type.kind) { + case TypeKind.SCALAR: + return buildScalarDef(type); + case TypeKind.OBJECT: + return buildObjectDef(type); + case TypeKind.INTERFACE: + return buildInterfaceDef(type); + case TypeKind.UNION: + return buildUnionDef(type); + case TypeKind.ENUM: + return buildEnumDef(type); + case TypeKind.INPUT_OBJECT: + return buildInputObjectDef(type); } - const typeStr = inspect(type); + // Unreachable. + // @ts-expect-error throw new Error( - `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${typeStr}.`, + `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${inspect( + type, + )}.`, ); } diff --git a/src/utilities/getIntrospectionQuery.ts b/src/utilities/getIntrospectionQuery.ts index fb6c65b4fe..9fc8963fa0 100644 --- a/src/utilities/getIntrospectionQuery.ts +++ b/src/utilities/getIntrospectionQuery.ts @@ -2,6 +2,8 @@ import type { Maybe } from '../jsutils/Maybe.js'; import type { DirectiveLocation } from '../language/directiveLocation.js'; +import type { TypeKind } from '../type/introspection.js'; + export interface IntrospectionOptions { /** * Whether to include descriptions in the introspection result. @@ -218,14 +220,14 @@ export type IntrospectionInputType = | IntrospectionInputObjectType; export interface IntrospectionScalarType { - readonly kind: 'SCALAR'; + readonly kind: TypeKind.SCALAR; readonly name: string; readonly description?: Maybe; readonly specifiedByURL?: Maybe; } export interface IntrospectionObjectType { - readonly kind: 'OBJECT'; + readonly kind: TypeKind.OBJECT; readonly name: string; readonly description?: Maybe; readonly fields: ReadonlyArray; @@ -235,7 +237,7 @@ export interface IntrospectionObjectType { } export interface IntrospectionInterfaceType { - readonly kind: 'INTERFACE'; + readonly kind: TypeKind.INTERFACE; readonly name: string; readonly description?: Maybe; readonly fields: ReadonlyArray; @@ -248,7 +250,7 @@ export interface IntrospectionInterfaceType { } export interface IntrospectionUnionType { - readonly kind: 'UNION'; + readonly kind: TypeKind.UNION; readonly name: string; readonly description?: Maybe; readonly possibleTypes: ReadonlyArray< @@ -257,14 +259,14 @@ export interface IntrospectionUnionType { } export interface IntrospectionEnumType { - readonly kind: 'ENUM'; + readonly kind: TypeKind.ENUM; readonly name: string; readonly description?: Maybe; readonly enumValues: ReadonlyArray; } export interface IntrospectionInputObjectType { - readonly kind: 'INPUT_OBJECT'; + readonly kind: TypeKind.INPUT_OBJECT; readonly name: string; readonly description?: Maybe; readonly inputFields: ReadonlyArray; @@ -274,14 +276,14 @@ export interface IntrospectionInputObjectType { export interface IntrospectionListTypeRef< T extends IntrospectionTypeRef = IntrospectionTypeRef, > { - readonly kind: 'LIST'; + readonly kind: TypeKind.LIST; readonly ofType: T; } export interface IntrospectionNonNullTypeRef< T extends IntrospectionTypeRef = IntrospectionTypeRef, > { - readonly kind: 'NON_NULL'; + readonly kind: TypeKind.NON_NULL; readonly ofType: T; }