diff --git a/docs/source/api/apollo-server.md b/docs/source/api/apollo-server.md index 50b2a464288..d15b1caace7 100644 --- a/docs/source/api/apollo-server.md +++ b/docs/source/api/apollo-server.md @@ -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({ diff --git a/docs/source/essentials/server.md b/docs/source/essentials/server.md index 8408c70efb4..542873ceee5 100644 --- a/docs/source/essentials/server.md +++ b/docs/source/essentials/server.md @@ -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.

Creating a server

@@ -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({ @@ -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`.

Serverless

diff --git a/docs/source/features/playground.md b/docs/source/features/playground.md index 230139ecbdb..f60d89f2f28 100644 --- a/docs/source/features/playground.md +++ b/docs/source/features/playground.md @@ -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({ @@ -30,11 +23,8 @@ const server = new ApolloServer({ introspection: true, }); -const app = express(); - // gui accepts a Playground configuration server.applyMiddleware({ - app, gui: true, }); @@ -42,5 +32,3 @@ 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 diff --git a/docs/source/features/subscriptions.md b/docs/source/features/subscriptions.md index e69c35df1d7..3a4dfdfab3b 100644 --- a/docs/source/features/subscriptions.md +++ b/docs/source/features/subscriptions.md @@ -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); diff --git a/docs/source/migration-engine.md b/docs/source/migration-engine.md index f1e786018e2..871eeb1a36d 100644 --- a/docs/source/migration-engine.md +++ b/docs/source/migration-engine.md @@ -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(); diff --git a/docs/source/migration-two-dot.md b/docs/source/migration-two-dot.md index 91e6ec8f643..81a7a88709e 100644 --- a/docs/source/migration-two-dot.md +++ b/docs/source/migration-two-dot.md @@ -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: @@ -55,9 +55,9 @@ Check out the following changes for Apollo Server 2.0 RC.

Middleware

-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. @@ -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(); @@ -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'; @@ -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 diff --git a/packages/apollo-server/src/index.ts b/packages/apollo-server/src/index.ts index f623abb2e72..2b6d03ee27e 100644 --- a/packages/apollo-server/src/index.ts +++ b/packages/apollo-server/src/index.ts @@ -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; export * from './exports'; @@ -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, @@ -62,15 +67,18 @@ export class ApolloServer extends ApolloServerBase { return serverInfo; } - // Listen takes the same arguments as http.Server.listen. - public async listen(...opts: Array): Promise { + 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): Promise { // provide generous values for the getting started experience this.applyMiddleware({ - app, path: '/', bodyParserConfig: { limit: '50mb' }, cors: { @@ -78,7 +86,7 @@ export class ApolloServer extends ApolloServerBase { }, }); - this.httpServer = http.createServer(app); + this.httpServer = http.createServer(this.app); if (this.subscriptionServerOptions) { this.installSubscriptionHandlers(this.httpServer);