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

apollo-server-micro: only serve landing page from graphql path #5516

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The version headers in this history reflect the versions of Apollo Server itself
## vNEXT

- `apollo-server-types`: TypeScript typings for `info.cacheControl` are now added to `GraphQLResolveInfo` as part of `apollo-server-types` rather than a nested file in `apollo-server-core`, and the field now has a named type, `ResolveInfoCacheControl`. [PR #5512](https://github.com/apollographql/apollo-server/pull/5512)
- `apollo-server-micro`: Like the other framework integrations, only serve landing pages from the GraphQL path (`/graphql` by default, configurable via the `path` option to `createHandler`). [PR #5516](https://github.com/apollographql/apollo-server/pull/5516)

## v3.0.1

Expand Down
5 changes: 3 additions & 2 deletions packages/apollo-server-micro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ npm install micro micro-cors apollo-server-micro graphql
```js
const cors = require('micro-cors')(); // highlight-line
const { ApolloServer, gql } = require('apollo-server-micro');
const { send } = require('micro');

const typeDefs = gql`
type Query {
Expand All @@ -89,8 +90,8 @@ const resolvers = {

const apolloServer = new ApolloServer({ typeDefs, resolvers });
module.exports = apolloServer.start().then(() => {
const handler = apolloServer.createHandler(); // highlight-line
return cors((req, res) => req.method === 'OPTIONS' ? res.end() : handler(req, res)) // highlight-line
const handler = apolloServer.createHandler();
return cors((req, res) => req.method === 'OPTIONS' ? send(res, 200, 'ok') : handler(req, res))
});
```

Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-server-micro/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export class ApolloServer extends ApolloServerBase {
}): boolean {
let handled = false;

if (req.method === 'GET') {
const url = req.url!.split('?')[0];
if (req.method === 'GET' && url === this.graphqlPath) {
const accept = parseAll(req.headers);
const types = accept.mediaTypes as string[];
const prefersHtml =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('apollo-server-micro', function () {
);

const body = await rp({
uri,
uri: `${uri}/graphql`,
method: 'GET',
headers: {
accept:
Expand Down