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(@aws-amplify/api): graphql API.cancel fix #9578

Merged
merged 15 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 19 additions & 6 deletions packages/api-rest/__tests__/RestClient-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ describe('RestClient test', () => {
});

describe('Cancel Token', () => {
afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

const apiOptions = {
headers: {},
endpoints: [
Expand All @@ -474,16 +479,24 @@ describe('RestClient test', () => {

test('request non existent', () => {
const restClient = new RestClient(apiOptions);
// if the request doesn't exist we can still say it is canceled successfully
expect(
restClient.cancel(
new Promise<any>((req, res) => {})
)
).toBeTruthy();
expect(restClient.cancel(new Promise<any>((req, res) => {}))).toBe(false);
});

test('request exist', () => {
const restClient = new RestClient(apiOptions);
const request = Promise.resolve();
restClient.updateRequestToBeCancellable(
request,
restClient.getCancellableToken()
);
expect(restClient.cancel(request)).toBe(true);
});

test('happy case', () => {
const restClient = new RestClient(apiOptions);
jest
.spyOn(RestClient.prototype, 'ajax')
.mockImplementationOnce(() => Promise.resolve());

const cancellableToken = restClient.getCancellableToken();
const request = restClient.ajax('url', 'method', { cancellableToken });
Expand Down
9 changes: 4 additions & 5 deletions packages/api-rest/src/RestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ export class RestClient {
const source = this._cancelTokenMap.get(request);
if (source) {
source.cancel(message);
return true;
}
return true;
return false;
}

/**
Expand Down Expand Up @@ -366,10 +367,8 @@ export class RestClient {
/** private methods **/

private _signed(params, credentials, isAllResponse, { service, region }) {
const {
signerServiceInfo: signerServiceInfoParams,
...otherParams
} = params;
const { signerServiceInfo: signerServiceInfoParams, ...otherParams } =
params;

const endpoint_region: string =
region || this._region || this._options.region;
Expand Down
28 changes: 28 additions & 0 deletions packages/api/__tests__/API-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,32 @@ describe('API test', () => {
const api = new API(null);
expect(await api.graphql(null)).toBe('grapqhqlResponse');
});

describe('cancel', () => {
test('cancel RestAPI request', async () => {
jest
.spyOn(GraphQLAPIClass.prototype, 'cancel')
.mockImplementation(() => false);
const restAPICancelSpy = jest
.spyOn(RestAPIClass.prototype, 'cancel')
.mockImplementation(() => true);
const api = new API(null);
const request = Promise.resolve();
expect(api.cancel(request)).toBe(true);
expect(restAPICancelSpy).toHaveBeenCalled();
});

test('cancel GraphQLAPI request', async () => {
const graphQLAPICancelSpy = jest
.spyOn(GraphQLAPIClass.prototype, 'cancel')
.mockImplementation(() => true);
jest
.spyOn(RestAPIClass.prototype, 'cancel')
.mockImplementation(() => false);
const api = new API(null);
const request = Promise.resolve();
expect(api.cancel(request)).toBe(true);
expect(graphQLAPICancelSpy).toHaveBeenCalled();
});
});
});
15 changes: 10 additions & 5 deletions packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,17 @@ export class APIClass {
return this._restApi.isCancel(error);
}
/**
* Cancels an inflight request
* @param {any} request - request to cancel
* @return {boolean} - A boolean indicating if the request was cancelled
* Cancels an inflight request for either a GraphQL request or a Rest API request.
* @param request - request to cancel
* @param [message] - custom error message
* @return If the request was cancelled
*/
cancel(request: Promise<any>, message?: string) {
return this._restApi.cancel(request, message);
cancel(request: Promise<any>, message?: string): boolean {
// it's safe to potentially call both method, as 'request' refers to a reference of a request, which is unique.
return (
this._restApi.cancel(request, message) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not as familiar with this part of the codebase, but wondering if it's possible to conditionally call one or the other, depending on what the API type is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can check what the API type is because the parameter only takes the request (which can be any promise) and check it against the internal WeakMap. I did add a hasCancelToken method to check before we actually call the .cancel function.

this._graphqlApi.cancel(request, message)
);
}

/**
Expand Down