Skip to content

Commit

Permalink
fix: args builder now coppies prototype methods from field builder so…
Browse files Browse the repository at this point in the history
… that extending FieldBuilder class works as expected
  • Loading branch information
Michael Hayes authored and hayes committed Apr 12, 2021
1 parent 65b8942 commit bc8fd04
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
16 changes: 12 additions & 4 deletions packages/core/src/fieldUtils/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ export default class InputFieldBuilder<
argBuilder(): ArgBuilder<Types> {
const builder: InputFieldBuilder<Types, 'Arg'>['field'] = this.field.bind(this);

([...Object.keys(this)] as (keyof InputFieldBuilder<Types, 'Arg'>)[]).forEach((key) => {
((builder as unknown) as { [s: string]: unknown })[key] =
typeof this[key] === 'function' ? (this[key] as Function).bind(this) : this[key];
});
const protoKeys = Object.keys(Object.getPrototypeOf(this)).filter(
(key) =>
typeof (this as Record<string, unknown>)[key] === 'function' &&
((Function.prototype as unknown) as Record<string, unknown>)[key] === undefined,
);

([...Object.keys(this), ...protoKeys] as (keyof InputFieldBuilder<Types, 'Arg'>)[]).forEach(
(key) => {
((builder as unknown) as { [s: string]: unknown })[key] =
typeof this[key] === 'function' ? (this[key] as Function).bind(this) : this[key];
},
);

return builder as ArgBuilder<Types>;
}
Expand Down
54 changes: 32 additions & 22 deletions packages/core/src/types/builder-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
FieldRef,
InputFieldRef,
inputFieldShapeKey,
InputRef,
InterfaceParam,
InterfaceRef,
MaybePromise,
Expand All @@ -31,13 +32,14 @@ export type Resolver<Parent, Args, Context, Type, Return = unknown> = (
context: Context,
info: GraphQLResolveInfo,
) => MaybePromiseWithInference<
Type extends unknown[]
? Readonly<
Return extends MaybePromise<readonly Promise<unknown>[]> ? Promise<Type[number]>[] : Type
>
: Type,
Return
> & Return;
Type extends unknown[]
? Readonly<
Return extends MaybePromise<readonly Promise<unknown>[]> ? Promise<Type[number]>[] : Type
>
: Type,
Return
> &
Return;

export type Subscriber<Parent, Args, Context, Shape> = (
parent: Parent,
Expand Down Expand Up @@ -137,13 +139,14 @@ export type ObjectTypeOptions<
Shape,
Interfaces extends InterfaceParam<Types>[]
> = (Param extends string
? {}
: Param extends ObjectRef<unknown>
? { name?: string }
: { name: string }) & (
| GiraphQLSchemaTypes.ObjectTypeOptions<Types, Shape>
| GiraphQLSchemaTypes.ObjectTypeWithInterfaceOptions<Types, Shape, Interfaces>
);
? {}
: Param extends ObjectRef<unknown>
? { name?: string }
: { name: string }) &
(
| GiraphQLSchemaTypes.ObjectTypeOptions<Types, Shape>
| GiraphQLSchemaTypes.ObjectTypeWithInterfaceOptions<Types, Shape, Interfaces>
);

export type InterfaceTypeOptions<
Types extends SchemaTypes,
Expand Down Expand Up @@ -202,14 +205,21 @@ export type InputShapeFromField<Field extends InputFieldRef> = Field extends {
: never;

export type FieldKind = keyof GiraphQLSchemaTypes.FieldOptionsByKind<
SchemaTypes,
{},
TypeParam<SchemaTypes>,
boolean,
{},
{},
{}
> & keyof GiraphQLSchemaTypes.GiraphQLKindToGraphQLType;
SchemaTypes,
{},
TypeParam<SchemaTypes>,
boolean,
{},
{},
{}
> &
keyof GiraphQLSchemaTypes.GiraphQLKindToGraphQLType;

export type InputFieldKind = keyof GiraphQLSchemaTypes.InputFieldOptionsByKind<
SchemaTypes,
InputRef<unknown>,
boolean
>;

export type CompatibleTypes<
Types extends SchemaTypes,
Expand Down

0 comments on commit bc8fd04

Please sign in to comment.