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

`useOnResolve` incorrectly calls `onResolve` multiple times #2222

Open magJ opened 2 months ago

magJ commented 2 months ago

Summary

If you have:

then field.resolve will be wrapped multiple times and onResolve will be triggered multiple times.

Detail

The number of redundant calls will be proportional to the number of useOnResolve plugins, and the number of times onSchemaChange is called.

This happens because the hasWrappedResolve symbol is private to a single plugin instance.

Reproduction

Consider the following scenario:

So far, so good, each plugin will still behave as expected(onResolve is still only called once for each plugin when a resolver is invoked), however:

Now when a resolver is called onResolve ends up getting called twice per plugin.

Suggested solution

Side-note, the parentResolver symbol should probably be publicly exported, so that other code that may also be wrapping resolvers, can use it.

EmrysMyrddin commented 1 month ago

Thank you for your very detailed report!

I'm not sure to understand how we can end up with wrapped resolvers inside the schema on the second onSchemaChange call ? If the schema change, its resolvers should not be wrapped isn't it ?