Skip to content

Commit

Permalink
Move expressMiddleware to express4 deep import (#6612)
Browse files Browse the repository at this point in the history
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
expressjs/express#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.
  • Loading branch information
glasser authored Jun 24, 2022
1 parent e051ea3 commit bbe883f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 19 deletions.
3 changes: 1 addition & 2 deletions WIP/migration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 8 additions & 0 deletions packages/server/express4/package.json
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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}' });
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/__tests__/express/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ export {
ForbiddenError,
UserInputError,
} from './errors.js';

// TODO(AS4): consider moving to `@apollo/server/express`
export { expressMiddleware } from './express/index.js';
2 changes: 1 addition & 1 deletion packages/server/src/standalone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit bbe883f

Please sign in to comment.