apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Cannot use `apollo-server-core` with TypeScript 4.8 and ESM

Open nfroidure opened this issue 3 years ago • 7 comments

Using this module fails with a setup using TypeScript 4.8 and ESM. I get the following error:

../../node_modules/apollo-server-core/dist/types.d.ts:3:50 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@graphql-tools/schema")' call instead.

3 import type { IExecutableSchemaDefinition } from '@graphql-tools/schema';

Looking at the packages I see that @graphql-tools/schema uses ESM (type: module in package.json version 8.5.1) but not apollo-server-core (version 3.10.2). I think the @graphql-tools/schema should probably be locked to its CommonJS version since importing ESM into CommonJS is supposed to not be doable.

nfroidure avatar Aug 31 '22 13:08 nfroidure

Seems related: https://github.com/ardatan/graphql-tools/issues/4674

nfroidure avatar Aug 31 '22 13:08 nfroidure

Is this a change from a recent @graphql-tools/schema upgrade (did we accidentally release an incompatible upgrade with an AS3 point release?) or just from the TS upgrade?

The forthcoming Apollo Server 4 is built for both CJS and ESM, so we are more likely to put tough work into improving how that works (you can test it out by reading https://www.apollographql.com/docs/apollo-server/v4/migration), but if there's a simple rollback we can apply to AS3 to fix things then that works. Do you understand what's new about TS4.8 that causes this?

glasser avatar Aug 31 '22 16:08 glasser

(I'll note that the AS3 dependency from apollo-server-core on @graphql-tools/schema is ^8.0.0 and hasn't changed in over a year. Did they change something about their build in a minor version?)

glasser avatar Aug 31 '22 16:08 glasser

Also it does look like @graphql-tools/[email protected] does list both cjs and esm things in its package.json, though what I've learned is that the behavior here depends on things like what moduleResolution you have set up in your tsconfig.json. What are you using for module and moduleResolution in tsconfig?

glasser avatar Aug 31 '22 16:08 glasser

It looks like the PR @nfroidure linked attempts to resolve the issue and was released in the most recent patch of a lot of the @graphql-tools dependencies from 5 days ago, some of which are transitive to deps we use. Make sure those are upgraded to the latest (i.e. @graphql-tools/[email protected]) in your lockfile, else force an upgrade.

trevor-scheer avatar Aug 31 '22 17:08 trevor-scheer

Hmm, good catch. Though that fix doesn't seem to have much to do with ESM vs CJS. And that was a patch to schema@9, but AS3 is stuck at schema@8.

glasser avatar Aug 31 '22 17:08 glasser

I think it is only due to the TypeScript upgrade. I had a bunch of similar issues with other modules likes this one: https://github.com/kogosoftwarellc/open-api/pull/830

I'm not sure what changes in TS causes this but I suspect stricter / deeper checks of the compiler.

nfroidure avatar Sep 01 '22 06:09 nfroidure

This should no longer be an issue in the recently released AS v4.

trevor-scheer avatar Oct 20 '22 20:10 trevor-scheer