From 16742d85cc3bc18af6e0ce9f11be365404ebd1a6 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Mon, 20 Feb 2023 17:14:21 -0500 Subject: [PATCH] feat: unwrap Promise fields for expose, fixes #793 --- .../tests/__snapshots__/index.test.ts.snap | 5 +++++ .../converter/tests/examples/random-stuff.ts | 5 +++++ packages/core/src/types/builder-options.ts | 16 ++++++++++------ .../deno/packages/core/types/builder-options.ts | 12 ++++++------ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/converter/tests/__snapshots__/index.test.ts.snap b/packages/converter/tests/__snapshots__/index.test.ts.snap index d3967731a..ce8179ed2 100644 --- a/packages/converter/tests/__snapshots__/index.test.ts.snap +++ b/packages/converter/tests/__snapshots__/index.test.ts.snap @@ -180,6 +180,10 @@ builder.objectType('Giraffe', { type: 'Int', resolve: (parent, args, context, info) => { throw new Error('Not implemented'); }, }), + favoriteFood: t.field({ + type: 'String', + resolve: (parent, args, context, info) => { throw new Error('Not implemented'); }, + }), name: t.field({ type: 'String', resolve: (parent, args, context, info) => { throw new Error('Not implemented'); }, @@ -393,6 +397,7 @@ input Example2 { type Giraffe implements Animal { age: Int! + favoriteFood: String! name: String! species: String } diff --git a/packages/converter/tests/examples/random-stuff.ts b/packages/converter/tests/examples/random-stuff.ts index 63641c58b..a8121f65a 100644 --- a/packages/converter/tests/examples/random-stuff.ts +++ b/packages/converter/tests/examples/random-stuff.ts @@ -34,6 +34,10 @@ class Giraffe extends Animal { this.name = name; this.age = age; } + + get favoriteFood() { + return Promise.resolve('acacia trees'); + } } const builder = new SchemaBuilder({}); @@ -58,6 +62,7 @@ builder.objectType(Giraffe, { fields: (t) => ({ name: t.exposeString('name', {}), age: t.exposeInt('age', {}), + favoriteFood: t.exposeString('favoriteFood'), }), }); diff --git a/packages/core/src/types/builder-options.ts b/packages/core/src/types/builder-options.ts index 083ebe378..27b154614 100644 --- a/packages/core/src/types/builder-options.ts +++ b/packages/core/src/types/builder-options.ts @@ -237,7 +237,11 @@ export type CompatibleTypes< Type extends TypeParam, Nullable extends FieldNullability, > = { - [K in keyof ParentShape]-?: ParentShape[K] extends ShapeFromTypeParam + [K in keyof ParentShape]-?: Awaited extends ShapeFromTypeParam< + Types, + Type, + Nullable + > ? K : never; }[keyof ParentShape] & @@ -249,7 +253,7 @@ export type ExposeNullability< ParentShape, Name extends keyof ParentShape, Nullable extends FieldNullability, -> = ParentShape[Name] extends ShapeFromTypeParam +> = Awaited extends ShapeFromTypeParam ? { nullable?: Nullable & ExposeNullableOption; } @@ -264,15 +268,15 @@ export type ExposeNullableOption< Name extends keyof ParentShape, > = FieldNullability & (Type extends [unknown] - ? ParentShape[Name] extends readonly (infer T)[] | null | undefined + ? Awaited extends readonly (infer T)[] | null | undefined ? [T] extends [NonNullable] - ? ParentShape[Name] extends NonNullable + ? Awaited extends NonNullable> ? boolean | { items: boolean; list: boolean } : true | { items: boolean; list: true } - : ParentShape[Name] extends NonNullable + : Awaited extends NonNullable> ? { items: true; list: boolean } : { items: true; list: true } : never - : ParentShape[Name] extends NonNullable + : Awaited extends NonNullable> ? boolean : true); diff --git a/packages/deno/packages/core/types/builder-options.ts b/packages/deno/packages/core/types/builder-options.ts index 886190cc6..ea9bf9765 100644 --- a/packages/deno/packages/core/types/builder-options.ts +++ b/packages/deno/packages/core/types/builder-options.ts @@ -61,29 +61,29 @@ export type InputShapeFromField = Field extends { export type FieldKind = keyof PothosSchemaTypes.FieldOptionsByKind, boolean, {}, {}, {}> & keyof PothosSchemaTypes.PothosKindToGraphQLType; export type InputFieldKind = keyof PothosSchemaTypes.InputFieldOptionsByKind, boolean>; export type CompatibleTypes, Nullable extends FieldNullability> = { - [K in keyof ParentShape]-?: ParentShape[K] extends ShapeFromTypeParam ? K : never; + [K in keyof ParentShape]-?: Awaited extends ShapeFromTypeParam ? K : never; }[keyof ParentShape] & string; -export type ExposeNullability, ParentShape, Name extends keyof ParentShape, Nullable extends FieldNullability> = ParentShape[Name] extends ShapeFromTypeParam ? { +export type ExposeNullability, ParentShape, Name extends keyof ParentShape, Nullable extends FieldNullability> = Awaited extends ShapeFromTypeParam ? { nullable?: Nullable & ExposeNullableOption; } : { nullable: Nullable & ExposeNullableOption; }; export type ExposeNullableOption, ParentShape, Name extends keyof ParentShape> = FieldNullability & (Type extends [ unknown -] ? ParentShape[Name] extends readonly (infer T)[] | null | undefined ? [ +] ? Awaited extends readonly (infer T)[] | null | undefined ? [ T ] extends [ NonNullable -] ? ParentShape[Name] extends NonNullable ? boolean | { +] ? Awaited extends NonNullable> ? boolean | { items: boolean; list: boolean; } : true | { items: boolean; list: true; -} : ParentShape[Name] extends NonNullable ? { +} : Awaited extends NonNullable> ? { items: true; list: boolean; } : { items: true; list: true; -} : never : ParentShape[Name] extends NonNullable ? boolean : true); +} : never : Awaited extends NonNullable> ? boolean : true);