Skip to content

Commit 46e0779

Browse files
committed
Add option to disable cache in server fetcher.
This can be set when NODE_ENV === development
1 parent e16474f commit 46e0779

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

.changeset/early-moons-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/react-query-opal": patch
3+
---
4+
5+
Add option to disable cache in server fetcher. This can be set when NODE_ENV === development

src/server.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const errorResponse = JSON.stringify({
1818
});
1919

2020
describe("gqlServerFetch", () => {
21-
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
2221

2322
it("should fetch a persisted query", async () => {
23+
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
2424
const mockedFetch = fetchMock.mockResponse(successResponse);
2525
const gqlResponse = await gqlServerFetch(
2626
query,
@@ -55,6 +55,7 @@ describe("gqlServerFetch", () => {
5555
});
5656

5757
it("should persist the query if it wasn't persisted yet", async () => {
58+
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
5859
// Mock server saying: 'PersistedQueryNotFound'
5960
const mockedFetch = fetchMock
6061
.mockResponseOnce(errorResponse)
@@ -96,6 +97,7 @@ describe("gqlServerFetch", () => {
9697
);
9798
});
9899
it("should fetch a persisted query without revalidate", async () => {
100+
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
99101
const mockedFetch = fetchMock.mockResponse(successResponse);
100102
const gqlResponse = await gqlServerFetch(
101103
query,
@@ -127,5 +129,39 @@ describe("gqlServerFetch", () => {
127129
);
128130
});
129131

132+
it("should disable cache when disableCache is set", async () => {
133+
const gqlServerFetch = initServerFetcher("https://localhost/graphql", {
134+
disableCache: true,
135+
});
136+
const mockedFetch = fetchMock.mockResponse(successResponse);
137+
const gqlResponse = await gqlServerFetch(
138+
query,
139+
{ myVar: "baz" },
140+
141+
// These don't have impact due to disableCache
142+
"force-cache",
143+
{ revalidate: 900 }
144+
);
145+
146+
147+
expect(gqlResponse).toEqual(response);
148+
expect(mockedFetch).toHaveBeenCalledTimes(1);
149+
expect(mockedFetch).toHaveBeenCalledWith(
150+
"https://localhost/graphql",
151+
{
152+
method: "POST", // <- Note that when persisting the query, the method is 'POST'
153+
body: JSON.stringify({
154+
operationName: "myQuery",
155+
query: query.toString(),
156+
variables: { myVar: "baz" }
157+
}),
158+
headers: {
159+
"Content-Type": "application/json",
160+
},
161+
cache: 'no-store',
162+
next: { revalidate: 0 },
163+
}
164+
);
165+
});
130166

131167
});

src/server.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,34 @@ import {
88
pruneObject,
99
} from "./helpers";
1010

11+
type Options = {
12+
disableCache?: boolean;
13+
};
14+
1115
export const initServerFetcher =
12-
(url: string) =>
16+
(url: string, options?: Options) =>
1317
/**
1418
* Replace full queries with generated ID's to reduce bandwidth.
1519
* @see https://www.apollographql.com/docs/react/api/link/persisted-queries/#protocol
1620
*/
1721
async <TResponse, TVariables>(
1822
astNode: DocumentTypeDecoration<TResponse, TVariables>,
1923
variables: TVariables,
20-
cache: RequestCache = 'force-cache',
24+
cache: RequestCache = "force-cache",
2125
next: NextFetchRequestConfig = {}
2226
): Promise<GqlResponse<TResponse>> => {
2327
const query = astNode.toString();
2428
const operationName = extractOperationName(query);
2529

26-
// Disable cache when revalidate is not set
30+
if (options?.disableCache) {
31+
return gqlPost<TResponse>(
32+
url,
33+
JSON.stringify({ operationName, query, variables }),
34+
"no-store",
35+
{ ...next, revalidate: 0 }
36+
);
37+
}
38+
2739
const extensions = {
2840
persistedQuery: {
2941
version: 1,

0 commit comments

Comments
 (0)