mswjs / msw

Seamless REST/GraphQL API mocking library for browser and Node.js.
https://mswjs.io
MIT License
15.72k stars 504 forks source link

2.4.0 Main entry point still exports handlers (i.e: `GraphQLHandler`) #2247

Closed nartc closed 1 month ago

nartc commented 1 month ago

Prerequisites

Environment check

Node.js version

20.14.0

Reproduction repository

https://github.com/nartc/msw-entry-point-bug

Reproduction steps

npm install npm test

Script fails with Cannot find module "graphql"

Current behavior

Thank you for your effort on msw.

Before I say anything, I believe our issue is our own "user error". However, I also believe this issue can still be an overlook as current code would break at runtime with 2.4.0 and the release note does not mention anything about the new secondary entry points available in 2.4.0

import { http, HttpHandler } from 'msw';

This works in pre 2.4.0 but will break in 2.4.0 for projects that don't have graphql installed. This is due to msw core entry point still exports the handlers which makes graphql NOT an optional peer dependency.

The proper fix would be to import from msw/core/http but it seems like msw/core/http does not export HttpHandler either.

Once again, our project runs into this due to many errors/mistakes on our part but I want to raise this issue in case others run into the same thing (or have the same user errors/mistakes)

Expected behavior

import { http } from 'msw';

This import should at least break at compilation time (type checking time) with something like: msw does not export http. And release note should mention migrating to msw/core/http (or respective entry point for the handlers).

ghost91- commented 1 month ago

Unfortunately, using more specific exports doesn't always seem to be possible. For example, unless I am mistaken, delay is only exported via the main export.

Similarly, HttpResponse also is not exported from msw/core/http. Probably applies to quite a few more things.

If msw/core/http is really intended as the "don't need graphql" entrypoint, the exports should probably be identical to those of msw, except from the graphql related stuff.

nartc commented 1 month ago

@ghost91- or msw entry point should be cleared of anything that requires optional peer dependencies so things like main utilities can still be exported from the main entry point.

kettanaito commented 1 month ago

@ghost91-, good point. The explicit import would be better once we drop graphql export from the root module. So you import graphl from msw/core/graphql only.

For now, I think the safest way to fix this and now force developers to install graphql is to make that import dynamic. Need to give this a try.

kettanaito commented 1 month ago

I will close this one in favor of #2248 (has a more searchable error message). Please keep track of the updates there. Thanks.

kettanaito commented 1 month ago

Released: v2.4.1 🎉

This has been released in v2.4.1!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.