Skip to content

Commit

Permalink
feat: unwrap Promise fields for expose, fixes hayes#793
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Feb 20, 2023
1 parent fe8ef17 commit 16742d8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/converter/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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'); },
Expand Down Expand Up @@ -393,6 +397,7 @@ input Example2 {
type Giraffe implements Animal {
age: Int!
favoriteFood: String!
name: String!
species: String
}
Expand Down
5 changes: 5 additions & 0 deletions packages/converter/tests/examples/random-stuff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Types>({});
Expand All @@ -58,6 +62,7 @@ builder.objectType(Giraffe, {
fields: (t) => ({
name: t.exposeString('name', {}),
age: t.exposeInt('age', {}),
favoriteFood: t.exposeString('favoriteFood'),
}),
});

Expand Down
16 changes: 10 additions & 6 deletions packages/core/src/types/builder-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ export type CompatibleTypes<
Type extends TypeParam<Types>,
Nullable extends FieldNullability<Type>,
> = {
[K in keyof ParentShape]-?: ParentShape[K] extends ShapeFromTypeParam<Types, Type, Nullable>
[K in keyof ParentShape]-?: Awaited<ParentShape[K]> extends ShapeFromTypeParam<
Types,
Type,
Nullable
>
? K
: never;
}[keyof ParentShape] &
Expand All @@ -249,7 +253,7 @@ export type ExposeNullability<
ParentShape,
Name extends keyof ParentShape,
Nullable extends FieldNullability<Type>,
> = ParentShape[Name] extends ShapeFromTypeParam<Types, Type, Nullable>
> = Awaited<ParentShape[Name]> extends ShapeFromTypeParam<Types, Type, Nullable>
? {
nullable?: Nullable & ExposeNullableOption<Types, Type, ParentShape, Name>;
}
Expand All @@ -264,15 +268,15 @@ export type ExposeNullableOption<
Name extends keyof ParentShape,
> = FieldNullability<Type> &
(Type extends [unknown]
? ParentShape[Name] extends readonly (infer T)[] | null | undefined
? Awaited<ParentShape[Name]> extends readonly (infer T)[] | null | undefined
? [T] extends [NonNullable<T>]
? ParentShape[Name] extends NonNullable<ParentShape[Name]>
? Awaited<ParentShape[Name]> extends NonNullable<Awaited<ParentShape[Name]>>
? boolean | { items: boolean; list: boolean }
: true | { items: boolean; list: true }
: ParentShape[Name] extends NonNullable<ParentShape[Name]>
: Awaited<ParentShape[Name]> extends NonNullable<Awaited<ParentShape[Name]>>
? { items: true; list: boolean }
: { items: true; list: true }
: never
: ParentShape[Name] extends NonNullable<ParentShape[Name]>
: Awaited<ParentShape[Name]> extends NonNullable<Awaited<ParentShape[Name]>>
? boolean
: true);
12 changes: 6 additions & 6 deletions packages/deno/packages/core/types/builder-options.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16742d8

Please sign in to comment.