Skip to content

Commit

Permalink
Address TODO(AS4) in express4 directory (#6952)
Browse files Browse the repository at this point in the history
Also make test subdirectory match source subdirectory.
  • Loading branch information
glasser committed Sep 27, 2022
1 parent 88968c5 commit 025b5c9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .changeset/light-kings-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import request from 'supertest';
import compression, { filter as defaultFilter } from 'compression';
import { ApolloServer } from '../../index.js';
import { ApolloServer, BaseContext } from '../../index.js';
import { expressMiddleware } from '../../express4/index.js';
import { it, expect } from '@jest/globals';
import resolvable from '@josephg/resolvable';
Expand Down Expand Up @@ -29,6 +29,27 @@ it('not calling start causes a clear error', async () => {
);
});

it('context optional only if TContext=BaseContext', async () => {
const baseContextServer = new ApolloServer<BaseContext>({
typeDefs: 'type Query{x:ID}',
});
await baseContextServer.start();
const differentContextServer = new ApolloServer<{ x: number }>({
typeDefs: 'type Query{x:ID}',
});
await differentContextServer.start();

// This is a typechecking test, so we don't actually do anything with these
// middlewares.
expressMiddleware(baseContextServer);
expressMiddleware(baseContextServer, { context: async () => ({}) });
expressMiddleware(differentContextServer, {
context: async () => ({ x: 5 }),
});
// @ts-expect-error
expressMiddleware(differentContextServer);
});

// This test validates that you can use incremental delivery with the
// `compression` package (which requires a hacky `res.flush()` call in the
// middleware).
Expand Down
3 changes: 0 additions & 3 deletions packages/server/src/express4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export interface ExpressMiddlewareOptions<TContext extends BaseContext> {
context?: ContextFunction<[ExpressContextFunctionArgument], TContext>;
}

// TODO(AS4): Figure out exact naming (eg is this Express-specific or just Node
// HTTP?)
// TODO(AS4): Write compilation tests about the context optionality stuff.
export function expressMiddleware(
server: ApolloServer<BaseContext>,
options?: ExpressMiddlewareOptions<BaseContext>,
Expand Down

0 comments on commit 025b5c9

Please sign in to comment.