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
5 changes: 5 additions & 0 deletions .changeset/forty-paws-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/graphql-fetcher": minor
---

override credentials option
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
.DS_Store
test-reports
61 changes: 60 additions & 1 deletion src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createSha256 } from "helpers";
import {
afterAll,
beforeAll,
Expand All @@ -10,7 +11,6 @@ import {
import createFetchMock from "vitest-fetch-mock";
import { initClientFetcher } from "./client";
import { TypedDocumentString } from "./testing";
import { createSha256 } from "helpers";

const query = new TypedDocumentString(/* GraphQL */ `
query myQuery {
Expand Down Expand Up @@ -98,6 +98,7 @@ describe("gqlClientFetch", () => {
},
);
});

it("should perform a mutation", async () => {
const mockedFetch = fetchMock.mockResponse(responseString);
const gqlResponse = await fetcher(mutation, {
Expand Down Expand Up @@ -196,6 +197,64 @@ describe("gqlClientFetch", () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
});

it("should use 'omit' credentials when provided", async () => {
const fetcher = initClientFetcher("https://localhost/graphql", {
defaultCredentials: "omit",
});
fetchMock.mockResponse(responseString);

const gqlResponse = await fetcher(query, {
myVar: "baz",
});

expect(gqlResponse).toEqual(response);

expect(fetchMock).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
credentials: "omit",
}),
);
});

it("should use 'same-origin' credentials when provided", async () => {
const fetcher = initClientFetcher("https://localhost/graphql", {
defaultCredentials: "same-origin",
});
fetchMock.mockResponse(responseString);

const gqlResponse = await fetcher(query, {
myVar: "baz",
});

expect(gqlResponse).toEqual(response);

expect(fetchMock).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
credentials: "same-origin",
}),
);
});

it("should use 'include' credentials when provided", async () => {
const fetcher = initClientFetcher("https://localhost/graphql");
fetchMock.mockResponse(responseString);

const gqlResponse = await fetcher(query, {
myVar: "baz",
});

expect(gqlResponse).toEqual(response);

expect(fetchMock).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
credentials: "include",
}),
);
});

it("should use the provided signal", async () => {
const fetcher = initClientFetcher("https://localhost/graphql");
fetchMock.mockResponse(responseString);
Expand Down
10 changes: 8 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type Options = {
*/
includeQuery?: boolean;

/**
* Default credentials to be sent with each request
*/
defaultCredentials?: RequestCredentials;

/**
* Function to customize creating the documentId from a query
*
Expand Down Expand Up @@ -75,6 +80,7 @@ export const initClientFetcher =
defaultTimeout = 30000,
defaultHeaders = {},
includeQuery = false,
defaultCredentials = "include",
createDocumentId = getDocumentId,
}: Options = {},
): ClientFetcher =>
Expand Down Expand Up @@ -119,7 +125,7 @@ export const initClientFetcher =
fetch(url.toString(), {
headers: headers,
method: "GET",
credentials: "include",
credentials: defaultCredentials,
signal: options.signal,
}),
);
Expand All @@ -140,7 +146,7 @@ export const initClientFetcher =
headers: headers,
method: "POST",
body: createRequestBody(request),
credentials: "include",
credentials: defaultCredentials,
signal: options.signal,
}),
);
Expand Down