n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
771 stars 119 forks source link

[core] Add an option for disallowing `useOnResolve` usage #2225

Open EmrysMyrddin opened 2 months ago

EmrysMyrddin commented 2 months ago

Context

In the past, Envelop allowed to hook into the execution at resolver level, meaning plugin was able to replace or wrap any resolver. This was causing performance issues, since wrapping every resolver added a significant overhead.

To circumvent this issue, we removed this onResolve hook and replaced it by a plugin useOnResolve, which wraps each resolvers only when used, meaning other users doesn't have perf issues for this feature.

But it turns out we think this should not be part of Envelop at all. Envelop is meant as a runtime plugin system. This means that schema modifications should happen at schema build time, and wrapping resolvers is a schema modification.

Since this change is a breaking change and will need the refactoring of multiple plugins, it will takes time. Meanwhile, we should allow users or library maintainers using Envelop to enforce the fact the schema is not mutated during Envelop execution.

Solution

Add an option to Envelop to globally disallow the usage of useOnResolve. If this option is enabled, useOnResolve should throw a comprehensive error. It could also be a good thing to make this error message configurable, so that library maintainers can explain why this plugin is disallowed.

Alternatives

An alternative could be to freeze the schema object. But this has a cost, and the error will not be clear from a user perspective.

Additional context

This is not a perfect solution, but it's mean to gracefully guide users and library maintainers towards the removal of useOnResolve.