Skip to content

Commit

Permalink
Break middleware info into its own article
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Barlow authored and Stephen Barlow committed Aug 29, 2019
1 parent ef71110 commit 98156b3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
3 changes: 2 additions & 1 deletion docs/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ module.exports = {
'testing-and-monitoring/mocking',
'testing-and-monitoring/testing',
],
'Improving Performance': ['performance/caching', 'performance/apq'],
Performance: ['performance/caching', 'performance/apq'],
Integrations: ['integrations/middleware'],
Deployment: [
'deployment/heroku',
'deployment/lambda',
Expand Down
35 changes: 1 addition & 34 deletions docs/source/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,40 +215,7 @@ One of the most important concepts of GraphQL is that clients can choose to quer
_only for the fields they need_. Delete `author` from the query string and execute
it again. The response updates to include only the `title` field for each book!

## Server integrations

Depending on whether we are creating a new application or an existing application, the steps will vary slightly since Apollo Server must adapt to the semantics of existing servers (e.g. Express, Hapi, etc.)

### Middleware

Existing applications generally already have middleware in place and Apollo Server works along with those middleware. To integrate with Apollo Server, we'll pass it into the `server.applyMiddleware` method as `app` to add the Apollo Server's middleware.

> The existing application is frequently already named `app`, especially when using Express. If the application is identified by a different variable, pass the existing variable in place of `app`.
The following code uses the `apollo-server-express` package, which can be installed with `npm install apollo-server-express`.

```js
const { ApolloServer, gql } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./schema');

const server = new ApolloServer({
// These will be defined for both new or existing servers
typeDefs,
resolvers,
});

server.applyMiddleware({ app }); // app is from an existing express app

app.listen({ port: 4000 }, () =>
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
)
```

Hapi follows the same pattern with `apollo-server-express` replaced with `apollo-server-hapi` and `app` replaced with Hapi server. `applyMiddleware` registers plugins, so it should be called with `await`.

> When transitioning from `apollo-server` to an integration package, running `npm uninstall apollo-server` will remove the extra dependency.
### SSL/TLS Support
## SSL/TLS Support

If you require an HTTPS connection to your Apollo Server, you can use the `https` module with `apollo-server-express`. Subscriptions can also go through an encrypted WebSocket (WSS).

Expand Down
44 changes: 44 additions & 0 deletions docs/source/integrations/middleware.md
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`.

0 comments on commit 98156b3

Please sign in to comment.