Skip to content

Commit

Permalink
Upgrade TS to 3.7.3 (#3618)
Browse files Browse the repository at this point in the history
This commit updates our typescript version to latest. It also
incorporates related fixes (workarounds?) that have blocked
the upgrade since 3.6.3. I haven't yet been able to identify
the underlying issue, nor do I think it's worth my time at this
point.

The fixes proposed here do limit some of the generic-ness of the
Dispatcher class and its functions, however I'd argue that the
level of said generic-ness is unnecessary.

* The Dispatcher really only needs to accept a GraphQLListener
* A GraphQLListener _only_ has methods for properties, so the
  hoops we previously jumped through to collect the names of
  strictly function properties on an object is unnecessary and
  greatly simplified by 'keyof T'.

It's worth mentioning a couple things:

* No type inference is lost where we consume Dispatcher.
  Autocomplete still smartly suggests the allowed function
  names of a GraphQLListener as we would hope
* The types being modified don't exist in the public API, so
  we shouldn't have any concerns with breaking type changes
  to the Dispatcher class.
  • Loading branch information
trevor-scheer authored Dec 17, 2019
1 parent b09c099 commit 1169db6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The version headers in this history reflect the versions of Apollo Server itself

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the the appropriate changes within that release will be moved into the new section.
- _Nothing yet. Stay tuned!_
- `apollo-server-core`: Upgrade TS to 3.7.3 [#3618](https://github.com/apollographql/apollo-server/pull/3618)

### v2.9.14

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"supertest": "4.0.2",
"test-listen": "1.1.0",
"ts-jest": "24.2.0",
"typescript": "3.6.2",
"typescript": "3.7.3",
"ws": "6.2.1"
},
"jest": {
Expand Down
20 changes: 6 additions & 14 deletions packages/apollo-server-core/src/utils/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { GraphQLRequestListener } from "apollo-server-plugin-base";

type AnyFunction = (...args: any[]) => any;
type Args<F> = F extends (...args: infer A) => any ? A : never;
type FunctionPropertyNames<T, F extends AnyFunction = AnyFunction> = {
[K in keyof T]: T[K] extends F ? K : never;
}[keyof T];
type AsFunction<F> = F extends AnyFunction ? F : never;
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;

type DidEndHook<TArgs extends any[]> = (...args: TArgs) => void;

export class Dispatcher<T> {
export class Dispatcher<T extends GraphQLRequestListener> {
constructor(protected targets: T[]) {}

public async invokeHookAsync<
TMethodName extends FunctionPropertyNames<Required<T>>
>(
public async invokeHookAsync<TMethodName extends keyof T>(
methodName: TMethodName,
...args: Args<T[TMethodName]>
): Promise<UnwrapPromise<ReturnType<AsFunction<T[TMethodName]>>>[]> {
Expand All @@ -27,9 +24,7 @@ export class Dispatcher<T> {
);
}

public async invokeHooksUntilNonNull<
TMethodName extends FunctionPropertyNames<Required<T>>
>(
public async invokeHooksUntilNonNull<TMethodName extends keyof T>(
methodName: TMethodName,
...args: Args<T[TMethodName]>
): Promise<UnwrapPromise<ReturnType<AsFunction<T[TMethodName]>>> | null> {
Expand All @@ -47,10 +42,7 @@ export class Dispatcher<T> {
}

public invokeDidStartHook<
TMethodName extends FunctionPropertyNames<
Required<T>,
(...args: any[]) => AnyFunction | void
>,
TMethodName extends keyof T,
TEndHookArgs extends Args<ReturnType<AsFunction<T[TMethodName]>>>
>(
methodName: TMethodName,
Expand Down

0 comments on commit 1169db6

Please sign in to comment.