Skip to content

Catch unhandled exception in abstract resolution #4392

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

Open
wants to merge 3 commits into
base: 16.x.x
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"src/jsutils/ObjMap.ts",
"src/jsutils/PromiseOrValue.ts",
"src/utilities/assertValidName.ts",
"src/utilities/typedQueryDocumentNode.ts"
"src/utilities/typedQueryDocumentNode.ts",
"src/**/__tests__/**/*.ts"
],
"clean": true,
"temp-directory": "coverage",
Expand Down
115 changes: 113 additions & 2 deletions src/execution/__tests__/union-interface-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { GraphQLBoolean, GraphQLString } from '../../type/scalars';
import { GraphQLSchema } from '../../type/schema';

import { executeSync } from '../execute';
import { execute, executeSync } from '../execute';

class Dog {
name: string;
Expand Down Expand Up @@ -118,7 +118,6 @@ const PetType = new GraphQLUnionType({
if (value instanceof Cat) {
return CatType.name;
}
/* c8 ignore next 3 */
// Not reachable, all possible types have been considered.
expect.fail('Not reachable');
},
Expand Down Expand Up @@ -154,6 +153,71 @@ odie.mother.progeny = [odie];
const liz = new Person('Liz');
const john = new Person('John', [garfield, odie], [liz, odie]);

const SearchableInterface = new GraphQLInterfaceType({
name: 'Searchable',
fields: {
id: { type: GraphQLString },
},
});

const TypeA = new GraphQLObjectType({
name: 'TypeA',
interfaces: [SearchableInterface],
fields: () => ({
id: { type: GraphQLString },
nameA: { type: GraphQLString },
}),
isTypeOf: (_value, _context, _info) =>
new Promise((_resolve, reject) =>
// eslint-disable-next-line
setTimeout(() => reject(new Error('TypeA_isTypeOf_rejected')), 10),
),
});

const TypeB = new GraphQLObjectType({
name: 'TypeB',
interfaces: [SearchableInterface],
fields: () => ({
id: { type: GraphQLString },
nameB: { type: GraphQLString },
}),
isTypeOf: (value: any, _context, _info) => value.id === 'b',
});

const queryTypeWithSearchable = new GraphQLObjectType({
name: 'Query',
fields: {
person: {
type: PersonType,
resolve: () => john,
},
search: {
type: SearchableInterface,
args: { id: { type: GraphQLString } },
resolve: (_source, { id }) => {
if (id === 'a') {
return { id: 'a', nameA: 'Object A' };
} else if (id === 'b') {
return { id: 'b', nameB: 'Object B' };
}
},
},
},
});

const schemaWithSearchable = new GraphQLSchema({
query: queryTypeWithSearchable,
types: [
PetType,
TypeA,
TypeB,
SearchableInterface,
PersonType,
DogType,
CatType,
],
});

describe('Execute: Union and intersection types', () => {
it('can introspect on union and intersection types', () => {
const document = parse(`
Expand Down Expand Up @@ -545,4 +609,51 @@ describe('Execute: Union and intersection types', () => {
expect(encounteredRootValue).to.equal(rootValue);
expect(encounteredContext).to.equal(contextValue);
});

it('handles promises from isTypeOf correctly when a later type matches synchronously', async () => {
const document = parse(`
query TestSearch {
search(id: "b") {
__typename
id
... on TypeA {
nameA
}
... on TypeB {
nameB
}
}
}
`);

let unhandledRejection: any = null;
const unhandledRejectionListener = (reason: any) => {
unhandledRejection = reason;
};
// eslint-disable-next-line
process.on('unhandledRejection', unhandledRejectionListener);

const result = await execute({
schema: schemaWithSearchable,
document,
});

expect(result.errors).to.equal(undefined);
expect(result.data).to.deep.equal({
search: {
__typename: 'TypeB',
id: 'b',
nameB: 'Object B',
},
});

// Give the TypeA promise a chance to reject and the listener to fire
// eslint-disable-next-line
await new Promise((resolve) => setTimeout(resolve, 20));

// eslint-disable-next-line
process.removeListener('unhandledRejection', unhandledRejectionListener);

expect(unhandledRejection).to.equal(null);
});
});
8 changes: 8 additions & 0 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,14 @@ export const defaultTypeResolver: GraphQLTypeResolver<unknown, unknown> =
if (isPromise(isTypeOfResult)) {
promisedIsTypeOfResults[i] = isTypeOfResult;
} else if (isTypeOfResult) {
if (promisedIsTypeOfResults.length) {
// Explicitly ignore any promise rejections
Promise.allSettled(promisedIsTypeOfResults)
/* c8 ignore next 3 */
.catch(() => {
// Do nothing
});
}
return type.name;
}
}
Expand Down
Loading