From 47d0e4221f86fdcff7070186e75777cdfddac32b Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Wed, 11 Jul 2018 09:38:08 -0700 Subject: [PATCH 1/4] add cors option to apollo-server's constructor --- packages/apollo-server/src/index.test.ts | 20 ++++++++++++++++++++ packages/apollo-server/src/index.ts | 20 ++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/apollo-server/src/index.test.ts b/packages/apollo-server/src/index.test.ts index 8afe2ce35d3..b43be73523e 100644 --- a/packages/apollo-server/src/index.test.ts +++ b/packages/apollo-server/src/index.test.ts @@ -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, diff --git a/packages/apollo-server/src/index.ts b/packages/apollo-server/src/index.ts index ff285cd76c4..4824efec036 100644 --- a/packages/apollo-server/src/index.ts +++ b/packages/apollo-server/src/index.ts @@ -5,13 +5,16 @@ import * as express from 'express'; import * as http from 'http'; import * as net from 'net'; +import { CorsOptions } from 'cors'; import { ApolloServer as ApolloServerBase } from 'apollo-server-express'; +import { Config } from 'apollo-server-core'; export { GraphQLUpload, GraphQLOptions, GraphQLExtension, gql, + Config, } from 'apollo-server-core'; export * from './exports'; @@ -27,7 +30,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 +87,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); From ff8787ffeded61a472aa35a1795b6ad5b94d6ba6 Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Wed, 11 Jul 2018 09:40:50 -0700 Subject: [PATCH 2/4] docs: add cors option to costructor for apollo-server --- docs/source/api/apollo-server.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/source/api/apollo-server.md b/docs/source/api/apollo-server.md index 5d4db3ae236..25fed15a1e2 100644 --- a/docs/source/api/apollo-server.md +++ b/docs/source/api/apollo-server.md @@ -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-sever. For other server integrations, place `cors` inside of `applyMiddleware`. + #### Returns `ApolloServer` From 7539b6784f0f1651d6739114f6e655ef7f899137 Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Wed, 11 Jul 2018 09:46:39 -0700 Subject: [PATCH 3/4] expose CorsOptions from vanilla and express integrations --- packages/apollo-server-express/src/index.ts | 3 +++ packages/apollo-server/src/index.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/apollo-server-express/src/index.ts b/packages/apollo-server-express/src/index.ts index 839fcafe6d5..102993062bb 100644 --- a/packages/apollo-server-express/src/index.ts +++ b/packages/apollo-server-express/src/index.ts @@ -21,3 +21,6 @@ export { registerServer, ServerRegistration, } from './ApolloServer'; + +export { CorsOptions } from 'cors'; +export { OptionsJson } from 'body-parser'; diff --git a/packages/apollo-server/src/index.ts b/packages/apollo-server/src/index.ts index 4824efec036..99e88cc802c 100644 --- a/packages/apollo-server/src/index.ts +++ b/packages/apollo-server/src/index.ts @@ -5,8 +5,10 @@ import * as express from 'express'; import * as http from 'http'; import * as net from 'net'; -import { CorsOptions } from 'cors'; -import { ApolloServer as ApolloServerBase } from 'apollo-server-express'; +import { + ApolloServer as ApolloServerBase, + CorsOptions, +} from 'apollo-server-express'; import { Config } from 'apollo-server-core'; export { @@ -17,6 +19,8 @@ export { Config, } from 'apollo-server-core'; +export { CorsOptions } from 'apollo-server-express'; + export * from './exports'; export interface ServerInfo { From 70ad5eccb9bac967af9b511a640fa236702b36c1 Mon Sep 17 00:00:00 2001 From: Martijn Walraven Date: Wed, 11 Jul 2018 10:03:30 -0700 Subject: [PATCH 4/4] Update apollo-server.md --- docs/source/api/apollo-server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/api/apollo-server.md b/docs/source/api/apollo-server.md index 25fed15a1e2..3ebfd4c070a 100644 --- a/docs/source/api/apollo-server.md +++ b/docs/source/api/apollo-server.md @@ -88,7 +88,7 @@ new ApolloServer({ * `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-sever. For other server integrations, place `cors` inside of `applyMiddleware`. + 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