Skip to content

Commit 2166bc8

Browse files
authored
fix(IntrospectionType): properly type using TypeKind enum (#4185)
1 parent c7d1cf4 commit 2166bc8

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/utilities/buildClientSchema.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,26 @@ export function buildClientSchema(
178178
// Given a type's introspection result, construct the correct
179179
// GraphQLType instance.
180180
function buildType(type: IntrospectionType): GraphQLNamedType {
181-
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
182-
if (type != null && type.name != null && type.kind != null) {
183-
// FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17
184-
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
185-
switch (type.kind) {
186-
case TypeKind.SCALAR:
187-
return buildScalarDef(type);
188-
case TypeKind.OBJECT:
189-
return buildObjectDef(type);
190-
case TypeKind.INTERFACE:
191-
return buildInterfaceDef(type);
192-
case TypeKind.UNION:
193-
return buildUnionDef(type);
194-
case TypeKind.ENUM:
195-
return buildEnumDef(type);
196-
case TypeKind.INPUT_OBJECT:
197-
return buildInputObjectDef(type);
198-
}
181+
switch (type.kind) {
182+
case TypeKind.SCALAR:
183+
return buildScalarDef(type);
184+
case TypeKind.OBJECT:
185+
return buildObjectDef(type);
186+
case TypeKind.INTERFACE:
187+
return buildInterfaceDef(type);
188+
case TypeKind.UNION:
189+
return buildUnionDef(type);
190+
case TypeKind.ENUM:
191+
return buildEnumDef(type);
192+
case TypeKind.INPUT_OBJECT:
193+
return buildInputObjectDef(type);
199194
}
200-
const typeStr = inspect(type);
195+
// Unreachable.
196+
// @ts-expect-error
201197
throw new Error(
202-
`Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${typeStr}.`,
198+
`Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${inspect(
199+
type,
200+
)}.`,
203201
);
204202
}
205203

src/utilities/getIntrospectionQuery.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { Maybe } from '../jsutils/Maybe.js';
22

33
import type { DirectiveLocation } from '../language/directiveLocation.js';
44

5+
import type { TypeKind } from '../type/introspection.js';
6+
57
export interface IntrospectionOptions {
68
/**
79
* Whether to include descriptions in the introspection result.
@@ -218,14 +220,14 @@ export type IntrospectionInputType =
218220
| IntrospectionInputObjectType;
219221

220222
export interface IntrospectionScalarType {
221-
readonly kind: 'SCALAR';
223+
readonly kind: TypeKind.SCALAR;
222224
readonly name: string;
223225
readonly description?: Maybe<string>;
224226
readonly specifiedByURL?: Maybe<string>;
225227
}
226228

227229
export interface IntrospectionObjectType {
228-
readonly kind: 'OBJECT';
230+
readonly kind: TypeKind.OBJECT;
229231
readonly name: string;
230232
readonly description?: Maybe<string>;
231233
readonly fields: ReadonlyArray<IntrospectionField>;
@@ -235,7 +237,7 @@ export interface IntrospectionObjectType {
235237
}
236238

237239
export interface IntrospectionInterfaceType {
238-
readonly kind: 'INTERFACE';
240+
readonly kind: TypeKind.INTERFACE;
239241
readonly name: string;
240242
readonly description?: Maybe<string>;
241243
readonly fields: ReadonlyArray<IntrospectionField>;
@@ -248,7 +250,7 @@ export interface IntrospectionInterfaceType {
248250
}
249251

250252
export interface IntrospectionUnionType {
251-
readonly kind: 'UNION';
253+
readonly kind: TypeKind.UNION;
252254
readonly name: string;
253255
readonly description?: Maybe<string>;
254256
readonly possibleTypes: ReadonlyArray<
@@ -257,14 +259,14 @@ export interface IntrospectionUnionType {
257259
}
258260

259261
export interface IntrospectionEnumType {
260-
readonly kind: 'ENUM';
262+
readonly kind: TypeKind.ENUM;
261263
readonly name: string;
262264
readonly description?: Maybe<string>;
263265
readonly enumValues: ReadonlyArray<IntrospectionEnumValue>;
264266
}
265267

266268
export interface IntrospectionInputObjectType {
267-
readonly kind: 'INPUT_OBJECT';
269+
readonly kind: TypeKind.INPUT_OBJECT;
268270
readonly name: string;
269271
readonly description?: Maybe<string>;
270272
readonly inputFields: ReadonlyArray<IntrospectionInputValue>;
@@ -274,14 +276,14 @@ export interface IntrospectionInputObjectType {
274276
export interface IntrospectionListTypeRef<
275277
T extends IntrospectionTypeRef = IntrospectionTypeRef,
276278
> {
277-
readonly kind: 'LIST';
279+
readonly kind: TypeKind.LIST;
278280
readonly ofType: T;
279281
}
280282

281283
export interface IntrospectionNonNullTypeRef<
282284
T extends IntrospectionTypeRef = IntrospectionTypeRef,
283285
> {
284-
readonly kind: 'NON_NULL';
286+
readonly kind: TypeKind.NON_NULL;
285287
readonly ofType: T;
286288
}
287289

0 commit comments

Comments
 (0)