-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break middleware info into its own article
- Loading branch information
Stephen Barlow
authored and
Stephen Barlow
committed
Aug 29, 2019
1 parent
ef71110
commit 98156b3
Showing
3 changed files
with
47 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Integrating with Node.js middleware | ||
description: Use Apollo Server with Express, Koa, and more | ||
--- | ||
|
||
Apollo Server integrates easily with several popular Node.js middleware libraries. | ||
To integrate, first install the appropriate package from the table below _instead of_ | ||
the core `apollo-server` package: | ||
|
||
| Middleware | Package | | ||
|---|---| | ||
| Express | `apollo-server-express` | | ||
| Fastify | `apollo-server-fastify` | | ||
| hapi | `apollo-server-hapi` | | ||
| Koa | `apollo-server-koa` | | ||
|
||
If you've already installed the core `apollo-server` package, you can `npm uninstall` | ||
it after installing an integration package. | ||
|
||
## Applying middleware | ||
|
||
When integrating with middleware, first you initialize Apollo Server just like you | ||
always do, and then you call `applyMiddleware`, like so: | ||
|
||
```js | ||
const { ApolloServer, gql } = require('apollo-server-express'); | ||
const { typeDefs, resolvers } = require('./schema'); | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
}); | ||
|
||
server.applyMiddleware({ app }); | ||
|
||
app.listen({ port: 4000 }, () => | ||
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`) | ||
) | ||
``` | ||
|
||
In the above example, the `app` parameter you provide to `applyMiddleware` | ||
is your middleware's top-level representation of your application. In Express applications, for example, this variable is commonly named `app`. | ||
|
||
> **Note:** When integrating with hapi, call `applyMiddleware` with `await`. |