mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
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:
parent
ecc56690df
commit
de4760ba3b
4 changed files with 48 additions and 5 deletions
|
@ -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`
|
||||
|
|
|
@ -21,3 +21,6 @@ export {
|
|||
registerServer,
|
||||
ServerRegistration,
|
||||
} from './ApolloServer';
|
||||
|
||||
export { CorsOptions } from 'cors';
|
||||
export { OptionsJson } from 'body-parser';
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue