Skip to content

feat: generate TS types from Storefront GraphQL #304

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

Merged

Conversation

NWRichmond
Copy link
Contributor

Why are these changes introduced?

Addresses ENG-8184

Configure GraphQL codegen

What is this pull request doing?

This PR configures GraphQL Code Generator to generate the following TS types:

  • All types available in the Nacelle Storefront API
  • Types for the Nacelle Storefront API results of GraphQL fragments and queries in packages/storefront-sdk/src/graphql

Discussion

There are some noteworthy aspects to the changes introduced by this PR:

Node.js x ESM x TS

In keeping with GraphQL Codegen's documentation for TypeScript x ESM, the TypeScript moduleResolution and module config have been updated to "Node16". This change, coupled with the fact that this package uses "type": "module" requires us to handle imports a bit differently than we're used to. Instead of:

import { thing } from '../someFile';

, we must instead import using a .js extension:

import { thing } from '../someFile.js';

You can learn more about why this is in the TypeScript 4.7 Release Notes.

The two consequences of this change are:

  1. We need to get used to importing with the .js extension.
  2. The ESLint config has been updated with the requisite extensionAlias settings.

For some lively reading on why we can't use a .ts extension instead of a .js extension for ESM imports, see microsoft/TypeScript#49083.

The graphql dependency

graphql has been installed as a dependency. This is probably fine, but it's worth contemplating whether this could lead to multiple versions of graphql being installed in a user's project, which can cause some tricky issues. I'm not sure how likely that would be. But if we can identify specific cases where it would be a problem, one potential solution would be to reinstall graphql as a devDep, and also include it in peerDependencies.

How to Test

  1. Check out the ENG-8184-configure-graphql-codegen branch.
  2. npm run bootstrap from the monorepo root, or npm i from packages/storefront-sdk.
  3. Navigate to packages/storefront-sdk.
  4. npm run build - the package should build without errors.
  5. npm run codegen - the codegen steps should finish without errors, and there should be no uncommitted changes to src/types/storefront.ts.

@NWRichmond NWRichmond requested review from KrisQ and mdarrik December 22, 2022 20:00
@changeset-bot
Copy link

changeset-bot bot commented Dec 22, 2022

🦋 Changeset detected

Latest commit: 95c4902

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Dec 22, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
nacelle-gatsby-reference-store ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Dec 28, 2022 at 9:55PM (UTC)
nacelle-gatsby-starter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Dec 28, 2022 at 9:55PM (UTC)
nacelle-next-reference-store ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Dec 28, 2022 at 9:55PM (UTC)
nacelle-next-starter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Dec 28, 2022 at 9:55PM (UTC)
nacelle-nuxt-reference-store ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Dec 28, 2022 at 9:55PM (UTC)
nacelle-nuxt-starter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Dec 28, 2022 at 9:55PM (UTC)

Comment on lines +1 to +10
overwrite: true
schema: 'https://storefront.api.nacelle.com/graphql/v1/spaces/${NACELLE_SPACE_ID}'
documents: 'src/graphql/**/*.ts'
generates:
src/types/storefront.ts:
plugins:
- typescript
- typescript-operations
config:
enumsAsTypes: true
Copy link
Contributor Author

@NWRichmond NWRichmond Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be wondering why we're using codegen.yml instead of codegen.ts. Although the GraphQL Code Generator docs suggest that using codgen.ts in an ESM project should work, there is a bug that's getting in the way: https://github.com/dotansimha/graphql-code-generator/issues/8488.

Copy link
Contributor

@KrisQ KrisQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

chore: remove unnecessary config
chore: apply formatting to generated types

fix: omit `test.ts` files
Copy link
Contributor

@mdarrik mdarrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Even after deleting src/types/storefront.ts and re-running it, there are no changes to be committed.

@NWRichmond NWRichmond merged commit c0e3aee into ENG-8179/storefront-sdk-2.0 Dec 29, 2022
@NWRichmond NWRichmond deleted the ENG-8184-configure-graphql-codegen branch December 29, 2022 00:30
NWRichmond added a commit that referenced this pull request Apr 26, 2023
* feat: generate TS types from Storefront GraphQL

chore: remove unnecessary config

* feat: generate types from TS documents

chore: apply formatting to generated types

fix: omit `test.ts` files

* docs: add changeset
NWRichmond added a commit that referenced this pull request Apr 26, 2023
* feat: generate TS types from Storefront GraphQL

chore: remove unnecessary config

* feat: generate types from TS documents

chore: apply formatting to generated types

fix: omit `test.ts` files

* docs: add changeset
NWRichmond added a commit that referenced this pull request Apr 26, 2023
* feat: generate TS types from Storefront GraphQL

chore: remove unnecessary config

* feat: generate types from TS documents

chore: apply formatting to generated types

fix: omit `test.ts` files

* docs: add changeset
NWRichmond added a commit that referenced this pull request Apr 26, 2023
* feat: generate TS types from Storefront GraphQL

chore: remove unnecessary config

* feat: generate types from TS documents

chore: apply formatting to generated types

fix: omit `test.ts` files

* docs: add changeset
NWRichmond added a commit that referenced this pull request Apr 26, 2023
* feat: storefront-sdk-v2 (#303)

* feat: storefront-sdk-v2

* chore: adds .editorconfig

* format: add newline

Co-authored-by: Nick Richmond <[email protected]>

* chore: upgrades vite version

* refactor: package config

* chore: formatting

Co-authored-by: Darrik <[email protected]>
Co-authored-by: Nick Richmond <[email protected]>
Co-authored-by: Nick Richmond <[email protected]>

* chore: enables pre-release mode (#305)

* chore: enables pre-release mode

* build: adds release flow for storefront-sdk beta

* feat: generate TS types from Storefront GraphQL (#304)

* feat: generate TS types from Storefront GraphQL

chore: remove unnecessary config

* feat: generate types from TS documents

chore: apply formatting to generated types

fix: omit `test.ts` files

* docs: add changeset

* build: version packages

* feat: add basic storefront client (#306)

* feat: add basic storefront client

* refactor: prefer native private class fields

* feat: add v2 query method using urql (#307)

* feat: configure typed-document-node generation

* test: configure vitest typechecking

* test: configure request mocks

* feat: adds v2 query method

* docs: adds changeset for query method changes

* chore: ignore explicit any for query types

Co-authored-by: Nick Richmond <[email protected]>

* chore: use destructuring

Co-authored-by: Nick Richmond <[email protected]>

Co-authored-by: Nick Richmond <[email protected]>

* feat: implement the `after` method (#310)

* feat: implement after method

types: improve callback type inference

docs: improve accuracy

refactor: remove unnecessary assignment

types: improve accuracy

docs: add changeset

* fix: run `applyAfter` on `query` method result

* feat: add `setConfig` & `getConfig` (#309)

* feat: add `setConfig` + `addConfig`

* feat: configure urql exchanges (#312)

* feat: configure urql exchanges

- adds retry exchange
- adds persisted fetch exchange for APQ

* docs: adds changeset

* test: adds test for APQ & crypto support to jsdom

* feat: scaffold commerce queries plugin (#313)

* fix: type definition resolution in ESM projects

* feat: scaffold commerce queries plugin

fix: codegen and tests

fix: entry point for TS resolution

fix: preserve symbolic links

fix: expect clause

* docs: annouce breaking change to client methods

* fix: linting

* chore: use consistent codegen scripts

* test: configure coverage locations

* chore: adds TSdocs to `setConfig` and `getConfig` (#314)

* chore: adds TSdocs to `setConfig` and `getConfig`

* docs: add js docs to the query method and StorefrontResponse type (#316)

* docs: add js docs to the query method and StorefrontResponse type

* docs: apply review suggestions

Co-authored-by: Nick Richmond <[email protected]>

Co-authored-by: Nick Richmond <[email protected]>

* feat: add `spaceProperties` method (#317)

* feat: add `spaceProperties` method

* feat: add `navigation` method (#321)

* feat: navigation

* feat(commerce-queries): add content method  (#318)

* feat: add content method

* feat: make data fetching reusable

* tests: remove storefront types from test coverage

* docs: adds changeset

* chore: adds suggestions from PR review (style & linting)

Co-authored-by: Nick Richmond <[email protected]>

* docs: adds some comments to the paginated method file

* test: fix tests for apq after merge

* chore: fix storefront response use after rebase

* lint: clean up eslint warnings

Co-authored-by: Nick Richmond <[email protected]>

* feat(commerce-queries): add product method (#322)

* feat: add product queries and fragments

* fix: add endCursor to product query

* fix: dedupe fragments so the product operation works

* feat: adds product method

* docs: adds changeset

* feat: implement the `productCollections` method (#323)

* refactor: separate codegen documents from types

* feat: implement productCollections method

* style: readability

* test: `productCollections` method coverage

* docs: add changeset

* fix: implement `maxReturnedEntriesPerCollection`

* refactor: remove unnecessary variable

* fix: return expected types from methods

* feat: add `productCollectionEntries` method (#325)

* feat: add productCollectionEntries method

* feat: add advanced option to control APQ (#327)

* chore: version packages (#329)

* test: clean up tests by disabling APQ (#328)

* feat: error handling (#330)

* feat: error handling

* refactor: externalize `@urql/core` & graphql deps (#332)

* feat: adds trace id to error messages (#333)

* feat: adds trace id to error messages

* docs: adds changeset

* chore: apply review suggestions

Co-authored-by: Nick Richmond <[email protected]>

---------

Co-authored-by: Nick Richmond <[email protected]>

* refactor: prefer named export; export types (#338)

* fix: use conditional types to avoid assertions (#336)

* fix: use conditional types to avoid assertions

* docs: add changeset

* chore: rename commerce queries plugin (#337)

* chore: remove `complexity error` error handling (#334)

* fix: remove the complexity error handling

* fix: changset package name

* build: skip lockfile updates when versioning (#341)

* fix: externalize dependencies (#339)

* build: version packages (beta) (#342)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix: dependency versions

* chore: remove storefront method (#340)

* chore: remove storefront method

* chore: remove request policy (#344)

* chore: exports QueryParams type (#345)

Also moves StorefrontResponse to an `export type` statement

* build: version packages (beta) (#343)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore: update lockfile

* chore: rename commerce query outputs (#347)

* chore: update build output filenames

* refactor: rename named export

* docs: add changeset

* build: version packages (beta) (#346)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix: export types to decouple packages (#348)

* fix: export types to decouple packages

* docs: add changeset

* fix: allow `null` for `previewToken`

* fix: export types

* build: simplify & defer to tsconfig defaults

* build: version packages (beta) (#349)

* build: version packages (beta)

* chore: reset storefront-sdk version range

* chore: use range set by changesets

* chore: update semver range

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nick Richmond <[email protected]>

* fix: do not send request variables as strings (#350)

* fix: do not send request variables as strings

* docs: add changeset

* docs: improve clarity

* build: version packages (beta) (#352)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore(Commerce Queries Plugin): exports all of the generated Storefront GQL Types (#351)

* chore: exports all of the generated storefront types

* docs: adds changeset

* chore: remove gql operation types from storefront.ts

* build: version packages (beta) (#353)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* refactor: make `after` method plugin-agnostic (#354)

* refactor: make after method plugin-agnostic

* docs: fix typo

* docs: add changeset

* refactor: remove useless line

* docs: adjust changeset

* refactor: remove unnecessary ESLint comment

* types: let's keep our promises

* refactor: remove unnecessary ESLint comment

* fix: undo type change

* build: version packages (beta) (#355)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix: set preview query param (#359)

* fix: set preview query param

* docs: add changeset

* build: version packages (beta) (#360)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix: preview token header (#361)

* fix: preview token header

* test: remove redundant tests

* docs: add changeset

* refactor: improve clarity

* build: version packages (beta) (#362)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* feat: accept `exchanges` in initialization params (#366)

* refactor: export exchanges

* refactor: default exchanges

* refactor: remove unused property

* feat: accept `exchanges` in initialization params

* refactor!: do not alter exchanges in `setConfig`

* test: use exchanges param to configure APQ

* test: exchanges exported by package

* docs: add changeset

* feat: use SDK v2 (#367)

* chore: remove `advancedOptions` and `enableAPQ` (#365)

* feat: remove `enableAPQ` logic and tests

* chore: add major bump changeset

* fix: update lock file

* build: version packages (beta) (#368)

* build: version packages (beta)

* docs: amend changelog

* chore: undo dependency version bump

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nick Richmond <[email protected]>

* docs: scaffold READMEs (#371)

* docs: set package description

* docs: scaffold README

* docs: add detail to README

* docs: add changeset

* docs: use consistent punctuation

* feat: upgrade internals to `@urql/core@4` (#370)

* feat: upgrade to urql v4

* chore: upgrade persisted exchange

* refactor: remove `dedupExchange`

* chore: remove dedup exchange

* chore: upgrade Storefront SDK dependency

* chore: unset specific beta version in peerDep

* chore: `npm audit fix`

* docs: add changeset

* docs: add changeset

* build: version packages (beta) (#372)

* build: version packages (beta)

* chore: undo dependency version bump

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nick Richmond <[email protected]>

* fix: install sdk in commerce query type issue (#369)

* fix: install sdk in commerce query type issue

* docs: add changeset

---------

Co-authored-by: Nick Richmond <[email protected]>

* build: version packages (beta) (#374)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore(publish): [skip ci] Update lockfiles

* chore: exit prerelease mode

* chore: remove feature branch from release branches

---------

Co-authored-by: KrisQ <[email protected]>
Co-authored-by: Darrik <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants