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

Expose Cors option from vanilla ApolloServer constructor #1335

Merged
merged 4 commits into from Jul 11, 2018
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
4 changes: 4 additions & 0 deletions docs/source/api/apollo-server.md
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
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
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
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