Skip to content
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

fix: Prevent introspection mapping on schemas with huge root types from failing #25

Merged
merged 5 commits into from
Jan 18, 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
5 changes: 5 additions & 0 deletions .changeset/dry-actors-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gql.tada': patch
---

Prevent type inference for schemas with “huge” root types (i.e. types with an excessive amount of fields) from failing introspection mapping.
32 changes: 22 additions & 10 deletions src/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ interface DefaultScalars {
Int: number;
}

type mapNames<T extends readonly any[]> = obj<{
[P in T[number]['name']]: T[number] extends infer Value
? Value extends { readonly name: P }
? obj<Value>
: never
: never;
}>;

type mapScalar<
Type extends IntrospectionScalarType,
Scalars extends ScalarsLike = DefaultScalars,
Expand All @@ -136,11 +128,25 @@ type mapEnum<T extends IntrospectionEnumType> = {
type: T['enumValues'][number]['name'];
};

type mapField<T> = T extends IntrospectionField
? {
name: T['name'];
type: T['type'];
args: any;
}
: never;

export type mapObject<T extends IntrospectionObjectType> = {
kind: 'OBJECT';
name: T['name'];
interfaces: T['interfaces'][number]['name'];
fields: obj<mapNames<T['fields']>>;
fields: obj<{
[P in T['fields'][number]['name']]: T['fields'][number] extends infer Field
? Field extends { readonly name: P }
? mapField<Field>
: never
: never;
}>;
};

export type mapInputObject<T extends IntrospectionInputObjectType> = {
Expand All @@ -154,7 +160,13 @@ type mapInterface<T extends IntrospectionInterfaceType> = {
name: T['name'];
interfaces: T['interfaces'] extends readonly any[] ? T['interfaces'][number]['name'] : never;
possibleTypes: T['possibleTypes'][number]['name'];
fields: obj<mapNames<T['fields']>>;
fields: obj<{
[P in T['fields'][number]['name']]: T['fields'][number] extends infer Field
? Field extends { readonly name: P }
? mapField<Field>
: never
: never;
}>;
};

type mapUnion<T extends IntrospectionUnionType> = {
Expand Down
3 changes: 1 addition & 2 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
} from './namespace';

import type {
IntrospectionField,
IntrospectionListTypeRef,
IntrospectionNamedTypeRef,
IntrospectionNonNullTypeRef,
Expand All @@ -27,7 +26,7 @@ import type {
type ObjectLikeType = {
kind: 'OBJECT' | 'INTERFACE' | 'UNION';
name: string;
fields: { [key: string]: IntrospectionField };
fields: { [key: string]: any };
};

type _unwrapTypeRec<
Expand Down
Loading