diff --git a/CHANGELOG.md b/CHANGELOG.md index ecfa7a493c1..61fde8e0541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### vNEXT - `apollo-server-koa`: Support OPTIONS requests [PR #2288](https://github.com/apollographql/apollo-server/pull/2288) +- Add `req` and `res` typings to the `ContextFunction` argument for apollo-server and apollo-server-express. Update `ContextFunction` return type to allow returning a value syncronously. [PR #2330](https://github.com/apollographql/apollo-server/pull/2330) ### v2.4.2 diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index 88358a7696a..b0d79611dd3 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -30,7 +30,7 @@ export { KeyValueCache } from 'apollo-server-caching'; export type Context = T; export type ContextFunction = ( context: Context, -) => Promise>; +) => Context | Promise>; // A plugin can return an interface that matches `ApolloServerPlugin`, or a // factory function that returns `ApolloServerPlugin`. diff --git a/packages/apollo-server-express/src/ApolloServer.ts b/packages/apollo-server-express/src/ApolloServer.ts index dae1b59dbad..b432ec45672 100644 --- a/packages/apollo-server-express/src/ApolloServer.ts +++ b/packages/apollo-server-express/src/ApolloServer.ts @@ -1,5 +1,5 @@ import express from 'express'; -import corsMiddleware from 'cors'; +import corsMiddleware, { CorsOptions } from 'cors'; import { json, OptionsJson } from 'body-parser'; import { renderPlaygroundPage, @@ -11,6 +11,9 @@ import { ApolloServerBase, formatApolloErrors, processFileUploads, + ContextFunction, + Context, + Config, } from 'apollo-server-core'; import accepts from 'accepts'; import typeis from 'type-is'; @@ -67,7 +70,21 @@ const fileUploadMiddleware = ( } }; +interface ExpressContext { + req: express.Request; + res: express.Response; +} + +export interface ApolloServerExpressConfig extends Config { + cors?: CorsOptions | boolean; + context?: ContextFunction | Context; +} + export class ApolloServer extends ApolloServerBase { + constructor(config: ApolloServerExpressConfig) { + super(config); + } + // This translates the arguments from the middleware into graphQL options It // provides typings for the integration specific behavior, ideally this would // be propagated with a generic to the super class diff --git a/packages/apollo-server-express/src/index.ts b/packages/apollo-server-express/src/index.ts index 22a3b9e8d76..dca6d3bd9e2 100644 --- a/packages/apollo-server-express/src/index.ts +++ b/packages/apollo-server-express/src/index.ts @@ -26,6 +26,7 @@ export { ApolloServer, registerServer, ServerRegistration, + ApolloServerExpressConfig, } from './ApolloServer'; export { CorsOptions } from 'cors'; diff --git a/packages/apollo-server/src/index.ts b/packages/apollo-server/src/index.ts index e92623fb9ee..6b9308eb279 100644 --- a/packages/apollo-server/src/index.ts +++ b/packages/apollo-server/src/index.ts @@ -8,8 +8,8 @@ import net from 'net'; import { ApolloServer as ApolloServerBase, CorsOptions, + ApolloServerExpressConfig, } from 'apollo-server-express'; -import { Config } from 'apollo-server-core'; export * from './exports'; @@ -27,7 +27,7 @@ export class ApolloServer extends ApolloServerBase { private httpServer?: http.Server; private cors?: CorsOptions | boolean; - constructor(config: Config & { cors?: CorsOptions | boolean }) { + constructor(config: ApolloServerExpressConfig) { super(config); this.cors = config && config.cors; }