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
Copy file name to clipboardexpand all lines: docs/source/api/express-middleware.mdx
+4-6
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ This API reference documents Apollo Server 4's [Express](https://expressjs.com/)
13
13
14
14
The `expressMiddleware` function enables you to attach Apollo Server to an Express server.
15
15
16
-
The `expressMiddleware` function expects you to set up HTTP body parsing and CORS headers for your web framework. Specifically, you should install the [`body-parser`](https://www.npmjs.com/package/body-parser)and [`cors`](https://www.npmjs.com/package/cors)packages and use them to set up your Express app, as shown below.
16
+
The `expressMiddleware` function expects you to set up HTTP body parsing and CORS headers for your web framework. Specifically, you can use the native [`express.json()`](https://expressjs.com/en/api.html#express.json) function (available in Express `v4.16.0` onwards) and the [`cors`](https://www.npmjs.com/package/cors)package to set up your Express app, as shown below.
17
17
18
18
> See [Configuring CORS](../security/cors#specifying-origins) for guidance on configuring the CORS behavior of your project.
19
19
@@ -25,7 +25,6 @@ The `expressMiddleware` function accepts two arguments. The first **required** a
Copy file name to clipboardexpand all lines: docs/source/api/standalone.mdx
+3-4
Original file line number
Diff line number
Diff line change
@@ -150,25 +150,24 @@ 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 HTTP body parsing and CORS for your server:
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:
154
154
155
155
```bash
156
-
npm install express cors body-parser
156
+
npm install express cors
157
157
```
158
158
159
159
Next, we can modify our code to match the following:
160
160
161
161
<MultiCodeBlock>
162
162
163
163
```ts
164
-
// npm install @apollo/server express graphql cors body-parser
164
+
// npm install @apollo/server express graphql cors
Copy file name to clipboardexpand all lines: docs/source/migration.mdx
+8-10
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ If there is no Apollo Server integration for your favorite framework _yet_, help
64
64
Below are a few high-level changes for using framework integrations:
65
65
66
66
- You can pass your [`context` initialization function](#context-initialization-function) directly to your framework's integration function (e.g., `expressMiddleware` or `startStandaloneServer`) instead of the `ApolloServer` constructor.
67
-
- You are responsible for [setting up HTTP body parsing and CORS](#body-parser-and-cors) using your framework integration's standard functionality.
67
+
- You are responsible for [setting up HTTP body parsing and CORS](#http-body-parsing-and-cors) using your framework integration's standard functionality.
68
68
- If you want your server to listen on a specific URL path, pass that path directly to your framework's router instead of using the [`path` option](#path-parsing). If you did not specify a URL path, the default in Apollo Server 3 was `/graphql`, so to preserve existing behavior, you should specify that path explicitly.
69
69
70
70
The following sections show how servers using `apollo-server` or `apollo-server-express` can update to Apollo Server 4.
@@ -189,9 +189,9 @@ If you used the `apollo-server-express` package in Apollo Server 3, use the `exp
189
189
190
190
To migrate from Apollo Server 3's `apollo-server-express` package to using the `expressMiddleware` function, do the following:
191
191
192
-
1. Install the `@apollo/server`, `cors`, and `body-parser` packages.
192
+
1. Install the `@apollo/server`and `cors` packages.
3. Add `cors`and `bodyParser.json()`to your server setup.
194
+
3. Add `cors` to your server setup.
195
195
4. Remove the Apollo Server 3 `apollo-server-express` and `apollo-server-core` packages.
196
196
5. If you are using `apollo-server-express`'s default `/graphql` URL path (i.e., not specifying another URL with the [path option](/apollo-server/v3/api/apollo-server/#path)), you can mount `expressMiddleware` at `/graphql` to maintain behavior. To use another URL path, mount your server (with `app.use`) at the specified path.
197
197
@@ -232,15 +232,13 @@ looks like this in Apollo Server 4:
232
232
<MultiCodeBlock>
233
233
234
234
```ts title="apollo-server-4.ts"
235
-
// npm install @apollo/server express graphql cors body-parser
235
+
// npm install @apollo/server express graphql cors
@@ -910,11 +908,11 @@ In Apollo Server 4, you should use your framework's routing feature to mount you
910
908
911
909
> Apollo Server 3's batteries-included `apollo-server` package, replaced by `startStandaloneServer` in Apollo Server 4, serves all URLs (i.e., rather than only listening on `/graphql`).
912
910
913
-
### `body-parser`and `cors`
911
+
### HTTP body parsing and CORS
914
912
915
913
In Apollo Server 3, framework integrations automatically set up HTTP body parsing and CORS response headers. You can customize your integration's CORS or body parsing functionality using the Apollo Server API; these configuration options [vary by integration](/apollo-server/v3/api/apollo-server#cors-1).
916
914
917
-
In Apollo Server 4, it's your responsibility to set up HTTP body parsing and CORS headers for your web framework. Specifically, when using [`expressMiddleware`](#migrate-from-apollo-server-express), you should install the `body-parser`and `cors` npm packages and use them in your Express app, just like with any other JSON-based API server. If you passed a `cors` option to `applyMiddleware` or `getMiddleware`, pass the same value to the `cors` function. If you passed a `bodyParserConfig` option to `applyMiddleware` or `getMiddleware`, pass the same value to the `body-parser` package's `json` function.
915
+
In Apollo Server 4, it's your responsibility to set up HTTP body parsing and CORS headers for your web framework. Specifically, when using [`expressMiddleware`](#migrate-from-apollo-server-express), you can use the native [`express.json()`](https://expressjs.com/en/api.html#express.json) function (available in Express `v4.16.0` onwards) for body parsing and the `cors` npm package for CORS headers. You can install use the `cors` package in your Express app, just like with any other JSON-based API server. If you passed a `cors` option to `applyMiddleware` or `getMiddleware`, pass the same value to the `cors` function. If you passed a `bodyParserConfig` option to `applyMiddleware` or `getMiddleware`, pass the same value to the [`express.json()`](https://expressjs.com/en/api.html#express.json) function.
918
916
919
917
Note that [`startStandaloneServer`](#migrate-from-apollo-server) sets up body parsing and CORS functionality for you, but you can't configure this behavior. In Apollo Server 3, you could configure the batteries-included `apollo-server`'s CORS behavior via the `cors` constructor option. In Apollo Server 4, if you need to configure CORS behavior, use `expressMiddleware` rather than `startStandaloneServer`.
920
918
@@ -1106,7 +1104,7 @@ Whereas this query would be invalid:
1106
1104
If you'd like to restore the previous behavior, you can `JSON.parse` the `variables` and `extensions` fields after your framework has parsed the request body. In Express that might look like:
Copy file name to clipboardexpand all lines: docs/source/monitoring/health-checks.mdx
+4-4
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Health checks
3
3
description: Determining the health status of Apollo Server
4
4
---
5
5
6
-
> **Apollo Server 4 no longer [supports built-in health checks](../migration/#health-checks)**. Instead, we recommend performing [GraphQL-level health checks](#graphql-level-health-checks) to ensure your server successfully serves traffic _and_ performs GraphQL operations.
6
+
> **Apollo Server 4 no longer [supports built-in health checks](../migration/#health-checks)**. Instead, we recommend performing [GraphQL-level health checks](#graphql-level-health-checks) to ensure your server successfully serves traffic _and_ performs GraphQL operations.
7
7
8
8
Load balancers often use health checks to determine if a server is available and ready to serve traffic.
9
9
@@ -21,7 +21,7 @@ Note that this health check will run an actual GraphQL operation. If your server
21
21
22
22
> Sending an `apollo-require-preflight: true` header alongside your health check ensures that Apollo Server's [CSRF prevention](../security/cors/#preventing-cross-site-request-forgery-csrf) feature won't block it.
23
23
24
-
If you want to create a health check for your HTTP server that is _unrelated_ to the health of the GraphQL execution engine (i.e., such as [Apollo Server 3's health check feature](/apollo-server/v3/monitoring/health-checks)), you can add a `GET` handler that always succeeds to your web framework.
24
+
If you want to create a health check for your HTTP server that is _unrelated_ to the health of the GraphQL execution engine (i.e., such as [Apollo Server 3's health check feature](/apollo-server/v3/monitoring/health-checks)), you can add a `GET` handler that always succeeds to your web framework.
25
25
26
26
Below is an example of an HTTP server health check with [`expressMiddleware`](../api/express-middleware/):
27
27
@@ -39,7 +39,7 @@ const server = new ApolloServer({
> If you are using `startStandaloneServer`, you must first [swap to using the `expressMiddleware` function](../api/standalone/#swapping-to-expressmiddleware) before creating an HTTP server health check.
59
+
> If you are using `startStandaloneServer`, you must first [swap to using the `expressMiddleware` function](../api/standalone/#swapping-to-expressmiddleware) before creating an HTTP server health check.
Copy file name to clipboardexpand all lines: docs/source/security/cors.mdx
+4-5
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,7 @@ If you create a public API or an API to embed in websites you don't control your
86
86
If your application doesn't fit into any of the above categories, [`startStandaloneServer`](../api/standalone)'s CORS behavior should suit your use case. You can always choose to swap to another Apollo Server integration later to [customize your CORS configuration](#configuring-cors-options-for-apollo-server).
87
87
88
88
## Configuring CORS options for Apollo Server
89
-
> 📣 **New in Apollo Server 4**: if you are using an Apollo Server integration (e.g., [`expressMiddleware`](../api/express-middleware)), you are responsible for [setting up CORS for your web framework](../migration#body-parser-and-cors).
89
+
> 📣 **New in Apollo Server 4**: if you are using an Apollo Server integration (e.g., [`expressMiddleware`](../api/express-middleware)), you are responsible for [setting up CORS for your web framework](../migration#http-body-parsing-and-cors).
90
90
91
91
Apollo Server's standalone server (i.e., [`startStandaloneServer`](../api/standalone)) serves the `Access-Control-Allow-Origin` HTTP header with the wildcard value (`*`). This allows scripts on any origin to make requests, _without cookies_, to the server and read its responses.
92
92
@@ -104,7 +104,6 @@ import { expressMiddleware } from '@apollo/server/express4';
@@ -137,7 +136,7 @@ console.log(`🚀 Server ready at http://localhost:4000/graphql`);
137
136
138
137
> Invoking the `cors` function with no arguments sets your server's `Access-Control-Allow-Origin` HTTP header to the wildcard value (`*`), allowing scripts on any origin to make requests. So, your server would have the same CORS behavior as `startStandaloneServer`.
139
138
140
-
Using the `cors` package directly, we can configure the `Access-Control-Allow-Origin` header using the [`origin` option](https://github.com/expressjs/cors#configuration-options). The example above enables CORS requests from `https://www.your-app.example`, along with `https://studio.apollographql.com`.
139
+
Using the `cors` package directly, we can configure the `Access-Control-Allow-Origin` header using the [`origin` option](https://github.com/expressjs/cors#configuration-options). The example above enables CORS requests from `https://www.your-app.example`, along with `https://studio.apollographql.com`.
141
140
142
141
> If you want to use [Apollo Studio Explorer](https://www.apollographql.com/docs/studio/explorer/explorer/) as a GraphQL web IDE, you should include `https://studio.apollographql.com` in your list of valid origins. However, if you plan to embed the [Explorer](https://www.apollographql.com/docs/studio/explorer/embed-explorer/) or use [Apollo Sandbox](https://www.apollographql.com/docs/studio/explorer/sandbox), you *don't* need to specify Studio's URL in your CORS origins because requests will go through the page embedding Studio.
0 commit comments