From cf1a2d709dc9f81f85054273c34c19cf30050198 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Thu, 23 Jun 2022 18:45:05 -0700 Subject: [PATCH] Move expressMiddleware to express4 deep import While expressMiddleware has no runtime dependencies, it does rely on the specific API of Express 4. One of the nice things about Express is that its development had slowed and it was a very stable target, but all good things must come to an end: Express 5 may be coming soon https://github.com/expressjs/express/issues/4920 (I kid: the changes in Express 5, like support for proper error handling with async functions, look great.) So let's preemptively put this under `/express4` so that if the Express 5 middleware needs to be a bit different (if nothing else, the req/res types in the context function may vary) that we'll have an obvious place to put it. Fixes #6587. --- WIP/migration.mdx | 3 +-- packages/server/express4/package.json | 8 ++++++++ packages/server/package.json | 5 +++++ .../server/src/__tests__/express/expressSpecific.test.ts | 3 ++- packages/server/src/__tests__/express/integration.test.ts | 8 ++------ .../src/__tests__/express/integrationServerless.test.ts | 8 ++------ packages/server/src/{express => express4}/index.ts | 0 packages/server/src/index.ts | 3 --- packages/server/src/standalone/index.ts | 2 +- 9 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 packages/server/express4/package.json rename packages/server/src/{express => express4}/index.ts (100%) diff --git a/WIP/migration.mdx b/WIP/migration.mdx index ab94fc55890..8b24ecd2ce2 100644 --- a/WIP/migration.mdx +++ b/WIP/migration.mdx @@ -415,8 +415,7 @@ import http from 'http'; import cors from 'cors'; import { json } from 'body-parser'; import { ApolloServer, ApolloServerPluginDrainHttpServer } from '@apollo/server'; -// TODO: Double check this import -import { expressMiddleware } from '@apollo/server/express'; +import { expressMiddleware } from '@apollo/server/express4'; import { typeDefs, resolvers } from ('./schema'); const server = new ApolloServer({ diff --git a/packages/server/express4/package.json b/packages/server/express4/package.json new file mode 100644 index 00000000000..1af895ad602 --- /dev/null +++ b/packages/server/express4/package.json @@ -0,0 +1,8 @@ +{ + "name": "@apollo/server/express4", + "type": "module", + "main": "../dist/cjs/express4/index.js", + "module": "../dist/esm/express4/index.js", + "types": "../dist/esm/express4/index.d.ts", + "sideEffects": false +} diff --git a/packages/server/package.json b/packages/server/package.json index 317e565f9ab..7666c53f471 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -12,6 +12,11 @@ "import": "./dist/esm/index.js", "require": "./dist/cjs/index.js" }, + "./express4": { + "types": "./dist/esm/express4/index.d.ts", + "import": "./dist/esm/express4/index.js", + "require": "./dist/cjs/express4/index.js" + }, "./standalone": { "types": "./dist/esm/standalone/index.d.ts", "import": "./dist/esm/standalone/index.js", diff --git a/packages/server/src/__tests__/express/expressSpecific.test.ts b/packages/server/src/__tests__/express/expressSpecific.test.ts index 750ebf5a013..84ec938e773 100644 --- a/packages/server/src/__tests__/express/expressSpecific.test.ts +++ b/packages/server/src/__tests__/express/expressSpecific.test.ts @@ -1,6 +1,7 @@ import express from 'express'; import request from 'supertest'; -import { ApolloServer, expressMiddleware } from '../..'; +import { ApolloServer } from '../..'; +import { expressMiddleware } from '../../express4'; it('gives helpful error if body-parser middleware is not installed', async () => { const server = new ApolloServer({ typeDefs: 'type Query {f: ID}' }); diff --git a/packages/server/src/__tests__/express/integration.test.ts b/packages/server/src/__tests__/express/integration.test.ts index aca82ea29cf..25e41c3e458 100644 --- a/packages/server/src/__tests__/express/integration.test.ts +++ b/packages/server/src/__tests__/express/integration.test.ts @@ -2,12 +2,8 @@ import { json } from 'body-parser'; import cors from 'cors'; import express from 'express'; import http from 'http'; -import { - ApolloServer, - ApolloServerOptions, - BaseContext, - expressMiddleware, -} from '../..'; +import { ApolloServer, ApolloServerOptions, BaseContext } from '../..'; +import { expressMiddleware } from '../../express4'; import { ApolloServerPluginDrainHttpServer } from '../../plugin/drainHttpServer'; import { urlForHttpServer } from '../../utils/urlForHttpServer'; import type { diff --git a/packages/server/src/__tests__/express/integrationServerless.test.ts b/packages/server/src/__tests__/express/integrationServerless.test.ts index 9c634229f91..f8219cc4982 100644 --- a/packages/server/src/__tests__/express/integrationServerless.test.ts +++ b/packages/server/src/__tests__/express/integrationServerless.test.ts @@ -2,12 +2,8 @@ import { json } from 'body-parser'; import cors from 'cors'; import express from 'express'; import http from 'http'; -import { - ApolloServer, - ApolloServerOptions, - BaseContext, - expressMiddleware, -} from '../..'; +import { ApolloServer, ApolloServerOptions, BaseContext } from '../..'; +import { expressMiddleware } from '../../express4'; import { urlForHttpServer } from '../../utils/urlForHttpServer'; import type { CreateServerForIntegrationTestsOptions, diff --git a/packages/server/src/express/index.ts b/packages/server/src/express4/index.ts similarity index 100% rename from packages/server/src/express/index.ts rename to packages/server/src/express4/index.ts diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index e67f119e565..08e07df55dc 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -10,6 +10,3 @@ export { ForbiddenError, UserInputError, } from './errors.js'; - -// TODO(AS4): consider moving to `@apollo/server/express` -export { expressMiddleware } from './express/index.js'; diff --git a/packages/server/src/standalone/index.ts b/packages/server/src/standalone/index.ts index f6f3086f499..45d06f95610 100644 --- a/packages/server/src/standalone/index.ts +++ b/packages/server/src/standalone/index.ts @@ -5,7 +5,7 @@ import express from 'express'; import http, { IncomingMessage, ServerResponse } from 'http'; import type { ListenOptions } from 'net'; import type { ApolloServer } from '../ApolloServer'; -import { expressMiddleware } from '../express/index.js'; +import { expressMiddleware } from '../express4/index.js'; import type { BaseContext, ContextFunction } from '../externalTypes'; import { ApolloServerPluginDrainHttpServer } from '../plugin/drainHttpServer/index.js'; import { urlForHttpServer } from '../utils/urlForHttpServer.js';