Skip to content

Commit

Permalink
Add option to disable cache in server fetcher.
Browse files Browse the repository at this point in the history
This can be set when NODE_ENV === development
  • Loading branch information
mvantellingen committed Nov 23, 2023
1 parent e16474f commit 46e0779
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-moons-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/react-query-opal": patch
---

Add option to disable cache in server fetcher. This can be set when NODE_ENV === development
38 changes: 37 additions & 1 deletion src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const errorResponse = JSON.stringify({
});

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

it("should fetch a persisted query", async () => {
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
const mockedFetch = fetchMock.mockResponse(successResponse);
const gqlResponse = await gqlServerFetch(
query,
Expand Down Expand Up @@ -55,6 +55,7 @@ describe("gqlServerFetch", () => {
});

it("should persist the query if it wasn't persisted yet", async () => {
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
// Mock server saying: 'PersistedQueryNotFound'
const mockedFetch = fetchMock
.mockResponseOnce(errorResponse)
Expand Down Expand Up @@ -96,6 +97,7 @@ describe("gqlServerFetch", () => {
);
});
it("should fetch a persisted query without revalidate", async () => {
const gqlServerFetch = initServerFetcher("https://localhost/graphql");
const mockedFetch = fetchMock.mockResponse(successResponse);
const gqlResponse = await gqlServerFetch(
query,
Expand Down Expand Up @@ -127,5 +129,39 @@ describe("gqlServerFetch", () => {
);
});

it("should disable cache when disableCache is set", async () => {
const gqlServerFetch = initServerFetcher("https://localhost/graphql", {
disableCache: true,
});
const mockedFetch = fetchMock.mockResponse(successResponse);
const gqlResponse = await gqlServerFetch(
query,
{ myVar: "baz" },

// These don't have impact due to disableCache
"force-cache",
{ revalidate: 900 }
);


expect(gqlResponse).toEqual(response);
expect(mockedFetch).toHaveBeenCalledTimes(1);
expect(mockedFetch).toHaveBeenCalledWith(
"https://localhost/graphql",
{
method: "POST", // <- Note that when persisting the query, the method is 'POST'
body: JSON.stringify({
operationName: "myQuery",
query: query.toString(),
variables: { myVar: "baz" }
}),
headers: {
"Content-Type": "application/json",
},
cache: 'no-store',
next: { revalidate: 0 },
}
);
});

});
18 changes: 15 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,34 @@ import {
pruneObject,
} from "./helpers";

type Options = {
disableCache?: boolean;
};

export const initServerFetcher =
(url: string) =>
(url: string, options?: Options) =>
/**
* Replace full queries with generated ID's to reduce bandwidth.
* @see https://www.apollographql.com/docs/react/api/link/persisted-queries/#protocol
*/
async <TResponse, TVariables>(
astNode: DocumentTypeDecoration<TResponse, TVariables>,
variables: TVariables,
cache: RequestCache = 'force-cache',
cache: RequestCache = "force-cache",
next: NextFetchRequestConfig = {}
): Promise<GqlResponse<TResponse>> => {
const query = astNode.toString();
const operationName = extractOperationName(query);

// Disable cache when revalidate is not set
if (options?.disableCache) {
return gqlPost<TResponse>(
url,
JSON.stringify({ operationName, query, variables }),
"no-store",
{ ...next, revalidate: 0 }
);
}

const extensions = {
persistedQuery: {
version: 1,
Expand Down

0 comments on commit 46e0779

Please sign in to comment.