Skip to content

Commit faac15a

Browse files
Update AS4 Plugin Docs (#6873)
Co-authored-by: Trevor Scheer <trevor.scheer@gmail.com>
1 parent e9084af commit faac15a

13 files changed

Lines changed: 529 additions & 360 deletions

File tree

.changeset/empty-peaches-heal.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,58 @@ api_reference: true
55

66
## Using the plugin
77

8-
This API reference documents the `ApolloServerPluginCacheControl` plugin.
8+
This article documents the options for the `ApolloServerPluginCacheControl` plugin, which you can import from `@apollo/server/plugin/cacheControl`.
99

10-
This plugin enables your GraphQL server to specify a cache policy at the field level, either statically in your schema with the `@cacheControl` directive, or dynamically in your resolvers via the `info.cacheControl` API. It also by default sets the `cache-control` HTTP response header. This page is a reference for the options available in configuring the plugin; more background and examples are available in [the caching documentation](../../performance/caching/).
10+
This plugin enables your GraphQL server to specify a cache policy at the field level, either statically in your schema with the `@cacheControl` directive, or dynamically in your resolvers via the `info.cacheControl` API. It also sets the `cache-control` HTTP response header by default. See [Server-side caching](../../performance/caching/) for more information and examples.
11+
12+
> To use the `@cacheControl` directive, you must first [define it in your schema](../../performance/caching/#in-your-schema-static).
1113
1214
Apollo Server installs this plugin by default in all servers, with its default configuration. You typically do not have to install this plugin yourself; you only need to do so if you want to provide non-default configuration.
1315

14-
If you want to configure this plugin, import it from the `apollo-server-core` package and pass it to your `ApolloServer` in the `plugins` array:
16+
If you want to configure the `ApolloServerPluginCacheControl` plugin, import it and pass it to your `ApolloServer` constructor's `plugins` array:
17+
18+
<MultiCodeBlock>
1519

16-
```js
17-
import { ApolloServer } from "apollo-server";
18-
import {
19-
ApolloServerPluginCacheControl,
20-
ApolloServerPluginLandingPageLocalDefault,
21-
} from "apollo-server-core";
20+
```ts
21+
import { ApolloServer } from '@apollo/server';
22+
import { ApolloServerPluginCacheControl } from '@apollo/server/plugin/cacheControl';
2223

2324
const server = new ApolloServer({
2425
typeDefs,
2526
resolvers,
26-
csrfPrevention: true,
27-
cache: "bounded",
2827
plugins: [
2928
ApolloServerPluginCacheControl({
3029
// Cache everything for 1 second by default.
3130
defaultMaxAge: 1,
3231
// Don't send the `cache-control` response header.
3332
calculateHttpHeaders: false,
3433
}),
35-
ApolloServerPluginLandingPageLocalDefault({ embed: true }),
3634
],
3735
});
3836
```
3937

38+
</MultiCodeBlock>
39+
4040
If you don't want to use cache control at all, you can explicitly disable it with the `ApolloServerPluginCacheControlDisabled` plugin:
4141

42-
```js
43-
import { ApolloServer } from "apollo-server";
44-
import {
45-
ApolloServerPluginCacheControlDisabled,
46-
ApolloServerPluginLandingPageLocalDefault,
47-
} from "apollo-server-core";
42+
<MultiCodeBlock>
43+
44+
```ts
45+
import { ApolloServer } from '@apollo/server';
46+
import { ApolloServerPluginCacheControlDisabled } from '@apollo/server/plugin/disabled';
4847

4948
const server = new ApolloServer({
5049
typeDefs,
5150
resolvers,
52-
csrfPrevention: true,
53-
cache: "bounded",
5451
plugins: [
5552
ApolloServerPluginCacheControlDisabled(),
56-
ApolloServerPluginLandingPageLocalDefault({ embed: true }),
5753
],
5854
});
5955
```
6056

61-
(The plugin does not have much of an effect on your app if you do not use the `@cacheControl` directive or use the `info.cacheControl` API; there might be a very slight performance improvement from disabling the plugin if you do not use it.)
57+
</MultiCodeBlock>
6258

63-
Note that in Apollo Server 3, the cache control plugin does *not* define the `@cacheControl` directive for you; if you want to use the directive, you must [define the `@cacheControl` directive in your schema](../..//performance/caching/#in-your-schema-static).
59+
The plugin doesn't affect your app much if you don't use the `@cacheControl` directive or the `info.cacheControl` API. If you don't currently use it, there might be a very slight performance improvement from disabling the plugin.
6460

6561
## Options
6662

@@ -83,7 +79,9 @@ Note that in Apollo Server 3, the cache control plugin does *not* define the `@c
8379
</td>
8480
<td>
8581

86-
By default, root fields and fields that return a composite type (object, interface, or union) are considered to be uncacheable (`maxAge` 0) unless a cache hint is explicitly provided via `@cacheControl` or `info.cacheControl`. You can set this option to make the default `maxAge` for these larger than 0; this will effectively cause all requests to be be cacheable. (This option was popular in Apollo Server 2 largely as a workaround for the problem solved by the [`@cacheControl(inheritMaxAge: true)`](../../performance/caching/#in-your-schema-static) directive argument; consider using `inheritMaxAge` instead of `defaultMaxAge` in Apollo Server 3.) You can read more about [`defaultMaxAge` in the caching documentation](../../performance/caching/#default-maxage).
82+
By default, root fields and fields that return a composite type (object, interface, or union) are considered to be uncacheable (`maxAge` 0) unless a cache hint is explicitly provided via `@cacheControl` or `info.cacheControl`. You can set this option to make the default `maxAge` for these larger than 0; this will effectively cause all requests to be cacheable.
83+
84+
This option was popular in Apollo Server 2 as a workaround for the problem solved by the [`@cacheControl(inheritMaxAge: true)`](../../performance/caching/#in-your-schema-static) directive argument. See [Default `maxAge`](../../performance/caching/#default-maxage) for more details.
8785

8886
</td>
8987
</tr>

docs/source/api/plugin/drain-http-server.mdx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,62 @@ api_reference: true
55

66
## Using the plugin
77

8-
This API reference documents the `ApolloServerPluginDrainHttpServer` plugin.
8+
This article documents the options for the `ApolloServerPluginDrainHttpServer` plugin, which you can import from `@apollo/server/plugin/drainHttpServer`.
99

10-
This plugin is designed for use with [`apollo-server-express` and other framework-specific packages](../../integrations/middleware/#all-supported-packages) which are built on top of [Node `http.Server`s](https://nodejs.org/api/http.html#http_class_http_server). It is highly recommended that you use this plugin with packages like `apollo-server-express` if you want your server to shut down gracefully.
10+
This plugin is designed for use with `expressMiddleware` and other framework integrations built on top of [Node `http.Server`s](https://nodejs.org/api/http.html#http_class_http_server). **We highly recommend** using this plugin to ensure your server shuts down gracefully.
1111

12-
You do not need to explicitly use this plugin with the batteries-included `apollo-server` package: that package automatically uses this plugin internally.
12+
> You do not need to use this plugin with the `startStandaloneServer` function; it automatically handles server draining.
1313
14-
When you use this plugin, Apollo Server will drain your HTTP server when you call the `stop()` method (which is also called for you when the `SIGTERM` and `SIGINT` signals are received, unless disabled with the [`stopOnTerminationSignals` constructor option](../apollo-server/#stoponterminationsignals)). Specifically, it will:
14+
When you use this plugin, Apollo Server will drain your HTTP server when you call the `stop()` method (which is also called for you when the `SIGTERM` and `SIGINT` signals are received, unless disabled with the [`stopOnTerminationSignals` constructor option](../apollo-server/#stoponterminationsignals)).
1515

16-
* Stop listening for new connections
17-
* Close idle connections (i.e., connections with no current HTTP request)
18-
* Close active connections whenever they become idle
19-
* Wait for all connections to be closed
20-
* After a grace period, if any connections remain active, forcefully close them.
16+
Specifically, it will:
2117

22-
This plugin is exported from the `apollo-server-core` package. It is tested with `apollo-server-express`, `apollo-server-koa`, and `apollo-server-fastify`. (If you're using Hapi, you should instead use the `ApolloServerPluginStopHapiServer` plugin exported from the `apollo-server-hapi` package.)
18+
- Stop listening for new connections
19+
- Close idle connections (i.e., connections with no current HTTP request)
20+
- Close active connections whenever they become idle
21+
- Wait for all connections to be closed
22+
- After a grace period, if any connections remain active, forcefully close them.
2323

24-
Here's a basic example of how to use it with Express. See the [framework integrations docs](../../integrations/middleware/) for examples of how to use it with other frameworks.
24+
<!-- TODO(AS4) add link to other integration article once it exists -->
25+
This plugin is exported from the `@apollo/server` package. Here's a basic example of how to use it with Express:
2526

2627
<MultiCodeBlock>
2728

2829
```ts title="index.ts"
29-
const express = require('express');
30-
const { ApolloServer } = require('apollo-server-express');
31-
const {
32-
ApolloServerPluginDrainHttpServer,
33-
ApolloServerPluginLandingPageLocalDefault,
34-
} = require('apollo-server-core');
35-
const { typeDefs, resolvers } = require('./schema');
36-
const http = require('http');
30+
import { ApolloServer } from '@apollo/server';
31+
import { expressMiddleware } from '@apollo/server/express4';
32+
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
33+
import express from 'express';
34+
import http from 'http';
35+
import cors from 'cors';
36+
import { json } from 'body-parser';
37+
import { typeDefs, resolvers } from './schema';
38+
39+
interface MyContext {
40+
token?: String;
41+
}
3742

3843
async function startApolloServer() {
3944
const app = express();
4045
// Our httpServer handles incoming requests to our Express app.
4146
// Below, we tell Apollo Server to "drain" this httpServer,
4247
// enabling our servers to shut down gracefully.
4348
const httpServer = http.createServer(app);
44-
const server = new ApolloServer({
49+
const server = new ApolloServer<MyContext>({
4550
typeDefs,
4651
resolvers,
47-
csrfPrevention: true,
48-
cache: 'bounded',
49-
plugins: [
50-
ApolloServerPluginDrainHttpServer({ httpServer }),
51-
ApolloServerPluginLandingPageLocalDefault({ embed: true }),
52-
],
52+
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
5353
});
54-
5554
await server.start();
56-
57-
// Mount Apollo middleware here.
58-
server.applyMiddleware({ app });
55+
app.use('/graphql',
56+
cors<cors.CorsRequest>(),
57+
json(),
58+
expressMiddleware(server, {
59+
context: async ({ req }) => ({ token: req.headers.token }),
60+
}),
61+
);
5962
await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
60-
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
63+
console.log(`🚀 Server ready at http://localhost:4000/graphql`);
6164
return { server, app };
6265
}
6366
```

docs/source/api/plugin/inline-trace.md

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)