Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Add default SDK headers to graphql-client
Browse files Browse the repository at this point in the history
  • Loading branch information
melissaluu committed Feb 28, 2024
1 parent c3e98d2 commit b2f29ae
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-doors-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopify/graphql-client": patch
---

Add default SDK headers to all API requests when none are provided
6 changes: 6 additions & 0 deletions packages/graphql-client/src/graphql-client/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export const CONTENT_TYPES = {
json: "application/json",
multipart: "multipart/mixed",
};
export const SDK_VARIANT_HEADER = "X-SDK-Variant";
export const SDK_VERSION_HEADER = "X-SDK-Version";

export const DEFAULT_SDK_VARIANT = "shopify-graphql-client";
// This is value is replaced with package.json version during rollup build process
export const DEFAULT_CLIENT_VERSION = "ROLLUP_REPLACE_CLIENT_VERSION";

export const RETRY_WAIT_TIME = 1000;
export const RETRIABLE_STATUS_CODES = [429, 503];
Expand Down
9 changes: 9 additions & 0 deletions packages/graphql-client/src/graphql-client/graphql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
HEADER_SEPARATOR,
DEFER_OPERATION_REGEX,
BOUNDARY_HEADER_REGEX,
SDK_VARIANT_HEADER,
SDK_VERSION_HEADER,
DEFAULT_SDK_VARIANT,
DEFAULT_CLIENT_VERSION,
} from "./constants";
import {
formatErrorMessage,
Expand Down Expand Up @@ -122,6 +126,11 @@ function generateFetch(
return headers;
}, {});

if (!flatHeaders[SDK_VARIANT_HEADER] && !flatHeaders[SDK_VERSION_HEADER]) {
flatHeaders[SDK_VARIANT_HEADER] = DEFAULT_SDK_VARIANT;
flatHeaders[SDK_VERSION_HEADER] = DEFAULT_CLIENT_VERSION;
}

const fetchParams: Parameters<CustomFetchApi> = [
overrideUrl ?? url,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import fetchMock from "jest-fetch-mock";

import { GraphQLClient } from "../../types";
import {
SDK_VARIANT_HEADER,
SDK_VERSION_HEADER,
DEFAULT_CLIENT_VERSION,
DEFAULT_SDK_VARIANT,
} from "../../constants";

import { operation, variables, clientConfig, getValidClient } from "./fixtures";
import { fetchApiTests, parametersTests, retryTests } from "./common-tests";
import {
fetchApiTests,
parametersTests,
sdkHeadersTests,
retryTests,
} from "./common-tests";

describe("GraphQL Client", () => {
let mockLogger: jest.Mock;
Expand Down Expand Up @@ -35,6 +46,10 @@ describe("GraphQL Client", () => {
parametersTests(functionName);
});

describe("SDK headers", () => {
sdkHeadersTests(functionName);
});

describe("returned object", () => {
it("returns the HTTP response", async () => {
const response = await client.fetch(operation);
Expand Down Expand Up @@ -121,7 +136,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
];

Expand Down Expand Up @@ -247,7 +266,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
];

Expand Down Expand Up @@ -312,7 +335,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
],
},
Expand All @@ -327,6 +354,10 @@ describe("GraphQL Client", () => {

fetchApiTests(functionName);

describe("SDK headers", () => {
sdkHeadersTests(functionName);
});

describe("calling the function", () => {
describe("function parameters", () => {
parametersTests(functionName);
Expand Down Expand Up @@ -626,7 +657,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
];

Expand Down Expand Up @@ -767,7 +802,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
];

Expand Down Expand Up @@ -833,7 +872,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import fetchMock from "jest-fetch-mock";

import { GraphQLClient, ClientStreamResponse } from "../../types";
import {
SDK_VARIANT_HEADER,
SDK_VERSION_HEADER,
DEFAULT_CLIENT_VERSION,
DEFAULT_SDK_VARIANT,
} from "../../constants";

import {
clientConfig,
Expand All @@ -9,7 +15,12 @@ import {
createIterableBufferResponse,
createReaderStreamResponse,
} from "./fixtures";
import { fetchApiTests, parametersTests, retryTests } from "./common-tests";
import {
fetchApiTests,
parametersTests,
sdkHeadersTests,
retryTests,
} from "./common-tests";

const operation = `
query shop($country: CountryCode, $language: LanguageCode) @inContext(country: $country, language: $language) {
Expand Down Expand Up @@ -57,6 +68,10 @@ describe("GraphQL Client", () => {

fetchApiTests(functionName, operation);

describe("SDK headers", () => {
sdkHeadersTests(functionName, operation);
});

describe("calling the function", () => {
describe("fetch parameters", () => {
parametersTests("requestStream", operation);
Expand Down Expand Up @@ -1171,7 +1186,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
];

Expand Down Expand Up @@ -1333,7 +1352,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
];

Expand Down Expand Up @@ -1407,7 +1430,11 @@ describe("GraphQL Client", () => {
{
method: "POST",
body: JSON.stringify({ query: operation }),
headers: clientConfig.headers,
headers: {
...clientConfig.headers,
[SDK_VARIANT_HEADER]: DEFAULT_SDK_VARIANT,
[SDK_VERSION_HEADER]: DEFAULT_CLIENT_VERSION,
},
},
],
},
Expand Down
Loading

0 comments on commit b2f29ae

Please sign in to comment.