Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposes applyMiddleware from apollo-server #1314

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ The `applyMiddleware` method is provided by the `apollo-server-{integration}` pa

### Usage

The `applyMiddleware` method from `apollo-server-express` registration of middleware as shown in the example below:
The `applyMiddleware` method from `apollo-server` registration of middleware as shown in the example below:

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

const server = new ApolloServer({
Expand Down
12 changes: 2 additions & 10 deletions docs/source/essentials/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ To install, run:

npm install --save apollo-server@rc graphql

When adding Apollo Server to an existing application, a corresponding HTTP server support package needs to be installed as well. For example, for Express this is:

npm install --save apollo-server-express@rc graphql

> Note: During the release candidate period, it's necessary to use the `rc` npm package, as shown in the above commands.

<h2 id="creating">Creating a server</h2>
Expand Down Expand Up @@ -117,10 +113,8 @@ Existing applications generally already have middleware in place and Apollo Serv

> 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@rc`.

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

const server = new ApolloServer({
Expand All @@ -136,9 +130,7 @@ app.listen({ port: 4000 }, () =>
)
```

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 transition from `apollo-server` to an integration package, running `npm uninstall apollo-server` will remove the extra dependency.
Currently the `apollo-server` package can be used directly with Express only. Koa and Hapi follow the same pattern with `apollo-server` replaced with `apollo-server-koa` and `apollo-server-hapi` and `app` replaced with Koa and Hapi server. For Hapi, `applyMiddleware` registers plugins, so it should be called with `await`.

<h3 id="serverless">Serverless</h3>

Expand Down
16 changes: 2 additions & 14 deletions docs/source/features/playground.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ description: Visually exploring a Apollo Server

## Enabling Playground in Production

To enable Playground in production, an integration package must be installed to provide more control over the middlewares used. The following example uses the express integration:

```bash
npm install --save apollo-server-express@rc graphql
```

Introspection and the gui can be enabled explicitly in the following manner.
By default, Apollo Server disables introspection and GraphQL Playground in production. For some applications, it is possible to expose them in production. In those case, introspection and the gui can be enabled explicitly in the following manner.

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

const server = new ApolloServer({
Expand All @@ -30,17 +23,12 @@ const server = new ApolloServer({
introspection: true,
});

const app = express();

// gui accepts a Playground configuration
server.applyMiddleware({
app,
gui: true,
});

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

> Note: when using apollo-server-express, you can remove apollo-server from your package.json
4 changes: 2 additions & 2 deletions docs/source/features/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ For example: with an Express server already running on port 4000 that accepts Gr

```js line=12
const http = require('http');
const { ApolloServer } = require('apollo-server-express');
const { ApolloServer } = require('apollo-server');
const express = require('express');

const PORT = 4000;
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });

server.applyMiddleware({app})
server.applyMiddleware({ app })

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
Expand Down
2 changes: 1 addition & 1 deletion docs/source/migration-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Some infrastructure already contains the Engine proxy and requires it for full r

```js
const { ApolloEngine } = require('apollo-engine');
const { ApolloServer } = require('apollo-server-express');
const { ApolloServer } = require('apollo-server');
const express = require('express');

const app = express();
Expand Down
12 changes: 6 additions & 6 deletions docs/source/migration-two-dot.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const typeDefs = gql(fs.readFileSync(__dirname.concat('/schema.graphql'), 'utf8'

> Apollo Server 2.0 RC requires Node.js v6 and higher.

Apollo Server 2.0 simplifies implementing a GraphQL server. Apollo Server 1.0 revolved around providing middleware-based solutions, which had to be added to an application which already existed. These middleware implementations were tied to the HTTP server in use (e.g. `apollo-server-express` for Express implementations, `apollo-server-hapi` for hapi, etc.).
Apollo Server 2.0 simplifies implementing a GraphQL server. Apollo Server 1.0 revolved around providing middleware-based solutions, which had to be added to an application which already existed. These middleware implementations were tied to the HTTP server in use (e.g. `apollo-server-koa` for Koa implementations, `apollo-server-hapi` for hapi, etc.).

There is a consideration to be made when following the rest of the guide:

Expand All @@ -55,9 +55,9 @@ Check out the following changes for Apollo Server 2.0 RC.

<h2 id="Middleware">Middleware</h2>

With the middleware option used by Apollo Server 1.0 users, it is necessary to install the release candidate version of `apollo-server-express`. To do this, use the `rc` tag when installing:
With the middleware option used by Apollo Server 1.0 users, it is necessary to install the release candidate version of `apollo-server`. To do this, use the `rc` tag when installing:

npm install --save apollo-server-express@rc graphql
npm install --save apollo-server@rc graphql

The changes are best shown by comparing the before and after of the application.

Expand Down Expand Up @@ -104,7 +104,7 @@ Now, you can just do this instead:

```js
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { ApolloServer, gql } = require('apollo-server');

const app = express();

Expand Down Expand Up @@ -172,7 +172,7 @@ For middleware that is collocated with the GraphQL endpoint, Apollo Server 2 all

```js
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { ApolloServer, gql } = require('apollo-server');

const app = express();
const path = '/graphql';
Expand Down Expand Up @@ -256,7 +256,7 @@ Apollo Server 2 removes the `logFunction` in favor of using `graphql-extensions`
Apollo Server 2 ships with GraphQL Playground instead of GraphiQL and collocates the gui with the endpoint. GraphQL playground can be customized in the following manner.

```js
const { ApolloServer, gql } = require('apollo-server-express');
const { ApolloServer, gql } = require('apollo-server');

const server = new ApolloServer({
// These will be defined for both new or existing servers
Expand Down
20 changes: 14 additions & 6 deletions packages/apollo-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import * as express from 'express';
import * as http from 'http';
import * as net from 'net';
import { ApolloServer as ApolloServerBase } from 'apollo-server-express';
import {
ApolloServer as ApolloServerBase,
ServerRegistration as ExpressRegistration,
} from 'apollo-server-express';

export { GraphQLOptions, GraphQLExtension, gql } from 'apollo-server-core';
export type ServerRegistration = Partial<ExpressRegistration>;

export * from './exports';

Expand All @@ -23,6 +27,7 @@ export interface ServerInfo {

export class ApolloServer extends ApolloServerBase {
private httpServer: http.Server;
private app: express.Application;

private createServerInfo(
server: http.Server,
Expand Down Expand Up @@ -62,23 +67,26 @@ export class ApolloServer extends ApolloServerBase {
return serverInfo;
}

// Listen takes the same arguments as http.Server.listen.
public async listen(...opts: Array<any>): Promise<ServerInfo> {
public applyMiddleware(options: ServerRegistration) {
// This class is the easy mode for people who don't create their own express
// object, so we have to create it.
const app = express();
this.app = options.app || express();
options.app = this.app;
super.applyMiddleware(options as ExpressRegistration);
}

// Listen takes the same arguments as http.Server.listen.
public async listen(...opts: Array<any>): Promise<ServerInfo> {
// provide generous values for the getting started experience
this.applyMiddleware({
app,
path: '/',
bodyParserConfig: { limit: '50mb' },
cors: {
origin: '*',
},
});

this.httpServer = http.createServer(app);
this.httpServer = http.createServer(this.app);

if (this.subscriptionServerOptions) {
this.installSubscriptionHandlers(this.httpServer);
Expand Down