Skip to content

Commit

Permalink
Fix: allow context return value to be anything
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Feb 21, 2019
1 parent 4355f2b commit 28a2651
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### vNEXT

- `apollo-server-express`: Export `ExpressContext`
- `apollo-server-express`: Fix so you can return anything in `context`
- `apollo-server-express`: Ability to specify a context interface to `ApolloServerExpressConfig`
- `apollo-server-core`: Update `ContextFunction` to allow both an incoming and outcoming context

### v2.4.3

Expand Down
11 changes: 7 additions & 4 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ export { GraphQLSchemaModule };
export { KeyValueCache } from 'apollo-server-caching';

export type Context<T = any> = T;
export type ContextFunction<T = any> = (
context: Context<T>,
) => Context<T> | Promise<Context<T>>;
export type ContextFunction<
T_IncomingContext = any,
T_OutgoingContext = any
> = (
context: Context<T_IncomingContext>,
) => Context<T_OutgoingContext> | Promise<Context<T_OutgoingContext>>;

// A plugin can return an interface that matches `ApolloServerPlugin`, or a
// factory function that returns `ApolloServerPlugin`.
Expand Down Expand Up @@ -71,7 +74,7 @@ export interface Config extends BaseConfig {
resolvers?: IResolvers;
schema?: GraphQLSchema;
schemaDirectives?: Record<string, typeof SchemaDirectiveVisitor>;
context?: Context<any> | ContextFunction<any>;
context?: Context<any> | ContextFunction<any, any>;
introspection?: boolean;
mocks?: boolean | IMocks;
mockEntireSchema?: boolean;
Expand Down
7 changes: 5 additions & 2 deletions packages/apollo-server-express/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ export interface ExpressContext {
res: express.Response;
}

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

export class ApolloServer extends ApolloServerBase {
Expand Down
32 changes: 30 additions & 2 deletions packages/apollo-server-express/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describe('apollo-server-express', () => {
let app: express.Application;
let httpServer: http.Server;

async function createServer(
serverOptions: ApolloServerExpressConfig,
async function createServer<T_OutgoingContext = any>(
serverOptions: ApolloServerExpressConfig<T_OutgoingContext>,
options: Partial<ServerRegistration> = {},
) {
server = new ApolloServer(serverOptions);
Expand All @@ -85,6 +85,34 @@ describe('apollo-server-express', () => {
it('accepts typeDefs and resolvers', () => {
return createServer({ typeDefs, resolvers });
});
describe('context', () => {
it('can be any value', () => {
return createServer({
typeDefs,
resolvers,
context: { userId: '1123' },
});
});
it('can be inferred by interface', () => {
interface MyContext {
token: string | null;
}

return createServer<MyContext>({
typeDefs,
resolvers,
context: ({ req }) => {
const token =
req.headers.authorization &&
req.headers.authorization.split(' ')[1];

return {
token: token || null,
};
},
});
});
});
});

describe('applyMiddleware', () => {
Expand Down

0 comments on commit 28a2651

Please sign in to comment.