You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: support Express v5, recommend new separate Express packages (#8064)
The built-in `expressMiddleware` in `@apollo/server/express4` only
supports Express v4, and that's not going to change. But Express v5 is
now released.
So we have released separate packages `@as-integrations/express4` and
`@as-integrations/express5`. The next major version of Apollo Server
will drop the hard-coded built-in integrations, putting all JS web
frameworks on the same playing field where you have to install a small
integration package to use it.
This PR:
- Documents that the two new packages exist (and that the old export
does still work)
- Changes most examples to use the new Express v5 integration
- Changes the AS3 to AS4 migration guide to use the new Express v4
integration (assuming that anyone migrating from AS3 is not on the
brand-new version of Express)
Fixes#7928.
---------
Co-authored-by: Maria Elisabeth Schreiber <maria.schreiber@apollographql.com>
Copy file name to clipboardExpand all lines: docs/source/api/express-middleware.mdx
+15-3
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,18 @@ import TopLevelAwait from "../shared/top-level-await.mdx"
7
7
8
8
This API reference documents Apollo Server 4's [Express](https://expressjs.com/) integration, the `expressMiddleware` function.
9
9
10
+
Apollo Server integrates with these Express versions through official integrations:
11
+
-[**Express v4**](https://www.npmjs.com/package/@as-integrations/express4): Install with `npm install @as-integrations/express4`
12
+
-[**Express v5**](https://www.npmjs.com/package/@as-integrations/express5): Install with `npm install @as-integrations/express5`
13
+
14
+
You must also install Express itself. Both packages export a function named `expressMiddleware` with the same API.
15
+
16
+
<Note>
17
+
18
+
The Express v4 integration is also bundled in `@apollo/server` at `@apollo/server/express4`. While you can use this bundled version now, we recommend installing the separate package. Express v5 requires the separate package, and future Apollo Server versions will remove the bundled integration.
19
+
20
+
</Note>
21
+
10
22
## `expressMiddleware`
11
23
12
24
<TopLevelAwait />
@@ -23,7 +35,7 @@ The `expressMiddleware` function accepts two arguments. The first **required** a
Copy file name to clipboardExpand all lines: docs/source/api/standalone.mdx
+6-4
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ The `startStandaloneServer` function is not right for every use case, particular
124
124
125
125
In these cases, we recommend you swap out `startStandaloneServer` for `expressMiddleware` (unless you are confident that you want to use a different Node.js framework). This change requires only a few lines and has a minimal effect on your server's existing behavior (`startStandaloneServer` uses `expressMiddleware` under the hood).
126
126
127
-
> We recommend Express because it's the most popular Node.js web framework, and it integrates well with many _other_ popular libraries. It does have its limitations (for example, Express async support is not built around `Promise`s and `async` functions), but backward incompatible changes to the framework are rarer than in newer libraries.
127
+
> We recommend Express because it's the most popular Node.js web framework, and it integrates well with many _other_ popular libraries.
128
128
129
129
### Example
130
130
@@ -150,20 +150,22 @@ console.log(`🚀 Server ready at ${url}`);
150
150
```
151
151
</MultiCodeBlock>
152
152
153
-
To swap to using `expressMiddleware`, you'll first need to install the following packages so you'll be able to set up CORS for your server:
153
+
To swap to using `expressMiddleware`, you'll first need to install the following packages: the Express library, Apollo's integration between Express and Apollo Server, and the CORS middleware for Express:
154
154
155
155
```bash
156
-
npm install express cors
156
+
npm install @as-integrations/express5 express cors
157
157
```
158
158
159
+
Note that this should install v5 of Express.
160
+
159
161
Next, we can modify our code to match the following:
160
162
161
163
<MultiCodeBlock>
162
164
163
165
```ts
164
166
// npm install @apollo/server express graphql cors
Copy file name to clipboardExpand all lines: docs/source/integrations/building-integrations.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,9 @@ Server in their web framework of choice.
15
15
## Overview
16
16
17
17
The primary responsibility of an Apollo Server integration is to translate
18
-
requests and responses between a web framework's native format to the format used by `ApolloServer`. This article conceptually covers how to build an integration, using the [Express integration](https://github.com/apollographql/apollo-server/blob/main/packages/server/src/express4/index.ts) (i.e.,`expressMiddleware`) as an example.
18
+
requests and responses between a web framework's native format to the format used by `ApolloServer`. This article conceptually covers how to build an integration, using the [Express integration](https://github.com/apollo-server-integrations/apollo-server-integration-express5/blob/main/src/index.ts) (i.e.,`expressMiddleware`) as an example.
19
19
20
-
> For more examples, see these Apollo Server 4 [integrations demos for Fastify and Lambda](https://github.com/apollographql/server-v4-integration-demos/tree/main/packages).
20
+
> For more examples, see the source of the [community-maintained Apollo Server integrations](./integration-index).
21
21
22
22
If you are building a serverless integration, we **strongly recommend** prepending your function name with the word `start` (e.g., `startServerAndCreateLambdaHandler(server)`). This naming convention helps maintain Apollo Server's standard that every server uses a function or method whose name contains the word `start` (such as `startStandaloneServer(server)`.
23
23
@@ -118,7 +118,7 @@ Apollo Server responds to a variety of requests via both `GET` and `POST` such a
118
118
119
119
Integrations _are_ responsible for parsing a request's body and using the values to construct the `HTTPGraphQLRequest` that Apollo Server expects.
120
120
121
-
In Apollo Server 4's Express integration, a user sets up the `body-parser` JSON middleware, which handles parsing JSON request bodies with a `content-type` of `application/json`. Integrations can require a similar middleware (or plugin) for their ecosystem, or they can handle body parsing themselves.
121
+
In Apollo Server 4's Express integration, you set up the `express.json()` JSON middleware, which handles parsing JSON request bodies with a `content-type` of `application/json`. Integrations can require a similar middleware (or plugin) for their ecosystem, or they can handle body parsing themselves.
122
122
123
123
For example, a correctly parsed body should have a shape resembling this:
Copy file name to clipboardExpand all lines: docs/source/integrations/integration-index.mdx
+6-2
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,13 @@ import IntegrationTable from "../shared/integration-table.mdx"
6
6
7
7
> Are you looking to build a new integration? Or help maintain an existing integration? See [Building Web Framework Integrations for Apollo Server](./building-integrations) for step-by-step guidance!
8
8
9
-
Apollo Server 4 includes two built-in integrations: [`startStandaloneServer`](../api/standalone)and [`expressMiddleware`](../api/express-middleware).
9
+
Apollo Server 4's [`startStandaloneServer`](../api/standalone)function spins up a basic web server with sensible defaults. The server is a fully functional GraphQL server supporting all of Apollo Server's GraphQL-level customizations, but it offers minimal opportunities for HTTP-level configuration.
10
10
11
-
The `startStandaloneServer` function sets useful defaults to get you started quickly. Under the hood, the `startStandaloneServer` function uses Apollo Server 4's Express integration (i.e., `expressMiddleware`). The `expressMiddleware` function is Apollo Server 4's [Express](https://expressjs.com/) integration.
11
+
If you need more control over how your GraphQL server speaks HTTP, you should instead use an _integration_. Integrations are packages which connect the Apollo Server API to your favorite web framework.
12
+
13
+
Apollo maintains an [integration](../api/express-middleware) between Apollo Server and [Express](https://expressjs.com/), the most popular Node.js web framework. The integration with Express v4 is published as [`@as-integrations/express4`](https://www.npmjs.com/package/@as-integrations/express), and the integration with Express v5 is published as [`@as-integrations/express5`](https://www.npmjs.com/package/@as-integrations/express5). Both packages export the function [`expressMiddleware`](../api/express-middleware).
14
+
15
+
> The Express v4 integration is also included in the main `@apollo/server` package, exported from `@apollo/server/express4`. Its behavior is identical to the `@as-integrations/express4`. Express v5 is only supported by the external package. (A future major version of Apollo Server will remove `@apollo/server/express4`.)
12
16
13
17
> Have you built, or are you maintaining, an Apollo Server integration that isn't listed here? Please [submit a PR](https://github.com/apollographql/apollo-server/blob/main/docs/source/integrations/integration-index.mdx) to be added to this list!
Copy file name to clipboardExpand all lines: docs/source/migration.mdx
+10-6
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,8 @@ Apollo Server 4 focuses on improving Apollo Server's extensibility and making it
11
11
12
12
Apollo Server 4 provides the following features:
13
13
* A well-defined API with a stable HTTP abstraction, enabling contributors to easily [build and maintain integrations](./integrations/building-integrations) in their preferred frameworks.
14
-
* A new `@apollo/server` package, combining numerous [smaller packages](#packages-merged-into-apolloserver) and including the [`startStandaloneServer`](#migrate-from-apollo-server) and [`expressMiddleware`](#migrate-from-apollo-server-express) functions.
14
+
* A new `@apollo/server` package, combining numerous [smaller packages](#packages-merged-into-apolloserver) and including the [`startStandaloneServer`](#migrate-from-apollo-server) function.
15
+
* Apollo-maintained packages to integrate with the Express web framework via the [`expressMiddleware`](#migrate-from-apollo-server-express) function.
15
16
* Packages that can be used as either ECMAScript or CJS modules.
16
17
* Experimental support for [incremental delivery](./workflow/requests#incremental-delivery-experimental) when combined with a pre-release of `graphql-js`.
17
18
@@ -32,7 +33,6 @@ Apollo Server 4 takes a different approach to integrations by providing a stable
32
33
The new `@apollo/server` package contains:
33
34
34
35
- The `ApolloServer` class
35
-
- An [Express 4 integration](#migrate-from-apollo-server-express) (similar to Apollo Server 3's `apollo-server-express` package)
36
36
- A [standalone server](#migrate-from-apollo-server) (similar to Apollo Server 3's `apollo-server` package)
37
37
- A set of [core plugins](#plugins-are-in-deep-imports) (similar to Apollo Server 3's `apollo-server-core` package)
38
38
@@ -53,10 +53,12 @@ graph TB;
53
53
```
54
54
55
55
- If you're currently using the `apollo-server` package, you should use the [`startStandaloneServer`](#migrate-from-apollo-server) function.
56
-
- If you're currently using the `apollo-server-express` package, you should use the [`expressMiddleware`](#migrate-from-apollo-server-express) function.
56
+
- If you're currently using the `apollo-server-express` package, you should use the [`expressMiddleware`](#migrate-from-apollo-server-express) function in the `@as-integrations/express4` package.
57
57
58
58
The [`@apollo/server` package](https://www.npmjs.com/package/@apollo/server) exports these functions alongside the `ApolloServer` class.
59
59
60
+
> In Apollo Server 4, `@apollo/server` also contains a copy of the same `expressMiddleware` function from `@as-integrations/express4` package. We recommend you use the separate integration package instead of the copy in `@apollo/server`, which a future major version of Apollo Server will remove.
61
+
60
62
If you are using another Apollo Server 3 framework integration package (such as `apollo-server-koa` or `apollo-server-lambda`), check out our [list of integrations](./integrations/integration-index) to see if a community-maintained integration package exists for your framework of choice.
61
63
62
64
If there is no Apollo Server integration for your favorite framework _yet_, help the broader community by [building a new integration](./integrations/building-integrations)! You can also [join the discussions about maintaining our existing integrations](https://github.com/apollographql/apollo-server/labels/integration-collaborators).
@@ -187,9 +189,11 @@ Similarly, if you used the `stopGracePeriodMillis` constructor option in Apollo
187
189
188
190
If you used the `apollo-server-express` package in Apollo Server 3, use the `expressMiddleware` function in Apollo Server 4 (i.e., instead of using `server.applyMiddleware` or `server.getMiddleware`).
189
191
192
+
> The following assumes you are using Express v4. If you would like to use Express v5 (which was not supported by Apollo Server 3), you can follow the instructions below, but use the package `@as-integrations/express5` instead of `@as-integrations/express4`.
193
+
190
194
To migrate from Apollo Server 3's `apollo-server-express` package to using the `expressMiddleware` function, do the following:
191
195
192
-
1. Install the `@apollo/server` and `cors` packages.
196
+
1. Install the `@apollo/server`, `@as-integrations/express4`, and `cors` packages.
@@ -1923,7 +1927,7 @@ This section lists the TypeScript-only types (i.e., interfaces, not classes) tha
1923
1927
1924
1928
Apollo Server 4 changes the name of the constructor options type from `Config` to `ApolloServerOptions`. In Apollo Server 3, some integration packages export their own versions of this type (e.g., `ApolloServerExpressConfig`). In Apollo Server 4, there is only one `ApolloServer` type with only one constructor, so these additional types are no longer necessary.
1925
1929
1926
-
Two types in `apollo-server-express` now have more explicit names exported from `@apollo/server/express4`. `GetMiddlewareOptions` is now `ExpressMiddlewareOptions` and `ExpressContext` is now `ExpressContextFunctionArgument`.
1930
+
Two types in `apollo-server-express` now have more explicit names exported from `@as-integrations/express4`. `GetMiddlewareOptions` is now `ExpressMiddlewareOptions` and `ExpressContext` is now `ExpressContextFunctionArgument`.
Copy file name to clipboardExpand all lines: packages/server/README.md
+6-7
Original file line number
Diff line number
Diff line change
@@ -90,30 +90,29 @@ Open the URL it prints in a web browser. It will show [Apollo Sandbox](https://w
90
90
91
91
## Getting started: Express middleware
92
92
93
-
Apollo Server's built-in Express middleware lets you run your GraphQL server as part of an app built with [Express](https://expressjs.com/), the most popular web framework for Node.
93
+
Apollo Server's Express middleware lets you run your GraphQL server as part of an app built with [Express](https://expressjs.com/), the most popular web framework for Node.
94
94
95
-
First, install Apollo Server, the JavaScript implementation of the core GraphQL algorithms, Express, and two common Express middleware packages:
95
+
First, install Apollo Server, its Express middleware, the JavaScript implementation of the core GraphQL algorithms, Express, and the standard Express middleware package for CORS headers:
96
96
97
97
```
98
-
npm install @apollo/server graphql express cors body-parser
98
+
npm install @apollo/server @as-integrations/express5 graphql express cors
99
99
```
100
100
101
101
If using Typescript you may also need to install additional type declaration packages as development dependencies to avoid common errors when importing the above packages (i.e. Could not find a declaration file for module '`cors`'):
0 commit comments