mercurius-js / mercurius

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

Possibly incomplete return type of subscription.onConnect #871

Closed mabecth closed 2 years ago

mabecth commented 2 years ago

I am trying to setup subscriptions and need some additional logic around connecting the client to the server using Websockets. After reading the docs on the subscription.onConnect method the return type is supposed to be "truthy" OR and object. When using mericurius in a TypeScript project the return type is set to Record<string, any> | Promise<Record<string, any>> but this will not allow for returning true/false which seem ok according to both the docs and the code itself.

The docs clearly states that:

subscription.onConnect: Function A function which can be used to validate the connection_init payload. If defined it should return a truthy value to authorize the connection. If it returns an object the subscription context will be extended with the returned object.

Returning true/false in onConnect gives me the functionality I need but TypeScript is not happy with that. How are you supposed to return a truthy OR falsy value when the return type is set to Record<string, any> | Promise<Record<string, any>>? I would propose extending the return type with boolean to handle this inconsistency.

mcollina commented 2 years ago

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