Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type parameters for context function separately from the context it produces #2350

Merged
merged 5 commits into from
Feb 21, 2019

Conversation

cheapsteak
Copy link
Member

Inspired by #2346 , this PR fixes the problem introduced by #2330 and should be solve the problem brought up in #1593

The two significant changes are

  • Type parameters for context function are now separate from the context it produces
    export type ContextFunction<FunctionParams = any, ContextContent = object> = (
    context: FunctionParams,
    ) => Context<ContextContent> | Promise<Context<ContextContent>>;
    (so apollo-server-express can say the context function will receive req and, but the resulting context doesn't need to contain (or only contain) req)
  • The type parameter for the Context object is updated from any to object
    export type Context<T = object> = T;
    , which I believe is what we really meant

TODO:

  • Update CHANGELOG.md with your change (include reference to issue & this PR)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass

```context?: ContextFunction<ExpressContext, any> | Context<any>;```
is
```context?: ContextFunction<ExpressContext, any> | any;```
which is
```context?: any;```

microsoft/TypeScript#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
Copy link
Member

@abernix abernix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -24,6 +24,51 @@ describe('apollo-server', () => {
it('accepts typeDefs and mocks', () => {
expect(() => new ApolloServer({ typeDefs, mocks: true })).not.toThrow;
});

describe('context field', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are a super welcome addition. Though I suspect we should put most of these in apollo-server-integration-testsuite to make sure they are exercised on each of the integration flavors (e.g. hapi, express, etc.), possibly alongside the existing constructor tests:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, I think this can be a follow-up PR. ^

@cheapsteak cheapsteak merged commit a4ebadc into master Feb 21, 2019
@cheapsteak cheapsteak deleted the chang/fix/contextFunction-types branch February 21, 2019 17:49
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants