Skip to content

fix(IntrospectionType): properly type using TypeKind enum #4185

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

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/utilities/buildClientSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)}.`,
);
}

Expand Down
18 changes: 10 additions & 8 deletions src/utilities/getIntrospectionQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -218,14 +220,14 @@ export type IntrospectionInputType =
| IntrospectionInputObjectType;

export interface IntrospectionScalarType {
readonly kind: 'SCALAR';
readonly kind: TypeKind.SCALAR;
readonly name: string;
readonly description?: Maybe<string>;
readonly specifiedByURL?: Maybe<string>;
}

export interface IntrospectionObjectType {
readonly kind: 'OBJECT';
readonly kind: TypeKind.OBJECT;
readonly name: string;
readonly description?: Maybe<string>;
readonly fields: ReadonlyArray<IntrospectionField>;
Expand All @@ -235,7 +237,7 @@ export interface IntrospectionObjectType {
}

export interface IntrospectionInterfaceType {
readonly kind: 'INTERFACE';
readonly kind: TypeKind.INTERFACE;
readonly name: string;
readonly description?: Maybe<string>;
readonly fields: ReadonlyArray<IntrospectionField>;
Expand All @@ -248,7 +250,7 @@ export interface IntrospectionInterfaceType {
}

export interface IntrospectionUnionType {
readonly kind: 'UNION';
readonly kind: TypeKind.UNION;
readonly name: string;
readonly description?: Maybe<string>;
readonly possibleTypes: ReadonlyArray<
Expand All @@ -257,14 +259,14 @@ export interface IntrospectionUnionType {
}

export interface IntrospectionEnumType {
readonly kind: 'ENUM';
readonly kind: TypeKind.ENUM;
readonly name: string;
readonly description?: Maybe<string>;
readonly enumValues: ReadonlyArray<IntrospectionEnumValue>;
}

export interface IntrospectionInputObjectType {
readonly kind: 'INPUT_OBJECT';
readonly kind: TypeKind.INPUT_OBJECT;
readonly name: string;
readonly description?: Maybe<string>;
readonly inputFields: ReadonlyArray<IntrospectionInputValue>;
Expand All @@ -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;
}

Expand Down
Loading