Skip to content

feat: quality of life improvements for graphql file generation#982

Merged
matthewvolk merged 1 commit intomainfrom
feat/provider-naming-convention
Jun 10, 2024
Merged

feat: quality of life improvements for graphql file generation#982
matthewvolk merged 1 commit intomainfrom
feat/provider-naming-convention

Conversation

@matthewvolk
Copy link
Copy Markdown
Contributor

What/Why?

This PR introduces changes that address the following concerns:

  • Tracking GraphQL schemas in version control
  • Consistent naming conventions for files related to GraphQL
  • More helpful error messaging when generating GraphQL introspection files

A common pattern I'm seeing when integrating Catalyst with various third party services is that some third party services may have their own GraphQL API's that Catalyst needs to interface with; in these cases, I've been leveraging the Multi-schema mode feature of gql.tada.

The moment that a second schema is added to a Catalyst project, the schema.graphql and graphql-env.d.ts file naming conventions become less useful, as the file names do not easily describe the provider from which the files were created. Changing the naming convention of the default BigCommerce GraphQL files to include bigcommerce in the name makes it much easier to identify which GraphQL API the files are generated from, especially when you have multiple GraphQL schema files in a single project.

Additionally, I want to propose a change to our opinions of which files should live in version control for both the Catalyst monorepository, and separately, scaffolded Catalyst projects. In the context of working within the monorepository, it makes sense to .gitignore these files; we don't want any single test store's schema to be tracked in /core, as this would cause those files to be copied into scaffolded Catalyst projects. However, this opinion changes in the context of scaffolded projects. Scaffolded projects should have their GraphQL schema/introspection files tracked so that developers who are all collaborating on a single storefront build can track changes introduced to GraphQL schemas in pull requests.

Finally, this PR also slightly modifies core/scripts/generate.cjs to prevent errors from being swallowed by the script instead of being logged to the console. Since generate is asynchronous, Node throws an unhandledRejection event instead of actually throwing the error in the console. The change introduced catches any errors thrown, and manually logs them to the console.

Tip

If this PR is merged, be sure to run pnpm generate locally to create required GraphQL files, and then delete the now-untracked schema.graphql and graphql-env.d.ts files to help prevent accidentally committing them.

Testing

Ensure you have a .env.local file inside of /core with required environment variables

  1. Run pnpm generate
  2. See that GraphQL files are generated with BigCommerce-specific naming conventions
  3. If you remove any required environment variables from .env.local (such as store hash) and run pnpm generate again, you'll see the more helpful error messages logged by generate.cjs.

@matthewvolk matthewvolk requested a review from a team June 7, 2024 19:08
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 7, 2024

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

Name Status Preview Comments Updated (UTC)
catalyst-latest ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2024 8:37pm
5 Ignored Deployments
Name Status Preview Comments Updated (UTC)
catalyst-1millionproducts-store ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 8:37pm
catalyst-au ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 8:37pm
catalyst-test-store ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 8:37pm
catalyst-uk ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 8:37pm
catalyst-unstable ⬜️ Ignored (Inspect) Visit Preview Jun 7, 2024 8:37pm

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 7, 2024

🦋 Changeset detected

Latest commit: f50b525

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

This PR includes changesets to release 1 package
Name Type
@bigcommerce/catalyst-core Patch

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

Comment thread core/scripts/generate.cjs Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we removing this? Shouldn't this point to bigcommerce-graphql.d.ts now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

While in Multi-schema mode, since generateOutput produces an introspection file for each GraphQL schema in the tsconfig plugin schemas array, we can't pass it a single file name - instead, generateOutput will create a file for each tadaOutputLocation key in each schema config object 🙂

If we try to manually set this to:

await generateOutput({
  disablePreprocessing: false,
  output: join(__dirname, '../bigcommerce-graphql.d.ts'),
  tsconfig: undefined,
});

We get the following:
Screenshot 2024-06-07 at 2 24 48 PM

Comment thread core/scripts/generate.cjs Outdated
Comment on lines 61 to 69
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you explain why this is needed?

Copy link
Copy Markdown
Contributor Author

@matthewvolk matthewvolk Jun 7, 2024

Choose a reason for hiding this comment

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

TL;DR:

  1. The errors thrown in getStoreHash and getToken are synchronous
  2. If these functions were called synchronously and an error was thrown, the script would immediately terminate (unless they were caught)
  3. However, these functions are called within an asynchronous context (generate)
  4. Since we are no longer synchronous, and are not catching errors thrown from calling generate, the throw statements emit an unhandledRejection event instead of immediately terminating the program

You can try this by reverting the code so that it looks like:

// ...

generate();

Then, remove a required environment variable from .env.local and try pnpm generate again. You'll notice the process exits with a 0 exit code, but no Schema or introspection files were generated.

If you then add:

// ...

generate();

process.on('unhandledRejection', (error) => {
  console.error(error);
  process.exit(1);
});

And run pnpm generate again, you'll see the error logged to the console.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Update: Refactored this to simply wrap the body of generate in a try/catch, lends to better readability compared to the IIFE

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 7, 2024

⚡️🏠 Lighthouse report

Lighthouse ran against https://catalyst-latest-adpbl47hi-bigcommerce-platform.vercel.app

🖥️ Desktop

We ran Lighthouse against the changes on a desktop and produced this report. Here's the summary:

Category Score
🟢 Performance 100
🟢 Accessibility 100
🟢 Best practices 100
🟢 SEO 90

📱 Mobile

We ran Lighthouse against the changes on a mobile and produced this report. Here's the summary:

Category Score
🟢 Performance 98
🟢 Accessibility 100
🟢 Best practices 100
🟢 SEO 92

@matthewvolk matthewvolk added this pull request to the merge queue Jun 10, 2024
Merged via the queue into main with commit b8ea900 Jun 10, 2024
@matthewvolk matthewvolk deleted the feat/provider-naming-convention branch June 10, 2024 15:34
This was referenced Jun 10, 2024
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.

2 participants