Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit 736cc41

Browse files
authored
Fix bug in storefront client. (#125)
Update the nextjs app to reflect that Fix issue with latest graphql codegen needing ts-node Add tests
1 parent 12b3239 commit 736cc41

File tree

13 files changed

+292
-55
lines changed

13 files changed

+292
-55
lines changed

.changeset/chilled-wombats-allow.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@shopify/storefront-kit-react': patch
3+
---
4+
5+
In the version 2023.1.1 "Breaking Changes" section, we said
6+
7+
> The storefront client and ShopifyProvider now provide the `storeDomain` exactly as it is received; it's recommended that you pass the domain with the protocol and the fully-qualified domain name for your Storefront. For example: `https://hydrogen-test.myshopify.com`
8+
9+
Unfortunately, the Storefront Client wasn't fully updated to actually do that. This update corrects this bug, but also means that you need to provide a full URL to your Storefront Domain (as was originally intended in our breaking change update).

apps/nextjs/gql/fragment-masking.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export function useFragment<TType>(
3131
export function useFragment<TType>(
3232
_documentNode: DocumentNode<TType, any>,
3333
fragmentType: ReadonlyArray<FragmentType<DocumentNode<TType, any>>> | null | undefined
34-
): ReadonlyArray<TType> | null | undefined
34+
): ReadonlyArray<TType> | null | undefined;
3535
export function useFragment<TType>(
3636
_documentNode: DocumentNode<TType, any>,
3737
fragmentType: FragmentType<DocumentNode<TType, any>> | ReadonlyArray<FragmentType<DocumentNode<TType, any>>> | null | undefined
3838
): TType | ReadonlyArray<TType> | null | undefined {
39-
return fragmentType as any
39+
return fragmentType as any;
4040
}
4141

4242

apps/nextjs/gql/gql.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ const documents = {
1919
"\n query Search($searchTerm: String) {\n products(first: 1, sortKey: RELEVANCE, query: $searchTerm) {\n pageInfo {\n startCursor\n endCursor\n hasNextPage\n hasPreviousPage\n }\n }\n }\n": types.SearchDocument,
2020
};
2121

22+
/**
23+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
24+
*
25+
*
26+
* @example
27+
* ```ts
28+
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
29+
* ```
30+
*
31+
* The query argument is unknown!
32+
* Please regenerate the types.
33+
*/
34+
export function graphql(source: string): unknown;
35+
2236
/**
2337
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
2438
*/
@@ -36,20 +50,6 @@ export function graphql(source: "\n query Product {\n shop {\n name\n
3650
*/
3751
export function graphql(source: "\n query Search($searchTerm: String) {\n products(first: 1, sortKey: RELEVANCE, query: $searchTerm) {\n pageInfo {\n startCursor\n endCursor\n hasNextPage\n hasPreviousPage\n }\n }\n }\n"): (typeof documents)["\n query Search($searchTerm: String) {\n products(first: 1, sortKey: RELEVANCE, query: $searchTerm) {\n pageInfo {\n startCursor\n endCursor\n hasNextPage\n hasPreviousPage\n }\n }\n }\n"];
3852

39-
/**
40-
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
41-
*
42-
*
43-
* @example
44-
* ```ts
45-
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
46-
* ```
47-
*
48-
* The query argument is unknown!
49-
* Please regenerate the types.
50-
**/
51-
export function graphql(source: string): unknown;
52-
5353
export function graphql(source: string) {
5454
return (documents as any)[source] ?? {};
5555
}

apps/nextjs/gql/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from "./gql"
2-
export * from "./fragment-masking"
1+
export * from "./gql";
2+
export * from "./fragment-masking";

apps/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"react-dom": "18.2.0"
2020
},
2121
"devDependencies": {
22-
"@graphql-codegen/cli": "2.16.4",
22+
"@graphql-codegen/cli": "2.16.3",
2323
"@graphql-codegen/client-preset": "1.2.6",
2424
"@types/node": "18.11.18",
2525
"@types/react": "18.0.26",

apps/nextjs/pages/_app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default function App({Component, pageProps}: AppProps) {
5656
return (
5757
<ShopifyProvider
5858
shopifyConfig={{
59-
storeDomain: `hydrogen-preview`,
59+
storeDomain: `https://hydrogen-preview.myshopify.com`,
6060
storefrontToken: '3b580e70970c4528da70c98e097c2fa0',
6161
storefrontApiVersion: '2023-01',
6262
locale: 'EN-US',

apps/nextjs/src/shopify-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {createStorefrontClient} from '@shopify/storefront-kit-react';
22

33
export const shopClient = createStorefrontClient({
4-
storeDomain: 'hydrogen-preview',
4+
storeDomain: 'https://hydrogen-preview.myshopify.com',
55
// TODO: convert to 'privateStorefrontToken'!
66
publicStorefrontToken: '3b580e70970c4528da70c98e097c2fa0',
77
storefrontApiVersion: '2023-01',

packages/react/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# React Storefront Kit
44

5-
React Storefront Kit provides React components, reusable functions, and utilities for interacting with the Storefront API.
5+
React Storefront Kit provides React components, reusable functions, and utilities for interacting with the Storefront API.
66

77
**IMPORTANT:** Refer to how this package is [versioned](../../README.md#versioning).
88

packages/react/src/ShopifyProvider.test.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,67 @@ describe('<ShopifyProvider/>', () => {
169169
).toBe('https://test.myshopify.com');
170170
});
171171
});
172+
173+
describe(`getStorefrontApiUrl`, () => {
174+
it(`generates the API URL`, () => {
175+
const {result} = renderHook(() => useShop(), {
176+
wrapper: ({children}) => (
177+
<ShopifyProvider
178+
shopifyConfig={{
179+
...SHOPIFY_CONFIG,
180+
storeDomain: 'https://notashop.myshopify.com',
181+
}}
182+
>
183+
{children}
184+
</ShopifyProvider>
185+
),
186+
});
187+
188+
expect(result.current.getStorefrontApiUrl()).toBe(
189+
'https://notashop.myshopify.com/api/2023-01/graphql.json'
190+
);
191+
});
192+
193+
it(`allows overrides`, () => {
194+
const {result} = renderHook(() => useShop(), {
195+
wrapper: ({children}) => (
196+
<ShopifyProvider
197+
shopifyConfig={{
198+
...SHOPIFY_CONFIG,
199+
storeDomain: 'https://notashop.myshopify.com',
200+
}}
201+
>
202+
{children}
203+
</ShopifyProvider>
204+
),
205+
});
206+
207+
expect(
208+
result.current.getStorefrontApiUrl({
209+
storeDomain: 'https://test.myshopify.com',
210+
})
211+
).toBe('https://test.myshopify.com/api/2023-01/graphql.json');
212+
});
213+
214+
it(`handles when a '/' is at the end of the url and doesn't add an extra one`, () => {
215+
const {result} = renderHook(() => useShop(), {
216+
wrapper: ({children}) => (
217+
<ShopifyProvider
218+
shopifyConfig={{
219+
...SHOPIFY_CONFIG,
220+
storeDomain: 'https://notashop.myshopify.com/',
221+
}}
222+
>
223+
{children}
224+
</ShopifyProvider>
225+
),
226+
});
227+
228+
expect(result.current.getStorefrontApiUrl()).toBe(
229+
'https://notashop.myshopify.com/api/2023-01/graphql.json'
230+
);
231+
});
232+
});
172233
});
173234

174235
export function getShopifyConfig(

packages/react/src/ShopifyProvider.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,12 @@ export function ShopifyProvider({
6565
},
6666
getShopifyDomain,
6767
getStorefrontApiUrl(overrideProps) {
68-
if (overrideProps?.storeDomain?.includes('.myshopify.com')) {
69-
if (__HYDROGEN_DEV__) {
70-
console.warn(
71-
`<ShopifyProvider/>: passing a 'storeDomain' prop that includes '.myshopify.com' will be unsupported in the future. Passing only the subdomain (for example, if the URL is 'test.myshopify.com', passing in 'test') will be the supported way going forward.`
72-
);
73-
}
74-
}
75-
return `${getShopifyDomain({
68+
const finalDomainUrl = getShopifyDomain({
7669
storeDomain: overrideProps?.storeDomain ?? shopifyConfig.storeDomain,
77-
})}/api/${
70+
});
71+
return `${finalDomainUrl}${
72+
finalDomainUrl.endsWith('/') ? '' : '/'
73+
}api/${
7874
overrideProps?.storefrontApiVersion ??
7975
shopifyConfig.storefrontApiVersion
8076
}/graphql.json`;

0 commit comments

Comments
 (0)