Skip to content

Commit

Permalink
Expose Cors option from vanilla ApolloServer constructor (#1335)
Browse files Browse the repository at this point in the history
* add cors option to apollo-server's constructor

* docs: add cors option to costructor for apollo-server

* expose CorsOptions from vanilla and express integrations

* Update apollo-server.md
  • Loading branch information
evans committed Jul 11, 2018
1 parent ecc5669 commit de4760b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ new ApolloServer({

The persisted queries option can be set to an object containing a `cache` field, which will be used to store the mapping between hash and query string.

* `cors`: <`Object` | `boolean`> ([apollo-server](https://github.com/expressjs/cors#cors))

Pass the integration-specific CORS options. `false` removes the CORS middleware and `true` uses the defaults. This option is only available to `apollo-server`. For other server integrations, place `cors` inside of `applyMiddleware`.

#### Returns

`ApolloServer`
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-server-express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ export {
registerServer,
ServerRegistration,
} from './ApolloServer';

export { CorsOptions } from 'cors';
export { OptionsJson } from 'body-parser';
20 changes: 20 additions & 0 deletions packages/apollo-server/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ describe('apollo-server', () => {
await apolloFetch({ query: '{hello}' });
});

it('configures cors', async () => {
server = new ApolloServer({
typeDefs,
resolvers,
cors: { origin: 'localhost' },
});

const { url: uri } = await server.listen();

const apolloFetch = createApolloFetch({ uri }).useAfter(
(response, next) => {
expect(
response.response.headers.get('access-control-allow-origin'),
).to.equal('localhost');
next();
},
);
await apolloFetch({ query: '{hello}' });
});

it('creates a healthcheck endpoint', async () => {
server = new ApolloServer({
typeDefs,
Expand Down
26 changes: 21 additions & 5 deletions packages/apollo-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
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,
CorsOptions,
} from 'apollo-server-express';
import { Config } from 'apollo-server-core';

export {
GraphQLUpload,
GraphQLOptions,
GraphQLExtension,
gql,
Config,
} from 'apollo-server-core';

export { CorsOptions } from 'apollo-server-express';

export * from './exports';

export interface ServerInfo {
Expand All @@ -27,7 +34,13 @@ export interface ServerInfo {
}

export class ApolloServer extends ApolloServerBase {
private httpServer: http.Server;
private httpServer!: http.Server;
private cors?: CorsOptions | boolean;

constructor(config: Config & { cors?: CorsOptions | boolean }) {
super(config);
this.cors = config && config.cors;
}

private createServerInfo(
server: http.Server,
Expand Down Expand Up @@ -78,9 +91,12 @@ export class ApolloServer extends ApolloServerBase {
app,
path: '/',
bodyParserConfig: { limit: '50mb' },
cors: {
origin: '*',
},
cors:
typeof this.cors !== 'undefined'
? this.cors
: {
origin: '*',
},
});

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

0 comments on commit de4760b

Please sign in to comment.