juancastillo0 / leto

Dart GraphQL server libraries. Utilities, code generator, examples and reference implementation.
https://juancastillo0.github.io/leto/
MIT License
38 stars 6 forks source link

Provide custom request context #17

Closed tigloo closed 6 months ago

tigloo commented 7 months ago

I'm building a GraphQL backend using Leto. Requests are authenticated using bearer tokens. The bearer token contains information about the client, for which I need to fetch context data (such as a user object, RBAC information) from the database.

I would like to add a request middleware that stores this information to the request context so that it only needs to be retrieved once, is stored in memory and every handler in the request pipeline can access it - essentially a Map<String,dynamic> that I could freely populate in Ctx.

Does something like this exist? Or an alternative method to achieve this?

juancastillo0 commented 7 months ago

Hi! Thanks for the issue.

You can see a direct usage in this extension where the ref is defined with a wrapper value (MutableValue, but can be anything) and we set de value like this. The MutableValue will change for every request since the ref where it is saved is a ScopedRef.local.

I believe some of the complete examples do something like this for user or token data.

tigloo commented 6 months ago

This works, thanks!