mercurius icon indicating copy to clipboard operation
mercurius copied to clipboard

Improve PubSub typescript typings

Open f15u opened this issue 4 years ago • 4 comments

I'm using the default mercurius' PubSub implementation, but typings do not help working with Typescript and code-first libraries like gqtx or nexus because every subscribe or publish call needs to be manually typed.

The following should cover most of the use cases without breaking old implementation:

- export interface PubSub {
+ export interface PubSub<TTopic = string, TResult = any> {
-  subscribe<TResult = any>(topics: string | string[]): Promise<Readable & AsyncIterableIterator<TResult>>;
+  subscribe(topics: TTopic | TTopic[]): Promise<Readable & AsyncIterableIterator<TResult>>;
-  publish<TResult = any>(event: { topic: string; payload: TResult }, callback?: () => void): void;
+  publish<R = Result>(event: { topic: TTopic; payload: R }, callback?: () => void): void;
}

f15u avatar Mar 01 '21 11:03 f15u

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Mar 01 '21 12:03 mcollina

For sure. I already done the update, but I don't know how to test this particular refactoring. Any hint or suggestion?

f15u avatar Mar 19 '21 13:03 f15u

I don't know, I imagine this was sparked from some real-world issue.

mcollina avatar Mar 19 '21 13:03 mcollina

It is a real-world issue (the code in the first comment is actually used in our repo) but we don't test those types or we have to support old installation of this library.

I'll try to update types.ts to reflect this change.

f15u avatar Mar 19 '21 14:03 f15u