Expose Cors option from vanilla ApolloServer constructor (#1335)

* 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
This commit is contained in:
Evans Hauser 2018-07-11 13:46:51 -07:00 committed by GitHub
parent ecc56690df
commit de4760ba3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 5 deletions

View file

@ -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`

View file

@ -21,3 +21,6 @@ export {
registerServer,
ServerRegistration,
} from './ApolloServer';
export { CorsOptions } from 'cors';
export { OptionsJson } from 'body-parser';

View file

@ -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,

View file

@ -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 {
@ -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,
@ -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);