Replies: 1 comment 1 reply
-
@joa23 you can use a middleware to add the DB into the request context, something like: api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
ctx = huma.WithValue(ctx, "db", myDB)
next(ctx)
}) Then in your resolver: func (t *MyType) Resolve(ctx huma.Context) []error {
db := ctx.Context().Value("db").(MyDBType)
db.DoSomething(...)
} Another potential way is to use a global pointer to the DB, then resolvers can just use that. Anyway keep in mind that this can slow down validation as it now depends on responses from the DB, but otherwise should be fine! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
What is the best approach to validate input when e.g. a DB connection is required.
Is there a way to use the resolvers and somehow pass the DB connection into context?
Or, is there another slick way to do that? I like to pin errors to locations on json objects for nice UI errors. :)
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions