Skip to content

Commit

Permalink
update defaultFieldResolver
Browse files Browse the repository at this point in the history
and add test
  • Loading branch information
yaacovCR committed Oct 28, 2024
1 parent 70f780e commit c17544e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/execution/__tests__/abort-signal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,53 @@ describe('Execute: Cancellation', () => {
});
});

it('should provide access to the abort signal within resolvers', async () => {
const throwIfAborted = async (abortSignal: AbortSignal) => {
await resolveOnNextTick();
abortSignal.throwIfAborted();
};

const abortController = new AbortController();
const document = parse(`
query {
todo {
id
}
}
`);

const resultPromise = execute({
document,
schema,
abortSignal: abortController.signal,
rootValue: {
todo: {
id: (_args: any, _context: any, _info: any, signal: AbortSignal) =>
throwIfAborted(signal),
},
},
});

abortController.abort();

const result = await resultPromise;

expectJSON(result).toDeepEqual({
data: {
todo: {
id: null,
},
},
errors: [
{
message: 'This operation was aborted',
path: ['todo', 'id'],
locations: [{ line: 4, column: 11 }],
},
],
});
});

it('should stop the execution when aborted during object field completion with a custom error', async () => {
const abortController = new AbortController();
const document = parse(`
Expand Down
4 changes: 2 additions & 2 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1955,12 +1955,12 @@ export const defaultTypeResolver: GraphQLTypeResolver<unknown, unknown> =
* of calling that function while passing along args and context value.
*/
export const defaultFieldResolver: GraphQLFieldResolver<unknown, unknown> =
function (source: any, args, contextValue, info) {
function (source: any, args, contextValue, info, abortSignal) {
// ensure source is a value for which property access is acceptable.
if (isObjectLike(source) || typeof source === 'function') {
const property = source[info.fieldName];
if (typeof property === 'function') {
return source[info.fieldName](args, contextValue, info);
return source[info.fieldName](args, contextValue, info, abortSignal);
}
return property;
}
Expand Down

0 comments on commit c17544e

Please sign in to comment.