Skip to content
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
"@graphql-typed-document-node/core": "^3.1.0",
"@types/aws4": "^1.5.1",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.10",
"@types/node": "^16.18.96",
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

updating @types/node was necessary to get the types for signal support. This should not change the minimum required node version of this project, as older versions of node will simply ignore the signal property.

"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"eslint": "^7.14.0",
"eslint-config-airbnb-typescript": "^12.0.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"jest": "^29.7.0",
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

updating Jest was necessary to support AbortController in tests. At least v27 is required, I updated all the way.

"nock": "^13.0.5",
"prettier": "^2.2.1",
"ts-jest": "^26.4.4",
"ts-jest": "^29.1.2",
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

updating ts-jest was required to match the jest version

"typescript": "~4.1.2",
"versiony-cli": "^1.3.0"
},
Expand Down
7 changes: 6 additions & 1 deletion src/AppsyncClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ export default class AppsyncClient {
public request<TData = any, TVariables = Record<string, any>>({
query,
variables,
signal,
}: {
query: TypedDocumentNode<TData, TVariables>;
variables?: TVariables;
signal?: AbortSignal;
}): Promise<TData> {
const requestObject = createRequestObject({
host: this.host,
Expand All @@ -118,7 +120,10 @@ export default class AppsyncClient {
sessionToken: this.sessionToken,
});

return httpsRequestPromisified(signedRequestObject) as Promise<TData>;
return httpsRequestPromisified({
...signedRequestObject,
signal,
}) as Promise<TData>;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/createRequestObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type RequestObjectParams = {
path: string;
query: TypedDocumentNode;
variables: any;
signal?: AbortSignal;
};

export type RequestObject = RequestOptions & {
Expand All @@ -23,6 +24,7 @@ export default function createRequestObject({
path,
query,
variables,
signal,
}: RequestObjectParams): RequestObject {
// Covert the DocumentNode to a raw string query
const rawQuery = print(query);
Expand All @@ -39,5 +41,6 @@ export default function createRequestObject({
query: rawQuery,
variables,
}),
signal,
};
}
17 changes: 17 additions & 0 deletions src/tests/AppsyncClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ test("Graphql fake-tag works", () => {
const res = gql`hi`;
expect(res).toBe("hi");
});

// nock currently doesn’t support abort signals, see https://github.com/nock/nock/issues/2478
// this test works but uses the real network, so it’s skipped by default
test("abort signal is respected", async () => {
const realUrl = "https://example.com";
const aborted = new AbortController();
aborted.abort();
await expect(() =>
new AppsyncClient({ ...testParams, apiUrl: realUrl }).request({
query: GetTodoDocument,
variables: {
id: "id",
},
signal: aborted.signal,
})
).rejects.toThrow("The operation was aborted");
});
Loading