@shopify/hydrogen-react 0.0.0-next-8a75ef6 → 0.0.0-next-9f025d3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -200
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
# Hydrogen React
|
|
4
4
|
|
|
5
|
-
Hydrogen React
|
|
5
|
+
Hydrogen React is an unopionated and performant library of Shopify-specific commerce components, hooks, and utilities. Bring the best parts of Hydrogen to more React frameworks, like Next.js and Gatsby, and accelerate headless development using Shopify’s pre-built React components including Cart, Shop Pay, and Shopify Analytics.
|
|
6
|
+
|
|
7
|
+
📚 [Overview](https://shopify.dev/custom-storefronts/hydrogen-react) | 🛠️ [Docs](https://shopify.dev/docs/api/hydrogen-react) | 🛍️ [Custom Storefronts at Shopify](https://shopify.dev/custom-storefronts) | 🗣 [Discord](https://discord.gg/Hefq6w5c5d) | 📝 [Changelog](https://github.com/Shopify/hydrogen-react/blob/main/packages/react/CHANGELOG.md)
|
|
6
8
|
|
|
7
9
|
**IMPORTANT:** Refer to how this package is [versioned](../../README.md#versioning).
|
|
8
10
|
|
|
@@ -13,12 +15,11 @@ Hydrogen React provides React components, reusable functions, and utilities for
|
|
|
13
15
|
This document contains the following topics:
|
|
14
16
|
|
|
15
17
|
- [Getting started with Hydrogen React](#getting-started)
|
|
16
|
-
- [
|
|
18
|
+
- [Improving the Developer Experience](#improving-the-developer-experience)
|
|
17
19
|
- [Development and production bundles](#development-and-production-bundles)
|
|
18
20
|
- [Hydrogen React in the browser](#hydrogen-react-in-the-browser)
|
|
19
|
-
- [Enabling autocompletion for the Storefront API](#enable-storefront-api-graphql-autocompletion)
|
|
20
|
-
- [Setting TypeScript types for Storefront API objects](#typescript)
|
|
21
21
|
- [Troubleshooting](#troubleshooting)
|
|
22
|
+
- [Contributing](#contributing)
|
|
22
23
|
|
|
23
24
|
## Getting started
|
|
24
25
|
|
|
@@ -36,107 +37,16 @@ This document contains the following topics:
|
|
|
36
37
|
yarn add @shopify/hydrogen-react
|
|
37
38
|
```
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
To make it easier to query the Storefront API, Hydrogen React exposes a helper function called `createStorefrontClient()`.
|
|
42
|
-
|
|
43
|
-
The client can take in the following tokens:
|
|
44
|
-
|
|
45
|
-
- **[Delegate access](https://shopify.dev/api/usage/authentication#getting-started-with-authenticated-access)**: Used for requests from a server or other private context. Set as `privateStorefrontToken`.
|
|
46
|
-
|
|
47
|
-
- **[Public](https://shopify.dev/api/usage/authentication#getting-started-with-public-access)**: Used for requests from a browser or other public context. Set as `publicAccessToken`.
|
|
48
|
-
|
|
49
|
-
The following is an example:
|
|
50
|
-
|
|
51
|
-
```ts
|
|
52
|
-
// Filename: '/shopify-client.js'
|
|
53
|
-
|
|
54
|
-
import {createStorefrontClient} from '@shopify/hydrogen-react';
|
|
55
|
-
|
|
56
|
-
const client = createStorefrontClient({
|
|
57
|
-
privateStorefrontToken: '...',
|
|
58
|
-
storeDomain: 'myshop',
|
|
59
|
-
storefrontApiVersion: '2023-01',
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
export const getStorefrontApiUrl = client.getStorefrontApiUrl;
|
|
63
|
-
export const getPrivateTokenHeaders = client.getPrivateTokenHeaders;
|
|
64
|
-
export const getShopifyDomain = client.getShopifyDomain;
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
You can then use this in your server-side queries. Here's an example of using it for [NextJS's `getServerSideProps`](https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props):
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
// Filename: '/pages/index.js'
|
|
71
|
-
|
|
72
|
-
import {
|
|
73
|
-
getStorefrontApiUrl,
|
|
74
|
-
getPrivateTokenHeaders,
|
|
75
|
-
} from '../shopify-client.js';
|
|
76
|
-
|
|
77
|
-
export async function getServerSideProps() {
|
|
78
|
-
const response = await fetch(getStorefrontApiUrl(), {
|
|
79
|
-
body: GRAPHQL_QUERY,
|
|
80
|
-
headers: getPrivateTokenHeaders({buyerIp: '...'}),
|
|
81
|
-
method: 'POST',
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
const json = await response.json();
|
|
40
|
+
Browse our [overview](https://shopify.dev/custom-storefronts/hydrogen-react) and [docs](https://shopify.dev/docs/api/hydrogen-react) to learn more.
|
|
85
41
|
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
```
|
|
42
|
+
## Improving the Developer Experience
|
|
89
43
|
|
|
90
|
-
|
|
44
|
+
`hydrogen-react` includes several tools that improve the developer experience, such as:
|
|
91
45
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
export default function handler(req, res) {
|
|
97
|
-
fetch(getShopifyDomain() + '/products', {
|
|
98
|
-
headers: {
|
|
99
|
-
/** forward some of the headers from the original request **/
|
|
100
|
-
},
|
|
101
|
-
})
|
|
102
|
-
.then((resp) => resp.text())
|
|
103
|
-
.then((text) => res.status(200).send(text))
|
|
104
|
-
.catch((error) => {
|
|
105
|
-
console.error(
|
|
106
|
-
`Error proxying the online store: ${
|
|
107
|
-
error instanceof Error ? error.stack : error
|
|
108
|
-
}`
|
|
109
|
-
);
|
|
110
|
-
res.status(500).send('Server error occurred');
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### (Optional) Set the content type for the Storefront client
|
|
116
|
-
|
|
117
|
-
By default, the Storefront client sends the `"content-type": "application/json"` header. Use the `json` content type when you have GraphQL variables and when the body is an object with the following shape:
|
|
118
|
-
|
|
119
|
-
```json
|
|
120
|
-
{
|
|
121
|
-
"query": "...",
|
|
122
|
-
"operationName": "...",
|
|
123
|
-
"variables": { "myVariable": "someValue", ... }
|
|
124
|
-
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
However, when the body is only a query string, such as `{"..."}`, you can optionally change the default header to `application/graphql`:
|
|
128
|
-
|
|
129
|
-
```ts
|
|
130
|
-
createStorefrontClient({contentType: 'graphql', ...})
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
Alternatively, each time you get the headers you can customize which `"content-type"` you want, for only that one invocation:
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
getPrivateTokenHeaders({contentType: 'graphql'});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
**Note:** If you're using TypeScript, then you can [improve the typing experience](#set-typescript-types).
|
|
46
|
+
- Creating a [storefront client](https://shopify.dev/docs/api/hydrogen-react/utilities/createstorefrontclient) to easily make API requests to the Storefront API
|
|
47
|
+
- Enabling [GraphQL validation and autocompletion](https://shopify.dev/docs/api/hydrogen-react/utilities/storefront-schema) for the Storefront API
|
|
48
|
+
- Using the pre-built [TypeScript types for the Storefront API](https://shopify.dev/docs/api/hydrogen-react/utilities/storefront-api-types)
|
|
49
|
+
- Correctly typing the Storefront API's custom scalars [when using GraphQL Codegen](https://shopify.dev/docs/api/hydrogen-react/utilities/storefrontapicustomscalars) and TypeScript
|
|
140
50
|
|
|
141
51
|
## Development and production bundles
|
|
142
52
|
|
|
@@ -152,104 +62,6 @@ Hydrogen React has a development `umd` build and a production `umd` build. Both
|
|
|
152
62
|
|
|
153
63
|
If you're using Hydrogen React as a global through the `<script>` tag, then the components can be accessed through the `hydrogenreact` global variable.
|
|
154
64
|
|
|
155
|
-
## Enable Storefront API GraphQL autocompletion
|
|
156
|
-
|
|
157
|
-
To improve your development experience, enable GraphQL autocompletion for the Storefront API in your integrated development environment (IDE).
|
|
158
|
-
|
|
159
|
-
1. Add [`graphql`](https://www.npmjs.com/package/graphql) and [GraphQL-config](https://www.graphql-config.com/docs/user/user-installation) with the following command:
|
|
160
|
-
|
|
161
|
-
```bash
|
|
162
|
-
yarn add --dev graphql graphql-config
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
1. Create a [GraphQL config file](https://www.graphql-config.com/docs/user/user-usage) at the root of your code. For example, `.graphqlrc.yml`.
|
|
166
|
-
1. Add a [`schema`](https://www.graphql-config.com/docs/user/user-schema) and point it to Hydrogen React's bundled schema for the Storefront API.
|
|
167
|
-
|
|
168
|
-
For example:
|
|
169
|
-
|
|
170
|
-
```yml
|
|
171
|
-
# Filename: .graphqlrc.yml
|
|
172
|
-
schema: node_modules/@shopify/hydrogen-react/storefront.schema.json
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
1. Install a GraphQL extension in your IDE, such as the [GraphQL extension for VSCode](https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql).
|
|
176
|
-
|
|
177
|
-
GraphQL autocompletion and validation will now work in `.graphql` files and in [`gql`](https://github.com/apollographql/graphql-tag) template literals!
|
|
178
|
-
|
|
179
|
-
If you're having trouble getting it to work, then consult our [troubleshooting section](#graphql-autocompletion).
|
|
180
|
-
|
|
181
|
-
## TypeScript
|
|
182
|
-
|
|
183
|
-
Improve your development experience by using Hydrogen React's generated Types and helpers.
|
|
184
|
-
|
|
185
|
-
### Storefront API types
|
|
186
|
-
|
|
187
|
-
Hydrogen React ships with generated TypeScript types that match the Storefront API and its objects. Import them from the `/storefront-api-types` package path:
|
|
188
|
-
|
|
189
|
-
```ts
|
|
190
|
-
import type {Product} from '@shopify/hydrogen-react/storefront-api-types';
|
|
191
|
-
|
|
192
|
-
const product = {} satisfies Product;
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
You can also use TypeScript's built-in helpers to create your own Types to fit your needs:
|
|
196
|
-
|
|
197
|
-
```ts
|
|
198
|
-
const partialProduct = {} satisfies Partial<Product>;
|
|
199
|
-
|
|
200
|
-
const productTitle: Pick<Product, 'title'> = '';
|
|
201
|
-
|
|
202
|
-
const productExceptTitle = {} satisfies Omit<Product, 'title'>;
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
### GraphQL CodeGen
|
|
206
|
-
|
|
207
|
-
To use GraphQL CodeGen, follow [their guide](https://the-guild.dev/graphql/codegen/docs/getting-started/installation) to get started. Then, when you have a `codegen.ts` file, you can modify the following lines in the codegen object to improve the CodgeGen experience:
|
|
208
|
-
|
|
209
|
-
```ts
|
|
210
|
-
import {storefrontApiCustomScalars} from '@shopify/hydrogen-react';
|
|
211
|
-
|
|
212
|
-
const config: CodegenConfig = {
|
|
213
|
-
// Use the schema that's bundled with @shopify/hydrogen-react
|
|
214
|
-
schema: './node_modules/@shopify/hydrogen-react/storefront.schema.json',
|
|
215
|
-
generates: {
|
|
216
|
-
'./gql/': {
|
|
217
|
-
preset: 'client',
|
|
218
|
-
plugins: [],
|
|
219
|
-
config: {
|
|
220
|
-
// Use the custom scalar definitions that @shopify/hydrogen-react provides to improve the types
|
|
221
|
-
scalars: storefrontApiCustomScalars,
|
|
222
|
-
},
|
|
223
|
-
},
|
|
224
|
-
},
|
|
225
|
-
};
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
### The `StorefrontApiResponseError` and `StorefrontApiResponseOk` helpers
|
|
229
|
-
|
|
230
|
-
The following is an example:
|
|
231
|
-
|
|
232
|
-
```tsx
|
|
233
|
-
import {
|
|
234
|
-
type StorefrontApiResponseError,
|
|
235
|
-
type StorefrontApiResponseOk,
|
|
236
|
-
} from '@shopify/hydrogen-react';
|
|
237
|
-
|
|
238
|
-
async function FetchApi<DataGeneric>() {
|
|
239
|
-
const apiResponse = await fetch('...');
|
|
240
|
-
|
|
241
|
-
if (!apiResponse.ok) {
|
|
242
|
-
// 400 or 500 level error
|
|
243
|
-
return (await apiResponse.text()) as StorefrontApiResponseError; // or apiResponse.json()
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const graphqlResponse: StorefrontApiResponseOk<DataGeneric> =
|
|
247
|
-
await apiResponse.json();
|
|
248
|
-
|
|
249
|
-
// You can now access 'graphqlResponse.data' and 'graphqlResponse.errors'
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
65
|
## Troubleshooting
|
|
254
66
|
|
|
255
67
|
The following will help you troubleshoot common problems in this version of Hydrogen React.
|
|
@@ -263,3 +75,9 @@ For example, in VSCode do the following:
|
|
|
263
75
|
1. Open the [command palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette).
|
|
264
76
|
1. Type `graphql`.
|
|
265
77
|
1. Select `VSCode GraphQL: Manual Restart`.
|
|
78
|
+
|
|
79
|
+
## Contributing
|
|
80
|
+
|
|
81
|
+
We love contributions! Contributing works best when you first confirm that something needs to be changed or fixed; please open an issue, start a discussion, or talk to us in Discord!
|
|
82
|
+
|
|
83
|
+
PRs are welcome! Be sure to read the [CONTRIBUTING.md](../../CONTRIBUTING.md) for an overview and guidelines to help your PR succeed.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shopify/hydrogen-react",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-9f025d3",
|
|
4
4
|
"description": "React components, hooks, and utilities for creating custom Shopify storefronts",
|
|
5
5
|
"homepage": "https://github.com/Shopify/hydrogen-react/tree/main/packages/react",
|
|
6
6
|
"license": "MIT",
|