Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source header to API calls #5305

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/twenty-apples-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Dashboard now sends source header to API
17 changes: 15 additions & 2 deletions src/components/DevModePanel/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ describe("getFetcher", () => {
expect(mockCreateGraphiQLFetcher).toHaveBeenCalledWith({
url: mockApiUrl,
fetch: fetch,
headers: {
"source-service-name": "saleor.dashboard.playground",
},
});
});

it("should return fetcher with fetch when Authorization-Bearer header present", () => {
// Arrange
const opts: FetcherOpts = {
headers: { "Authorization-Bearer": "token" },
headers: {
"Authorization-Bearer": "token",
},
};

// Act
Expand All @@ -74,13 +79,18 @@ describe("getFetcher", () => {
expect(mockCreateGraphiQLFetcher).toHaveBeenCalledWith({
url: mockApiUrl,
fetch: fetch,
headers: {
"source-service-name": "saleor.dashboard.playground",
},
});
});

it("should return fetcher with fetch when lowercase header present", () => {
// Arrange
const opts: FetcherOpts = {
headers: { "authorization-bearer": "token" },
headers: {
"authorization-bearer": "token",
},
};

// Act
Expand All @@ -90,6 +100,9 @@ describe("getFetcher", () => {
expect(mockCreateGraphiQLFetcher).toHaveBeenCalledWith({
url: mockApiUrl,
fetch: fetch,
headers: {
"source-service-name": "saleor.dashboard.playground",
},
});
});
});
3 changes: 3 additions & 0 deletions src/components/DevModePanel/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ export const getFetcher = (opts: FetcherOpts) => {
return createGraphiQLFetcher({
url: process.env.API_URL as string,
fetch: httpFetch as typeof fetch,
headers: {
"source-service-name": "saleor.dashboard.playground",
},
});
};
15 changes: 11 additions & 4 deletions src/graphql/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import { getApiUrl } from "../config";
import introspectionQueryResultData from "./fragmentTypes.generated";
import { TypedTypePolicies } from "./typePolicies.generated";

const attachVariablesLink = new ApolloLink((operation, forward) =>
forward(operation).map(data => ({
const attachVariablesLink = new ApolloLink((operation, forward) => {
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
"source-service-name": "saleor.dashboard",
},
}));

return forward(operation).map(data => ({
...data,
extensions: {
...data.extensions,
variables: operation.variables,
},
})),
);
}));
});

export const link = attachVariablesLink.concat(
createUploadLink({
Expand Down
Loading