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: revert unnecessary breaking change for resolveType #3249

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions src/execution/__tests__/abstract-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ describe('Execute: Handles execution of abstract types', () => {
};
}

function expectNoError({ forTypeName }: { forTypeName: unknown }) {
const rootValue = { pet: { __typename: forTypeName } };
const result = executeSync({ schema, document, rootValue });
expect(result).to.deep.equal({
data: { pet: { name: null } },
});
}

expectError({ forTypeName: undefined }).toEqual(
'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet". Either the "Pet" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.',
);
Expand Down Expand Up @@ -580,8 +588,6 @@ describe('Execute: Handles execution of abstract types', () => {
// @ts-expect-error
assertInterfaceType(schema.getType('Pet')).resolveType = () =>
schema.getType('Cat');
expectError({ forTypeName: undefined }).toEqual(
'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.',
);
expectNoError({ forTypeName: undefined });
});
});
27 changes: 14 additions & 13 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
GraphQLResolveInfo,
GraphQLTypeResolver,
GraphQLList,
GraphQLNamedType,
} from '../type/definition';
import { assertValidSchema } from '../type/validate';
import {
Expand Down Expand Up @@ -813,46 +814,46 @@ function completeAbstractValue(
}

function ensureValidRuntimeType(
runtimeTypeName: unknown,
runtimeObjectTypeOrTypeName: unknown,
exeContext: ExecutionContext,
returnType: GraphQLAbstractType,
fieldNodes: ReadonlyArray<FieldNode>,
info: GraphQLResolveInfo,
result: unknown,
): GraphQLObjectType {
if (runtimeTypeName == null) {
if (runtimeObjectTypeOrTypeName == null) {
throw new GraphQLError(
`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}". Either the "${returnType.name}" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.`,
fieldNodes,
);
}

let runtimeType: GraphQLNamedType | undefined;

// releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType`
// TODO: remove in 17.0.0 release
if (isObjectType(runtimeTypeName)) {
throw new GraphQLError(
'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.',
);
}

if (typeof runtimeTypeName !== 'string') {
if (isObjectType(runtimeObjectTypeOrTypeName)) {
runtimeType = runtimeObjectTypeOrTypeName;
} else if (typeof runtimeObjectTypeOrTypeName !== 'string') {
throw new GraphQLError(
`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
`value ${inspect(result)}, received "${inspect(runtimeTypeName)}".`,
`value ${inspect(result)}, ` +
`received "${inspect(runtimeObjectTypeOrTypeName)}".`,
);
} else {
runtimeType = exeContext.schema.getType(runtimeObjectTypeOrTypeName);
}

const runtimeType = exeContext.schema.getType(runtimeTypeName);
if (runtimeType == null) {
throw new GraphQLError(
`Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`,
`Abstract type "${returnType.name}" was resolved to a type "${runtimeObjectTypeOrTypeName}" that does not exist inside the schema.`,
fieldNodes,
);
}

if (!isObjectType(runtimeType)) {
throw new GraphQLError(
`Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`,
`Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeObjectTypeOrTypeName}".`,
fieldNodes,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ export type GraphQLTypeResolver<TSource, TContext> = (
context: TContext,
info: GraphQLResolveInfo,
abstractType: GraphQLAbstractType,
) => PromiseOrValue<string | undefined>;
) => PromiseOrValue<GraphQLObjectType | string | undefined>;

export type GraphQLIsTypeOfFn<TSource, TContext> = (
source: TSource,
Expand Down