From 8aba81fcb67dc600fc2547fcc5565393d756f8ff Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Thu, 21 Feb 2019 10:35:12 -0500 Subject: [PATCH 1/5] Add AS constructor with context usage to tests --- .../apollo-server/src/__tests__/index.test.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/packages/apollo-server/src/__tests__/index.test.ts b/packages/apollo-server/src/__tests__/index.test.ts index 56001d97bff..0d739c2b166 100644 --- a/packages/apollo-server/src/__tests__/index.test.ts +++ b/packages/apollo-server/src/__tests__/index.test.ts @@ -24,6 +24,51 @@ describe('apollo-server', () => { it('accepts typeDefs and mocks', () => { expect(() => new ApolloServer({ typeDefs, mocks: true })).not.toThrow; }); + + describe('context field', () => { + describe('as a function', () => { + it('can accept and return `req`', () => { + expect( + new ApolloServer({ + typeDefs, + resolvers, + context: ({ req }) => ({ req }), + }), + ).not.toThrow; + }); + + it('can accept nothing and return an empty object', () => { + expect( + new ApolloServer({ + typeDefs, + resolvers, + context: () => ({}), + }), + ).not.toThrow; + }); + }); + }); + describe('as an object', () => { + it('can be an empty object', () => { + expect( + new ApolloServer({ + typeDefs, + resolvers, + context: {}, + }), + ).not.toThrow; + }); + + it('can contain arbitrary values', () => { + expect( + new ApolloServer({ + typeDefs, + resolvers, + context: { value: 'arbitrary' }, + }), + ).not.toThrow; + }); + }); }); describe('without registerServer', () => { From d05212e1623726341a33ee047be7fd4ccde7a86a Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Thu, 21 Feb 2019 11:27:00 -0500 Subject: [PATCH 2/5] fix(typing): Context template type should be 'object' and not 'any' ```context?: ContextFunction | Context;``` is ```context?: ContextFunction | any;``` which is ```context?: any;``` https://github.com/Microsoft/TypeScript/issues/18568 Since the ContextFunction<...> part is ignored, no type hinting will be provided when a function is passed to `context`. We don't really mean `any`, we mean an object with any shape, so we should use `object` instead --- packages/apollo-server-core/src/types.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index e083da43866..f461fbc9b02 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -29,10 +29,10 @@ export { GraphQLSchemaModule }; export { KeyValueCache } from 'apollo-server-caching'; -export type Context = T; export type ContextFunction = ( context: Context, ) => Context | Promise>; +export type Context = T; // A plugin can return an interface that matches `ApolloServerPlugin`, or a // factory function that returns `ApolloServerPlugin`. @@ -50,7 +50,7 @@ export interface SubscriptionServerOptions { } type BaseConfig = Pick< - GraphQLOptions>, + GraphQLOptions, | 'formatError' | 'debug' | 'rootValue' @@ -71,11 +71,11 @@ export interface Config extends BaseConfig { resolvers?: IResolvers; schema?: GraphQLSchema; schemaDirectives?: Record; - context?: Context | ContextFunction; + context?: Context | ContextFunction; introspection?: boolean; mocks?: boolean | IMocks; mockEntireSchema?: boolean; - engine?: boolean | EngineReportingOptions>; + engine?: boolean | EngineReportingOptions; extensions?: Array<() => GraphQLExtension>; cacheControl?: CacheControlExtensionOptions | boolean; plugins?: PluginDefinition[]; From d08569ef9aaeb8a05aa15fe8438253fde182959d Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Thu, 21 Feb 2019 11:31:22 -0500 Subject: [PATCH 3/5] fix(typing): type ContextFunction params separately from resulting context object --- packages/apollo-server-core/src/types.ts | 6 +++--- packages/apollo-server-express/src/ApolloServer.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index f461fbc9b02..070f2cebf02 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -29,10 +29,10 @@ export { GraphQLSchemaModule }; export { KeyValueCache } from 'apollo-server-caching'; -export type ContextFunction = ( - context: Context, -) => Context | Promise>; export type Context = T; +export type ContextFunction = ( + context: FunctionParams, +) => 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 b432ec45672..c13c7ac0aa6 100644 --- a/packages/apollo-server-express/src/ApolloServer.ts +++ b/packages/apollo-server-express/src/ApolloServer.ts @@ -77,7 +77,7 @@ interface ExpressContext { export interface ApolloServerExpressConfig extends Config { cors?: CorsOptions | boolean; - context?: ContextFunction | Context; + context?: ContextFunction | Context; } export class ApolloServer extends ApolloServerBase { From 816d9a89d00ea80f89049ac18bc65814e1aa1209 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Thu, 21 Feb 2019 11:40:58 -0500 Subject: [PATCH 4/5] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8505e8746c7..f4de992dc82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ### vNEXT +- Fix typing for ContextFunction incorrectly requiring the context object the function produces to match the parameters of the function [PR #2350](https://github.com/apollographql/apollo-server/pull/2350) ### v2.4.3 From 3eabfe58cc85e38875642149ffa049e3ae01097f Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Thu, 21 Feb 2019 11:42:30 -0500 Subject: [PATCH 5/5] rename: ContextContent -> ProducedContext --- packages/apollo-server-core/src/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index 070f2cebf02..1affbeafe8a 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -30,9 +30,9 @@ export { GraphQLSchemaModule }; export { KeyValueCache } from 'apollo-server-caching'; export type Context = T; -export type ContextFunction = ( +export type ContextFunction = ( context: FunctionParams, -) => Context | Promise>; +) => Context | Promise>; // A plugin can return an interface that matches `ApolloServerPlugin`, or a // factory function that returns `ApolloServerPlugin`.