Skip to content

Commit 94fdddd

Browse files
frehnerrennyG
andauthored
Add our scalars to the package for other developers (#69)
* Add our scalars to the package for other developers * Update .changeset/swift-olives-beam.md Co-authored-by: Ren Chaturvedi <[email protected]> * Update .changeset/swift-olives-beam.md Co-authored-by: Ren Chaturvedi <[email protected]> * Update apps/nextjs/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update apps/nextjs/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update apps/nextjs/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/README.md Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/README.md Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/codegen.ts Co-authored-by: Ren Chaturvedi <[email protected]> * Update packages/react/src/codegen.helpers.ts Co-authored-by: Ren Chaturvedi <[email protected]> Co-authored-by: Ren Chaturvedi <[email protected]>
1 parent ccfbbbd commit 94fdddd

11 files changed

+143
-51
lines changed

Diff for: .changeset/swift-olives-beam.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@shopify/hydrogen-react': patch
3+
---
4+
5+
Provide a mapping of Storefront API's custom scalars to their actual types, for use with GraphQL CodeGen.
6+
7+
For example:
8+
9+
```ts
10+
import {storefrontApiCustomScalars} from '@shopify/hydrogen-react';
11+
12+
const config: CodegenConfig = {
13+
// Use the schema that's bundled with @shopify/hydrogen-react
14+
schema: './node_modules/@shopify/hydrogen-react/storefront.schema.json',
15+
generates: {
16+
'./gql/': {
17+
preset: 'client',
18+
plugins: [],
19+
config: {
20+
// Use the custom scalar definitions that @shopify/hydrogen-react provides to improve the custom scalar types
21+
scalars: storefrontApiCustomScalars,
22+
},
23+
},
24+
},
25+
};
26+
```

Diff for: apps/nextjs/codegen.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import type {CodegenConfig} from '@graphql-codegen/cli';
2+
// Due to issues with TurboRepo and dev dependencies, we can't use this in the monorepo. But if we could, then it would look like this import, and then used below in "scalars"
3+
// import {storefrontApiCustomScalars} from '@shopify/hydrogen-react';
24

35
const config: CodegenConfig = {
46
overwrite: true,
5-
// a normal app would only need `./node_modules/...` but we're in a monorepo
7+
// A normal app would only need `./node_modules/...` but we're in a monorepo
68
schema: '../../node_modules/@shopify/hydrogen-react/storefront.schema.json',
79
documents: 'pages/**/*.tsx',
8-
// @TODO a chance here to provide the settings we use to generate our Types for the schema here as well,
9-
// so that the Custom Scalar definitions we have in hydrogen-ui can be used here as well.
1010
generates: {
1111
'./gql/': {
1212
preset: 'client',
1313
plugins: [],
14+
config: {
15+
// scalars: storefrontApiCustomScalars,
16+
scalars: {
17+
// Because of the limitations outlined above, these need to be kept in sync with the original definitions in the @shopify/hydrogen-react repo!
18+
DateTime: 'string',
19+
Decimal: 'string',
20+
HTML: 'string',
21+
URL: 'string',
22+
Color: 'string',
23+
UnsignedInt64: 'string',
24+
},
25+
},
1426
},
1527
},
1628
};

Diff for: apps/nextjs/gql/graphql.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ export type Scalars = {
1818
* For example, "#6A8D48".
1919
*
2020
*/
21-
Color: any;
21+
Color: string;
2222
/**
2323
* Represents an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)-encoded date and time string.
2424
* For example, 3:50 pm on September 7, 2019 in the time zone of UTC (Coordinated Universal Time) is
2525
* represented as `"2019-09-07T15:50:00Z`".
2626
*
2727
*/
28-
DateTime: any;
28+
DateTime: string;
2929
/**
3030
* A signed decimal number, which supports arbitrary precision and is serialized as a string.
3131
*
3232
* Example values: `"29.99"`, `"29.999"`.
3333
*
3434
*/
35-
Decimal: any;
35+
Decimal: string;
3636
/**
3737
* A string containing HTML code. Refer to the [HTML spec](https://html.spec.whatwg.org/#elements-3) for a
3838
* complete list of HTML elements.
3939
*
4040
* Example value: `"<p>Grey cotton knit sweater.</p>"`.
4141
*
4242
*/
43-
HTML: any;
43+
HTML: string;
4444
/**
4545
* A [JSON](https://www.json.org/json-en.html) object.
4646
*
@@ -66,14 +66,14 @@ export type Scalars = {
6666
* (`johns-apparel.myshopify.com`).
6767
*
6868
*/
69-
URL: any;
69+
URL: string;
7070
/**
7171
* An unsigned 64-bit integer. Represents whole numeric values between 0 and 2^64 - 1 encoded as a string of base-10 digits.
7272
*
7373
* Example value: `"50"`.
7474
*
7575
*/
76-
UnsignedInt64: any;
76+
UnsignedInt64: string;
7777
};
7878

7979
/**
@@ -6845,7 +6845,7 @@ export enum WeightUnit {
68456845
export type IndexQueryQueryVariables = Exact<{ [key: string]: never; }>;
68466846

68476847

6848-
export type IndexQueryQuery = { __typename?: 'QueryRoot', shop: { __typename?: 'Shop', name: string }, products: { __typename?: 'ProductConnection', nodes: Array<{ __typename?: 'Product', id: string, title: string, publishedAt: any, handle: string, variants: { __typename?: 'ProductVariantConnection', nodes: Array<{ __typename?: 'ProductVariant', id: string, image?: { __typename?: 'Image', url: any, altText?: string | null, width?: number | null, height?: number | null } | null }> } }> } };
6848+
export type IndexQueryQuery = { __typename?: 'QueryRoot', shop: { __typename?: 'Shop', name: string }, products: { __typename?: 'ProductConnection', nodes: Array<{ __typename?: 'Product', id: string, title: string, publishedAt: string, handle: string, variants: { __typename?: 'ProductVariantConnection', nodes: Array<{ __typename?: 'ProductVariant', id: string, image?: { __typename?: 'Image', url: string, altText?: string | null, width?: number | null, height?: number | null } | null }> } }> } };
68496849

68506850

68516851
export const IndexQueryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"IndexQuery"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"shop"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}}]}},{"kind":"Field","name":{"kind":"Name","value":"products"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"first"},"value":{"kind":"IntValue","value":"1"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nodes"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"publishedAt"}},{"kind":"Field","name":{"kind":"Name","value":"handle"}},{"kind":"Field","name":{"kind":"Name","value":"variants"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"first"},"value":{"kind":"IntValue","value":"1"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nodes"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"image"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"url"}},{"kind":"Field","name":{"kind":"Name","value":"altText"}},{"kind":"Field","name":{"kind":"Name","value":"width"}},{"kind":"Field","name":{"kind":"Name","value":"height"}}]}}]}}]}}]}}]}}]}}]} as unknown as DocumentNode<IndexQueryQuery, IndexQueryQueryVariables>;

Diff for: packages/react/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ If you're having trouble getting it to work, then consult our [troubleshooting s
154154

155155
Improve your development experience by adding strong typing to Storefront API responses. The following are some options for doing this.
156156

157+
## GraphQL CodeGen
158+
159+
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:
160+
161+
```ts
162+
import {storefrontApiCustomScalars} from '@shopify/hydrogen-react';
163+
164+
const config: CodegenConfig = {
165+
// Use the schema that's bundled with @shopify/hydrogen-react
166+
schema: './node_modules/@shopify/hydrogen-react/storefront.schema.json',
167+
generates: {
168+
'./gql/': {
169+
preset: 'client',
170+
plugins: [],
171+
config: {
172+
// Use the custom scalar definitions that @shopify/hydrogen-react provides to improve the types
173+
scalars: storefrontApiCustomScalars,
174+
},
175+
},
176+
},
177+
};
178+
```
179+
157180
### Use the `StorefrontApiResponseError` and `StorefrontApiResponseOk` helpers
158181

159182
The following is an example:

Diff for: packages/react/codegen.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {CodegenConfig} from '@graphql-codegen/cli';
2+
// Because this file is processed only in TypeScript, we can make one exception for not using an extension here.
3+
// eslint-disable-next-line import/extensions
4+
import {storefrontApiCustomScalars} from './src/codegen.helpers';
5+
6+
const config: CodegenConfig = {
7+
overwrite: true,
8+
schema: {
9+
'https://hydrogen-preview.myshopify.com/api/2022-10/graphql.json': {
10+
headers: {
11+
'X-Shopify-Storefront-Access-Token': '3b580e70970c4528da70c98e097c2fa0',
12+
'content-type': 'application/json',
13+
},
14+
},
15+
},
16+
generates: {
17+
// The generated base types
18+
'src/storefront-api-types.d.ts': {
19+
plugins: [
20+
{
21+
add: {
22+
content: `
23+
/**
24+
* THIS FILE IS AUTO-GENERATED, DO NOT EDIT
25+
* Based on Storefront API 2022-10
26+
* If changes need to happen to the types defined in this file, then generally the Storefront API needs to update. After it's updated, you can run \`yarn graphql-types\`.
27+
* Except custom Scalars, which are defined in the \`codegen.ts\` file
28+
*/
29+
/* eslint-disable */`,
30+
},
31+
},
32+
{
33+
typescript: {
34+
useTypeImports: true,
35+
// If a default type for a scalar isn't set, then instead of 'any' we set to 'unknown' for better type safety.
36+
defaultScalarType: 'unknown',
37+
useImplementingTypes: true,
38+
enumsAsTypes: true,
39+
// Define how the Storefront API's custom scalars map to TypeScript types
40+
scalars: storefrontApiCustomScalars,
41+
},
42+
},
43+
],
44+
},
45+
// The schema file, which is the local representation of the GraphQL endpoint
46+
'./storefront.schema.json': {
47+
plugins: [
48+
{
49+
introspection: {
50+
minify: true,
51+
},
52+
},
53+
],
54+
},
55+
},
56+
};
57+
58+
export default config;

Diff for: packages/react/codegen.yml

-38
This file was deleted.

Diff for: packages/react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"build:tsc:es": "tsc --emitDeclarationOnly --project tsconfig.typeoutput.json",
9292
"copy-storefront-types": "cpy ./src/storefront-api-types.d.ts ./dist/types/ --flat",
9393
"format": "prettier --write \"src/**/*\"",
94-
"graphql-types": "graphql-codegen --config codegen.yml && yarn format",
94+
"graphql-types": "graphql-codegen --config codegen.ts && yarn format",
9595
"prepack": "yarn build",
9696
"test": "vitest",
9797
"test:ci": "vitest run --coverage",

Diff for: packages/react/src/AddToCartButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {useCart} from './CartProvider.js';
44
import {useProduct} from './ProductProvider.js';
55
import {BaseButton, BaseButtonProps} from './BaseButton.js';
66

7-
interface AddToCartButtonProps {
7+
export interface AddToCartButtonProps {
88
/** An array of cart line attributes that belong to the item being added to the cart. */
99
attributes?: {
1010
key: string;

Diff for: packages/react/src/codegen.helpers.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** Meant to be used with GraphQL CodeGen to type the Storefront API's custom scalars correctly */
2+
export const storefrontApiCustomScalars = {
3+
// Keep in sync with the definitions in the app/nextjs/codegen.ts!
4+
DateTime: 'string',
5+
Decimal: 'string',
6+
HTML: 'string',
7+
URL: 'string',
8+
Color: 'string',
9+
UnsignedInt64: 'string',
10+
};

Diff for: packages/react/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type {
99
} from './cart-types.js';
1010
export {CartCheckoutButton} from './CartCheckoutButton.js';
1111
export {CartProvider, useCart} from './CartProvider.js';
12+
export {storefrontApiCustomScalars} from './codegen.helpers.js';
1213
export {ExternalVideo} from './ExternalVideo.js';
1314
export {flattenConnection} from './flatten-connection.js';
1415
export {Image} from './Image.js';

Diff for: packages/react/src/storefront-api-types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* THIS FILE IS AUTO-GENERATED, DO NOT EDIT
33
* Based on Storefront API 2022-10
44
* If changes need to happen to the types defined in this file, then generally the Storefront API needs to update, and then you can run `yarn graphql-types`
5-
* Except custom Scalars, which are defined in the `codegen.yml` file
5+
* Except custom Scalars, which are defined in the `codegen.ts` file
66
*/
77
/* eslint-disable */
88
export type Maybe<T> = T | null;

0 commit comments

Comments
 (0)