From 98595b9f139c4b9d86fb1f2c2f9fc425aa3878fe Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Fri, 15 Feb 2019 14:41:57 -0500 Subject: [PATCH 1/4] typing: context function could return value synchronously --- packages/apollo-server-core/src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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`. From b5b494bc03031af73cd7ad0080bec699d9a30588 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Fri, 15 Feb 2019 16:30:05 -0500 Subject: [PATCH 2/4] Create ApolloServerExpressConfig type that has req and res in context --- .../apollo-server-express/src/ApolloServer.ts | 15 ++++++++++++++- packages/apollo-server-express/src/index.ts | 1 + packages/apollo-server/src/index.ts | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/apollo-server-express/src/ApolloServer.ts b/packages/apollo-server-express/src/ApolloServer.ts index dae1b59dbad..d99cf38b975 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,6 +70,16 @@ 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 { // This translates the arguments from the middleware into graphQL options It // provides typings for the integration specific behavior, ideally this would 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; } From 0d0b511988ae28d1627f2101a205ca4d5a5d954e Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Fri, 15 Feb 2019 16:59:28 -0500 Subject: [PATCH 3/4] Have apollo-server-express's constructor also use the express-specific config --- packages/apollo-server-express/src/ApolloServer.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/apollo-server-express/src/ApolloServer.ts b/packages/apollo-server-express/src/ApolloServer.ts index d99cf38b975..b432ec45672 100644 --- a/packages/apollo-server-express/src/ApolloServer.ts +++ b/packages/apollo-server-express/src/ApolloServer.ts @@ -81,6 +81,10 @@ export interface ApolloServerExpressConfig extends Config { } 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 From 0587be85c836d00c004c861615691451d0851618 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Fri, 15 Feb 2019 17:07:19 -0500 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4022536bec..502f5adb7ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### vNEXT +- 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 - `apollo-server-fastify` is now on Apollo Server and lives within the `apollo-server` repository. This is being introduced in a _patch_ version, however it's a _major_ version bump from the last time `apollo-server-fastify` was published under `1.0.2`. [PR #1971](https://github.com/apollostack/apollo-server/pull/1971)