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 experimental codegen support to CLI #707

Merged
merged 59 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
5783598
Infer return and variable types from query string argument
frandiox Mar 20, 2023
23f2cf5
Make arguments optional if variables are not required
frandiox Mar 20, 2023
9bdfb0e
Force optional arguments when not using codegen
frandiox Mar 20, 2023
37b2105
Ignore country and language when checking optional arguments
frandiox Mar 20, 2023
12768a4
New @shopify/hydrogen-codegen package
frandiox Mar 20, 2023
2dabe78
Add vendor patch for graphql-tag-pluck
frandiox Mar 20, 2023
9df43e4
Remove unused deps and add comments
frandiox Mar 20, 2023
72e13c1
Add codegen to hello-world template
frandiox Mar 20, 2023
a1e2a0a
Remove codegen from generated JS templates
frandiox Mar 20, 2023
a54cf1b
Use Pick instead of primitives for generated operations
frandiox Mar 22, 2023
05dd759
Remove unnecessary properties in package.json
frandiox Mar 22, 2023
6080a2e
Add comments and rename some generics
frandiox Mar 22, 2023
5745030
Simplify storefront types for codegen
frandiox Mar 22, 2023
6ba905c
Add createRequire in ESM build
frandiox Mar 22, 2023
b4917ff
Fix typecheck
frandiox Mar 22, 2023
f7422a7
Disable tests for now
frandiox Mar 22, 2023
818e42f
Prettier readme
frandiox Mar 22, 2023
99db3a7
Use any by default for the return type
frandiox Mar 23, 2023
59db3e1
Adjust new lines and exports
frandiox Mar 23, 2023
b07cafe
Check custom plugins and export hydrogen plugin
frandiox Mar 23, 2023
d606f8c
wip
frandiox Mar 28, 2023
e590e22
Merge branch '2023-01' into fd-codegen
frandiox Mar 31, 2023
3c41972
Update visitor implementation
frandiox Apr 3, 2023
9acb5b8
Merge branch '2023-01' into fd-codegen
frandiox Apr 12, 2023
0ea484d
Merge branch '2023-04' into fd-codegen
frandiox May 12, 2023
f255917
Make fragment and query names unique in demo-store
frandiox May 12, 2023
653e684
Change namespace and import path
frandiox May 12, 2023
c51762b
Better separate plugin from preset
frandiox May 12, 2023
03317a2
Log error
frandiox May 12, 2023
31b829a
Fix generated code order
frandiox May 12, 2023
9b4e85a
Separate preset from utilities
frandiox May 12, 2023
98f0ef9
Reorganize files
frandiox May 12, 2023
9bc15b9
Fix ESM build
frandiox May 12, 2023
620e3c9
Use JSON schema from Hydrogen package
frandiox May 12, 2023
03a8c1a
Specify extensions in imports and fix CJS build
frandiox May 15, 2023
286d04f
Change graphql-tag-pluck patching strategy
frandiox May 15, 2023
c9194f6
Add experimental-codegen command
frandiox May 15, 2023
42733cc
Add codegen to dev command with a flag
frandiox May 15, 2023
f4b87a8
Fix config path flag in dev command
frandiox May 15, 2023
33e5b4c
Generate Oclif manifest
frandiox May 15, 2023
69195e0
Remove files from templates
frandiox May 15, 2023
db2aef0
Merge branch '2023-04' into fd-codegen
frandiox May 15, 2023
736b33b
Fix pluck paths during tests
frandiox May 15, 2023
a4f3396
Add unit tests
frandiox May 15, 2023
1227c30
Fix deps
frandiox May 15, 2023
f89fa01
Fix clean-all, update package-lock
frandiox May 15, 2023
cbd5ba7
Wrap require.resolve in try-catch
frandiox May 15, 2023
a83cabd
Make schema accessible without building Hydrogen first
frandiox May 15, 2023
060533c
Patch pluck before importing codegen cli
frandiox May 15, 2023
c0a32a9
Cleanup
frandiox May 15, 2023
5f9055a
Improve error messages
frandiox May 16, 2023
70af764
Feedback update
frandiox May 16, 2023
efc7d87
Fix tests
frandiox May 16, 2023
1b6bb62
Update package.json and make package publishable
frandiox May 16, 2023
60578a1
Update flag descriptions
frandiox May 16, 2023
b6d5695
Changesets and docs
frandiox May 16, 2023
cf1604a
Change default generated file name
frandiox May 17, 2023
e53efe1
Fix paths on Windows
frandiox May 17, 2023
adcf3ef
Merge branch '2023-04' into fd-codegen
frandiox May 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .changeset/friendly-clouds-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
'@shopify/cli-hydrogen': minor
---

Add **UNSTABLE** support for GraphQL Codegen to automatically generate types for every Storefront API query in the project via `@shopify/hydrogen-codegen`.

> Note: This feature is unstable and subject to change in patch releases.

How to use it while unstable:

1. Write your queries/mutations in `.ts` or `.tsx` files and use the `#graphql` comment inside the strings. It's important that every query/mutation/fragment in your project has a **unique name**:

```ts
const UNIQUE_NAME_SHOP_QUERY = `#graphql
query unique_name_shop { shop { id } }
`;
```

If you use string interpolation in your query variables (e.g. for reusing fragments) you will need to specify `as const` after each interpolated template literal. This helps TypeScript infer the types properly instead of getting a generic `string` type:

```ts
const UNIQUE_NAME_SHOP_FRAGMENT = `#graphql
fragment unique_name_shop_fields on Shop { id name }
`;

const UNIQUE_NAME_SHOP_QUERY = `#graphql
query unique_name_shop { shop { ...unique_name_shop_fields } }
${UNIQUE_NAME_SHOP_FRAGMENT}
` as const;
```

2. Pass the queries to the Storefront client and do not specify a generic type value:

```diff
-import type {Shop} from '@shopify/hydrogen/storefront-api-types';
// ...
-const result = await storefront.query<{shop: Shop}>(UNIQUE_NAME_SHOP_QUERY);
+const result = await storefront.query(UNIQUE_NAME_SHOP_QUERY);
```

3. Pass the flag `--codegen-unstable` when running the development server, or use the new `codegen-unstable` command to run it standalone without a dev-server:

```bash
npx shopify hydrogen dev --codegen-unstable # Dev server + codegen watcher
npx shopify hydrogen codegen-unstable # One-off codegen
npx shopify hydrogen codegen-unstable --watch # Standalone codegen watcher
```

As a result, a new `storefrontapi.generated.d.ts` file should be generated at your project root. You don't need to reference this file from anywhere for it to work, but you should commit it every time the types change.

**Optional**: you can tune the codegen configuration by providing a `<root>/codegen.ts` file (or specify a different path with the `--codegen-config-path` flag) with the following content:

```ts
import type {CodegenConfig} from '@graphql-codegen/cli';
import {preset, pluckConfig, schema} from '@shopify/hydrogen-codegen';

export default <CodegenConfig>{
overwrite: true,
pluckConfig,
generates: {
['storefrontapi.generated.d.ts']: {
preset,
schema,
documents: ['*.{ts,tsx}', 'app/**/*.{ts,tsx}'],
},
},
};
```

Feel free to add your custom schemas and generation config here or read from different document files. Please, report any issue you find in our repository.
7 changes: 7 additions & 0 deletions .changeset/hot-cats-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@shopify/hydrogen-codegen': patch
---

New package that provides GraphQL Codegen plugins and configuration to generate types automatically for Storefront queries in Hydrogen.

While in alpha/beta, this package should not be used standalone without the Hydrogen CLI.
5 changes: 5 additions & 0 deletions .changeset/selfish-zebras-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Add support for generated types from the new unstable codegen feature in the CLI.
Loading