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

Add support for AbortController signal passed via context #296

Open
wants to merge 2 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/__tests__/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4211,4 +4211,76 @@ describe('Playing nice with others', () => {
authors: { message: 'Your query was bad and you should feel bad!' },
});
});

it('should fulfill fetch when an AbortController signal is passed but never aborted', done => {
fetchMock.get('/api/posts', posts);
const link = new RestLink({ uri: '/api' });

const query = gql`
query {
people @rest(type: "[Post]", path: "/posts") {
title
}
}
`;

const controller = new AbortController();
const reqPromise = toPromise<Result>(
execute(link, {
operationName: 'abortQuery',
query,
context: { fetchOptions: { signal: controller.signal } },
}),
);

const timeout = setTimeout(() => {
done('timeout should never run');
}, 100);

return reqPromise.then(res => {
clearTimeout(timeout);
expect(res.data).toEqual({
people: [
{ title: 'Love apollo', __typename: 'Post' },
{ title: 'Respect apollo', __typename: 'Post' },
],
});
done();
});
});

it('should cancel fetch when an AbortController signal is passed and aborted', done => {
fetchMock.get('/api/posts', posts);
const link = new RestLink({ uri: '/api' });

const query = gql`
query {
people @rest(ftype: "[Post]", path: "/posts") {
title
}
}
`;

const controller = new AbortController();
const reqPromise = toPromise<Result>(
execute(link, {
operationName: 'abortQuery',
query,
context: { fetchOptions: { signal: controller.signal } },
}),
);
controller.abort();

let reqData = null;
const timeout = setTimeout(() => {
expect(reqData).toBeNull();
done();
}, 100);

return reqPromise.then(res => {
clearTimeout(timeout);
reqData = res.data;
done('fetch request should not resolve');
});
});
});
12 changes: 12 additions & 0 deletions src/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ interface LinkChainContext {

/** An array of the responses from each fetched URL, useful for accessing headers in earlier links */
restResponses?: Response[];

/** Overrides some fetch options arguments passed to the fetch call */
fetchOptions?: {
signal?: AbortSignal;
};
}

/** Context passed via graphql() to our resolver */
Expand All @@ -856,6 +861,8 @@ interface RequestContext {

/** An array of the responses from each fetched URL */
responses: Response[];

signal?: AbortSignal;
}

const addTypeToNode = (node, typename) => {
Expand Down Expand Up @@ -940,6 +947,7 @@ const resolver: Resolver = async (
fieldNameNormalizer: linkLevelNameNormalizer,
fieldNameDenormalizer: linkLevelNameDenormalizer,
serializers,
signal,
responseTransformer,
} = context;

Expand Down Expand Up @@ -1076,6 +1084,7 @@ const resolver: Resolver = async (
// Only set credentials if they're non-null as some browsers throw an exception:
// https://github.com/apollographql/apollo-link-rest/issues/121#issuecomment-396049677
...(credentials ? { credentials } : {}),
...(signal ? { signal } : {}),
};
const requestUrl = `${endpointOption.uri}${pathWithParams}`;

Expand Down Expand Up @@ -1348,6 +1357,8 @@ export class RestLink extends ApolloLink {

const credentials: RequestCredentials =
context.credentials || this.credentials;
const signal: AbortSignal | undefined =
context.fetchOptions != null ? context.fetchOptions.signal : undefined;

const queryWithTypename = addTypenameToDocument(query);

Expand All @@ -1371,6 +1382,7 @@ export class RestLink extends ApolloLink {
fragmentDefinitions,
typePatcher: this.typePatcher,
serializers: this.serializers,
signal,
responses: [],
responseTransformer: this.responseTransformer,
};
Expand Down