Skip to content

Commit

Permalink
Re-land "Expose composed middleware via getMiddleware()" (#3047)
Browse files Browse the repository at this point in the history
* Re-land "Expose composed middleware via getMiddleware()"

This reverts commit d05aceb which was
originally #2435, but was
reverted in 52ab22e (#3046) as a
precaution, prior to releasing 2.7.0.  This attempts to reland it.

* Use an `express.Router` rather than `compose-middleware`.

Aside from avoiding unnecessary dependencies, by using this Router, we avoid
the `apollo-server` base package from behaving different than it has in the
past.  Specifically, `apollo-server` uses `/` as the default path, but it
does so in a wild-card way which serves GraphQL requests on _any_ path.
That means that `/graphqlllll` and `/graphql` both also served the GraphQL
Playground interface, and also responded to GraphQL execution requests.

Since some may be leveraging that behavior, we should preserve it, if we
can.

* Add CHANGELOG.md link to #3184, for awareness.
  • Loading branch information
abernix authored Aug 23, 2019
1 parent 8557e85 commit 6d9c3b8
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 213 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The version headers in this history reflect the versions of Apollo Server itself

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the the appropriate changes within that release will be moved into the new section.
- _Nothing yet!_
- `apollo-server-express`, `apollo-server-koa`: A new `getMiddleware` method has been introduced, which accepts the same parameters as `applyMiddleware` with the exception of the `app` property. This allows implementors to obtain the middleware directly and "`use`" it within an existing `app`. In the near-term, this should ease some of the pain points with the previous technique. Longer-term, we are exploring what we consider to be a much more natural approach by introducing an "HTTP transport" in Apollo Server 3.x. See [this proposal issue](https://github.com/apollographql/apollo-server/issues/3184) for more information. [PR #2435](https://github.com/apollographql/apollo-server/pull/2435)

- `@apollo/federation`: `buildFederatedSchema`'s `typeDefs` parameter now accepts arrays of `DocumentNode`s (i.e. type definitions wrapped in `gql`) and `resolvers` to make the migration from a single service into a federated service easier for teams previously utilizing this pattern. [PR #3188](https://github.com/apollographql/apollo-server/pull/3188)

Expand Down
8 changes: 8 additions & 0 deletions docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ app.use('*', jwtCheck, requireAuth, checkScope);
server.applyMiddleware({ app, path: '/specialUrl' }); // app is from an existing express app. Mount Apollo middleware here. If no path is specified, it defaults to `/graphql`.
```

## `ApolloServer.getMiddleware`

Similar to the `applyMiddleware` method above, though rather than applying the composition of the various Apollo Server middlewares which comprise a full-featured Apollo Server deployment (e.g. middleware for HTTP body parsing, GraphQL Playground, uploads and subscriptions) the `getMiddleware` simply returns the middleware.

The `getMiddleware` method takes the same arguments as `applyMiddleware` **except** `app` should not be passed. Instead, the result of `getMiddleware` must be added as a middleware directly to an existing application (e.g. with `app.use(...)`).

For example, for `apollo-server-express`, this means that rather than passing `applyMiddleware` an `app` which was already initiated from calling `express()`, and `applyMiddleware` "using" (i.e. `app.use`), the implementor will instead call `app.use(...)` on the result of `getMiddleware` with the same arguments.

## `gql`

The `gql` is a [template literal tag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates). Template literals were introduced in recent versions of ECMAScript to provide embedded expressions (i.e. `` `A string with interpolated ${variables}` ``) and template literal tags exist to provide additional functionality for what would otherwise be a normal template literal.
Expand Down
Loading

0 comments on commit 6d9c3b8

Please sign in to comment.