-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ESM/CJS and deep imports (#6606)
There were a few problems with #6580, which added ESM builds and deep imports to version-4. The `exports` section of `packages/server/package.json` had two mistakes: the `types` lines linked to files in the cjs directory but we only placed them in the esm directory, and the `types` lines are apparently supposed to appear first in each block. However, it turned out that this didn't matter because tsc doesn't even look in the `exports` section unless you set `moduleResolution` to `node16` or `nodenext` in your tsconfig `compilerOptions`, and we weren't! And it turns out doing that setting is a bit of a pain because many packages don't currently work with that flag. So if we published our package with the deep imports only listed in `exports` in package.json, we'd break any TypeScript user who's on the normal `moduleResolution: "node"` and telling them to set `nodenext` would be hard to obey. So since we want to continue to support tsc with `moduleResolution: "node"`, we have to teach tsc about our deep imports using the strategy of "spew a bunch of package.jsons around the package", not even under `src`/`dist`. Fortunately these package.json files can use paths starting with `../` in order to find the actual generated files. So for example, `standalone/package.json` tells tsc how to find `../dist/standalone/index.d.ts`, via fields like `types` and `main`. You might think that now that we have these little package.json files, the `exports` block would be completely redundant. Not so! Node itself does not support deep imports like `@apollo/server/standalone` by looking for a `main` in `standalone/package.json`: when importing from a published package name, it only looks for `main` at the top level of the package. So to support deep imports in Node we need the `exports` block! So this PR leaves both the `exports` block and the tree of `package.json`s in place, to be consumed by different software. We add some tsc use cases to the new smoke test. Our smoke tests verify that consumers with `moduleResolution: "nodenext"` work (or at least, that they will work once ardatan/graphql-tools#4532 is released). However, our own builds don't work that way next because some of our test-only dependencies don't work with that flag.
- Loading branch information
Showing
21 changed files
with
213 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,5 @@ node_modules/ | |
|
||
# Volta binaries (when using direnv/.envrc) | ||
.volta | ||
|
||
smoke-test/generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/cacheControl", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/cacheControl/index.js", | ||
"module": "../dist/esm/plugin/cacheControl/index.js", | ||
"types": "../dist/esm/plugin/cacheControl/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/disabled", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/disabled/index.js", | ||
"module": "../dist/esm/plugin/disabled/index.js", | ||
"types": "../dist/esm/plugin/disabled/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/drainHttpServer", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/drainHttpServer/index.js", | ||
"module": "../dist/esm/plugin/drainHttpServer/index.js", | ||
"types": "../dist/esm/plugin/drainHttpServer/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/inlineTrace", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/inlineTrace/index.js", | ||
"module": "../dist/esm/plugin/inlineTrace/index.js", | ||
"types": "../dist/esm/plugin/inlineTrace/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/landingPage/default", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/landingPage/default/index.js", | ||
"module": "../dist/esm/plugin/landingPage/default/index.js", | ||
"types": "../dist/esm/plugin/landingPage/default/index.d.ts", | ||
"sideEffects": false | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/server/plugin/landingPage/graphqlPlayground/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/landingPage/graphqlPlayground", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/landingPage/graphqlPlayground/index.js", | ||
"module": "../dist/esm/plugin/landingPage/graphqlPlayground/index.js", | ||
"types": "../dist/esm/plugin/landingPage/graphqlPlayground/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/schemaReporting", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/schemaReporting/index.js", | ||
"module": "../dist/esm/plugin/schemaReporting/index.js", | ||
"types": "../dist/esm/plugin/schemaReporting/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/plugin/usageReporting", | ||
"type": "module", | ||
"main": "../dist/cjs/plugin/usageReporting/index.js", | ||
"module": "../dist/esm/plugin/usageReporting/index.js", | ||
"types": "../dist/esm/plugin/usageReporting/index.d.ts", | ||
"sideEffects": false | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/server/src/plugin/disabled.ts → packages/server/src/plugin/disabled/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@apollo/server/standalone", | ||
"type": "module", | ||
"main": "../dist/cjs/standalone/index.js", | ||
"module": "../dist/esm/standalone/index.js", | ||
"types": "../dist/esm/standalone/index.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ApolloServer } from '@apollo/server'; | ||
import { startStandaloneServer } from '@apollo/server/standalone'; | ||
import fetch from 'make-fetch-happen'; | ||
import assert from 'assert'; | ||
|
||
async function smokeTest() { | ||
const s = new ApolloServer({ | ||
typeDefs: 'type Query {hello:String}', | ||
resolvers: { | ||
Query: { | ||
hello() { | ||
return 'world'; | ||
}, | ||
}, | ||
}, | ||
}); | ||
const { url } = await startStandaloneServer(s, { listen: { port: 0 } }); | ||
|
||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ query: '{hello}' }), | ||
}); | ||
const body = await response.json(); | ||
|
||
assert.strictEqual(body.data.hello, 'world'); | ||
|
||
await s.stop(); | ||
} | ||
|
||
smokeTest() | ||
.then(() => { | ||
console.log('TS-CJS smoke test passed!'); | ||
process.exit(0); | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ApolloServer } from '@apollo/server'; | ||
import { startStandaloneServer } from '@apollo/server/standalone'; | ||
import fetch from 'make-fetch-happen'; | ||
import assert from 'assert'; | ||
|
||
async function smokeTest() { | ||
const s = new ApolloServer({ | ||
typeDefs: 'type Query {hello:String}', | ||
resolvers: { | ||
Query: { | ||
hello() { | ||
return 'world'; | ||
}, | ||
}, | ||
}, | ||
}); | ||
const { url } = await startStandaloneServer(s, { listen: { port: 0 } }); | ||
|
||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ query: '{hello}' }), | ||
}); | ||
const body = await response.json(); | ||
|
||
assert.strictEqual(body.data.hello, 'world'); | ||
|
||
await s.stop(); | ||
} | ||
|
||
smokeTest() | ||
.then(() => { | ||
console.log('TS-ESM smoke test passed!'); | ||
process.exit(0); | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"files": [ | ||
"smoke-test.cts", | ||
], | ||
"compilerOptions": { | ||
"outDir": "generated/tsc", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"files": [ | ||
"smoke-test.mts", | ||
], | ||
"compilerOptions": { | ||
"outDir": "generated/tsc", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
}, | ||
} |