Skip to content

Commit bcad59d

Browse files
tvervestblurrah
authored andcommitted
chore: additional tests
1 parent 65db905 commit bcad59d

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

src/client.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe("gqlClientFetch", () => {
6060
headers: {
6161
"Content-Type": "application/json",
6262
},
63+
signal: expect.any(AbortSignal),
6364
}
6465
);
6566
});
@@ -84,7 +85,7 @@ describe("gqlClientFetch", () => {
8485
"Content-Type": "application/json",
8586
},
8687
signal: expect.any(AbortSignal),
87-
},
88+
}
8889
);
8990
});
9091
it("should perform a mutation", async () => {
@@ -161,7 +162,7 @@ describe("gqlClientFetch", () => {
161162

162163
it("should use the provided timeout duration", async () => {
163164
const fetcher = initClientFetcher("https://localhost/graphql", {
164-
timeout: 1,
165+
defaultTimeout: 1,
165166
});
166167
const timeoutSpy = vi.spyOn(AbortSignal, "timeout");
167168
fetchMock.mockResponse(responseString);
@@ -175,4 +176,28 @@ describe("gqlClientFetch", () => {
175176
// It should not try to POST the query if the persisted query cannot be parsed
176177
expect(fetchMock).toHaveBeenCalledTimes(1);
177178
});
179+
180+
it("should use the provided signal", async () => {
181+
const fetcher = initClientFetcher("https://localhost/graphql");
182+
fetchMock.mockResponse(responseString);
183+
184+
const controller = new AbortController();
185+
await fetcher(
186+
query,
187+
{
188+
myVar: "baz",
189+
},
190+
controller.signal
191+
);
192+
193+
expect(fetchMock).toHaveBeenCalledWith(
194+
expect.any(String),
195+
expect.objectContaining({
196+
signal: controller.signal,
197+
})
198+
);
199+
200+
// It should not try to POST the query if the persisted query cannot be parsed
201+
expect(fetchMock).toHaveBeenCalledTimes(1);
202+
});
178203
});

src/client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ type Options = {
1919
persistedQueries?: boolean;
2020

2121
/**
22-
* Sets the timeout duration in ms after which a request will throw a timeout error
22+
* Sets the default timeout duration in ms after which a request will throw a timeout error
2323
*
2424
* @default 30000
2525
*/
26-
timeout?: number;
26+
defaultTimeout?: number;
2727
};
2828

2929
export type ClientFetcher = <TResponse, TVariables>(
@@ -35,7 +35,7 @@ export type ClientFetcher = <TResponse, TVariables>(
3535
export const initClientFetcher =
3636
(
3737
endpoint: string,
38-
{ persistedQueries = false, timeout = 30000 }: Options = {}
38+
{ persistedQueries = false, defaultTimeout = 30000 }: Options = {}
3939
): ClientFetcher =>
4040
/**
4141
* Executes a GraphQL query post request on the client.
@@ -46,7 +46,7 @@ export const initClientFetcher =
4646
async <TResponse, TVariables>(
4747
astNode: DocumentTypeDecoration<TResponse, TVariables>,
4848
variables?: TVariables,
49-
signal: AbortSignal = AbortSignal.timeout(timeout)
49+
signal: AbortSignal = AbortSignal.timeout(defaultTimeout)
5050
): Promise<GqlResponse<TResponse>> => {
5151
const query = astNode.toString();
5252

@@ -94,6 +94,7 @@ export const initClientFetcher =
9494
method: "POST",
9595
body: JSON.stringify({ query, variables, extensions }),
9696
credentials: "include",
97+
signal,
9798
})
9899
);
99100
}

src/server.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe("gqlServerFetch", () => {
4646
},
4747
cache: "force-cache",
4848
next: { revalidate: 900 },
49+
signal: expect.any(AbortSignal),
4950
}
5051
);
5152
});
@@ -119,6 +120,7 @@ describe("gqlServerFetch", () => {
119120
},
120121
cache: "no-store",
121122
next: { revalidate: undefined },
123+
signal: expect.any(AbortSignal),
122124
}
123125
);
124126
});
@@ -192,7 +194,7 @@ describe("gqlServerFetch", () => {
192194
it("should use the provided timeout duration", async () => {
193195
const timeoutSpy = vi.spyOn(AbortSignal, "timeout");
194196
const gqlServerFetch = initServerFetcher("https://localhost/graphql", {
195-
timeout: 1,
197+
defaultTimeout: 1,
196198
});
197199
fetchMock.mockResponse(successResponse);
198200

@@ -203,4 +205,24 @@ describe("gqlServerFetch", () => {
203205
// It should not try to POST the query if the persisted query cannot be parsed
204206
expect(fetchMock).toHaveBeenCalledTimes(1);
205207
});
208+
209+
it("should use the provided signal", async () => {
210+
const gqlServerFetch = initServerFetcher("https://localhost/graphql", {
211+
defaultTimeout: 1,
212+
});
213+
fetchMock.mockResponse(successResponse);
214+
215+
const controller = new AbortController();
216+
await gqlServerFetch(query, { myVar: "baz" }, {}, controller.signal);
217+
218+
expect(fetchMock).toHaveBeenCalledWith(
219+
expect.any(String),
220+
expect.objectContaining({
221+
signal: controller.signal,
222+
})
223+
);
224+
225+
// It should not try to POST the query if the persisted query cannot be parsed
226+
expect(fetchMock).toHaveBeenCalledTimes(1);
227+
});
206228
});

src/server.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ type Options = {
2020
dangerouslyDisableCache?: boolean;
2121

2222
/**
23-
* Sets the timeout duration in ms after which a request will throw a timeout error
23+
* Sets the default timeout duration in ms after which a request will throw a timeout error
2424
*
2525
* @default 30000
2626
*/
27-
timeout?: number;
27+
defaultTimeout?: number;
2828
};
2929

3030
type CacheOptions = {
@@ -40,13 +40,13 @@ const tracer = trace.getTracer(
4040
export const initServerFetcher =
4141
(
4242
url: string,
43-
{ dangerouslyDisableCache = false, timeout = 30000 }: Options = {}
43+
{ dangerouslyDisableCache = false, defaultTimeout = 30000 }: Options = {}
4444
) =>
4545
async <TResponse, TVariables>(
4646
astNode: DocumentTypeDecoration<TResponse, TVariables>,
4747
variables: TVariables,
4848
{ cache, next = {} }: CacheOptions,
49-
signal: AbortSignal = AbortSignal.timeout(timeout)
49+
signal: AbortSignal = AbortSignal.timeout(defaultTimeout)
5050
): Promise<GqlResponse<TResponse>> => {
5151
const query = astNode.toString();
5252
const operationName = extractOperationName(query) || "(GraphQL)";
@@ -93,15 +93,17 @@ export const initServerFetcher =
9393
let response = await gqlPersistedQuery(
9494
url,
9595
getQueryString(operationName, variables, extensions),
96-
{ cache, next }
96+
{ cache, next },
97+
signal
9798
);
9899

99100
if (response.errors?.[0]?.message === "PersistedQueryNotFound") {
100101
// If the cached query doesn't exist, fall back to POST request and let the server cache it.
101102
response = await gqlPost(
102103
url,
103104
JSON.stringify({ operationName, query, variables, extensions }),
104-
{ cache, next }
105+
{ cache, next },
106+
signal
105107
);
106108
}
107109

@@ -138,13 +140,15 @@ const gqlPost = async (
138140
const gqlPersistedQuery = async (
139141
url: string,
140142
queryString: URLSearchParams,
141-
{ cache, next }: CacheOptions
143+
{ cache, next }: CacheOptions,
144+
signal: AbortSignal = AbortSignal.timeout(30000)
142145
) => {
143146
const response = await fetch(`${url}?${queryString}`, {
144147
method: "GET",
145148
headers: defaultHeaders,
146149
cache,
147150
next,
151+
signal,
148152
});
149153

150154
return parseResponse(response);

0 commit comments

Comments
 (0)