Skip to content

Commit

Permalink
Provide more accurate types for apollo-server-express's ContextFunc…
Browse files Browse the repository at this point in the history
…tion (#2330)

* typing: context function could return value synchronously

* Create ApolloServerExpressConfig type that has req and res in context

* Have apollo-server-express's constructor also use the express-specific config

* Update changelog
  • Loading branch information
cheapsteak authored and abernix committed Feb 19, 2019
1 parent 2f77362 commit 6308996
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export { KeyValueCache } from 'apollo-server-caching';
export type Context<T = any> = T;
export type ContextFunction<T = any> = (
context: Context<T>,
) => Promise<Context<T>>;
) => Context<T> | Promise<Context<T>>;

// A plugin can return an interface that matches `ApolloServerPlugin`, or a
// factory function that returns `ApolloServerPlugin`.
Expand Down
19 changes: 18 additions & 1 deletion packages/apollo-server-express/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,6 +11,9 @@ import {
ApolloServerBase,
formatApolloErrors,
processFileUploads,
ContextFunction,
Context,
Config,
} from 'apollo-server-core';
import accepts from 'accepts';
import typeis from 'type-is';
Expand Down Expand Up @@ -67,7 +70,21 @@ const fileUploadMiddleware = (
}
};

interface ExpressContext {
req: express.Request;
res: express.Response;
}

export interface ApolloServerExpressConfig extends Config {
cors?: CorsOptions | boolean;
context?: ContextFunction<ExpressContext> | Context<ExpressContext>;
}

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
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export {
ApolloServer,
registerServer,
ServerRegistration,
ApolloServerExpressConfig,
} from './ApolloServer';

export { CorsOptions } from 'cors';
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
}
Expand Down

0 comments on commit 6308996

Please sign in to comment.