Skip to content

Commit 2e2a6d7

Browse files
ryymyaacovCR
authored andcommitted
fix type for first argument to isTypeOf (#3385)
Previously, the type of `source` argument in `isTypeOf` was `TSource` (the source type of `GraphQLObjectType`), which is incorrect, since `isTypeOf` may be called with a source value of any type that implements the same interface or that belongs to the same union. This PR fixes this by introducing a new `TAbstract` generic parameter for `GraphQLObjectType/Config`, defaulted to `unknown`.
1 parent 928a321 commit 2e2a6d7

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/execution/__tests__/abstract-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('Execute: Handles execution of abstract types', () => {
158158
const DogType = new GraphQLObjectType<Dog, Context>({
159159
name: 'Dog',
160160
interfaces: [PetType],
161-
isTypeOf(_source, context) {
161+
isTypeOf(_value, context) {
162162
const error = new Error('We are testing this error');
163163
if (context.async) {
164164
return Promise.reject(error);
@@ -240,7 +240,7 @@ describe('Execute: Handles execution of abstract types', () => {
240240
const DogType = new GraphQLObjectType<Dog, Context>({
241241
name: 'Dog',
242242
interfaces: [PetType],
243-
isTypeOf(_source, context) {
243+
isTypeOf(_value, context) {
244244
return context.async ? Promise.resolve(false) : false;
245245
},
246246
fields: {

src/type/definition.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -896,21 +896,23 @@ export interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
896896
* });
897897
* ```
898898
*/
899-
export class GraphQLObjectType<TSource = any, TContext = any>
899+
export class GraphQLObjectType<TSource = any, TContext = any, TAbstract = any>
900900
implements GraphQLSchemaElement
901901
{
902902
readonly __kind = objectSymbol;
903903
name: string;
904904
description: Maybe<string>;
905-
isTypeOf: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
905+
isTypeOf: Maybe<GraphQLIsTypeOfFn<TAbstract, TContext>>;
906906
extensions: Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>;
907907
astNode: Maybe<ObjectTypeDefinitionNode>;
908908
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
909909

910910
private _fields: ThunkObjMap<GraphQLField<TSource, TContext>>;
911911
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;
912912

913-
constructor(config: Readonly<GraphQLObjectTypeConfig<TSource, TContext>>) {
913+
constructor(
914+
config: Readonly<GraphQLObjectTypeConfig<TSource, TContext, TAbstract>>,
915+
) {
914916
this.__kind = objectSymbol;
915917
this.name = assertName(config.name);
916918
this.description = config.description;
@@ -944,7 +946,7 @@ export class GraphQLObjectType<TSource = any, TContext = any>
944946
return this._interfaces;
945947
}
946948

947-
toConfig(): GraphQLObjectTypeNormalizedConfig<TSource, TContext> {
949+
toConfig(): GraphQLObjectTypeNormalizedConfig<TSource, TContext, TAbstract> {
948950
return {
949951
name: this.name,
950952
description: this.description,
@@ -987,19 +989,26 @@ function defineFieldMap<TSource, TContext>(
987989
);
988990
}
989991

990-
export interface GraphQLObjectTypeConfig<TSource, TContext> {
992+
export interface GraphQLObjectTypeConfig<
993+
TSource,
994+
TContext,
995+
TAbstract = unknown,
996+
> {
991997
name: string;
992998
description?: Maybe<string>;
993999
interfaces?: ThunkReadonlyArray<GraphQLInterfaceType> | undefined;
9941000
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
995-
isTypeOf?: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
1001+
isTypeOf?: Maybe<GraphQLIsTypeOfFn<TAbstract, TContext>>;
9961002
extensions?: Maybe<Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>>;
9971003
astNode?: Maybe<ObjectTypeDefinitionNode>;
9981004
extensionASTNodes?: Maybe<ReadonlyArray<ObjectTypeExtensionNode>>;
9991005
}
10001006

1001-
export interface GraphQLObjectTypeNormalizedConfig<TSource, TContext>
1002-
extends GraphQLObjectTypeConfig<TSource, TContext> {
1007+
export interface GraphQLObjectTypeNormalizedConfig<
1008+
TSource,
1009+
TContext,
1010+
TAbstract = unknown,
1011+
> extends GraphQLObjectTypeConfig<TSource, TContext, TAbstract> {
10031012
interfaces: ReadonlyArray<GraphQLInterfaceType>;
10041013
fields: GraphQLFieldNormalizedConfigMap<TSource, TContext>;
10051014
extensions: Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>;
@@ -1013,8 +1022,8 @@ export type GraphQLTypeResolver<TSource, TContext> = (
10131022
abstractType: GraphQLAbstractType,
10141023
) => PromiseOrValue<string | undefined>;
10151024

1016-
export type GraphQLIsTypeOfFn<TSource, TContext> = (
1017-
source: TSource,
1025+
export type GraphQLIsTypeOfFn<TAbstract, TContext> = (
1026+
value: TAbstract,
10181027
context: TContext,
10191028
info: GraphQLResolveInfo,
10201029
) => PromiseOrValue<boolean>;

0 commit comments

Comments
 (0)