mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.34k stars 234 forks source link

Improve PubSub typescript typings #414

Open FedericoBiccheddu opened 3 years ago

FedericoBiccheddu commented 3 years ago

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;
}
mcollina commented 3 years ago

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

FedericoBiccheddu commented 3 years ago

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

mcollina commented 3 years ago

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

FedericoBiccheddu commented 3 years ago

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.