Skip to content

Commit c17544e

Browse files
committed
update defaultFieldResolver
and add test
1 parent 70f780e commit c17544e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/execution/__tests__/abort-signal-test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,53 @@ describe('Execute: Cancellation', () => {
109109
});
110110
});
111111

112+
it('should provide access to the abort signal within resolvers', async () => {
113+
const throwIfAborted = async (abortSignal: AbortSignal) => {
114+
await resolveOnNextTick();
115+
abortSignal.throwIfAborted();
116+
};
117+
118+
const abortController = new AbortController();
119+
const document = parse(`
120+
query {
121+
todo {
122+
id
123+
}
124+
}
125+
`);
126+
127+
const resultPromise = execute({
128+
document,
129+
schema,
130+
abortSignal: abortController.signal,
131+
rootValue: {
132+
todo: {
133+
id: (_args: any, _context: any, _info: any, signal: AbortSignal) =>
134+
throwIfAborted(signal),
135+
},
136+
},
137+
});
138+
139+
abortController.abort();
140+
141+
const result = await resultPromise;
142+
143+
expectJSON(result).toDeepEqual({
144+
data: {
145+
todo: {
146+
id: null,
147+
},
148+
},
149+
errors: [
150+
{
151+
message: 'This operation was aborted',
152+
path: ['todo', 'id'],
153+
locations: [{ line: 4, column: 11 }],
154+
},
155+
],
156+
});
157+
});
158+
112159
it('should stop the execution when aborted during object field completion with a custom error', async () => {
113160
const abortController = new AbortController();
114161
const document = parse(`

src/execution/execute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,12 +1955,12 @@ export const defaultTypeResolver: GraphQLTypeResolver<unknown, unknown> =
19551955
* of calling that function while passing along args and context value.
19561956
*/
19571957
export const defaultFieldResolver: GraphQLFieldResolver<unknown, unknown> =
1958-
function (source: any, args, contextValue, info) {
1958+
function (source: any, args, contextValue, info, abortSignal) {
19591959
// ensure source is a value for which property access is acceptable.
19601960
if (isObjectLike(source) || typeof source === 'function') {
19611961
const property = source[info.fieldName];
19621962
if (typeof property === 'function') {
1963-
return source[info.fieldName](args, contextValue, info);
1963+
return source[info.fieldName](args, contextValue, info, abortSignal);
19641964
}
19651965
return property;
19661966
}

0 commit comments

Comments
 (0)