context question
Why are the context examples only showing opinionated examples with next/react auth ? Also i find it very confusing that it is not mentioned in the docs what kind of arguments the context function is called with.
28 Replies
Feel free to write up your thoughts here: https://github.com/trpc/trpc/discussions/3963
GitHub
RFC: 2023 Upgraded Documentation · trpc trpc · Discussion #3963
Preface Currently the docs have a lot of really well written content, and some which needs a little love. The biggest problem is they're often written from the perspective of APIs, rather than ...
We're actively working on improvements so this is very appreciated 🙂
That is great and all, but do you think you can help me understand what is the proper way to type a context function?
What should be the type of an object that is able to be passed to a procedure's
use
method ?t.middleware()
So wait, i'm a bit confused
What exactly is the difference between context and middleware?
Context is a place to put data and functionality that procedures need to use
Middleware is a place to put behaviour which gets called before a procedure is called, and can also modify the Context
So if i'm understanding this correctly,
context
-> middleware
-> procedure
It might be helpful to look through the example projects as they have implementations of this
Context is available to both middlewares and procedures
Middleware can be specific to a procedure, but a context is router wide, is that correct?
So createContext will be API-wide, yes
Middlewares are attached to a procedure yes
Middlewares which modify context only modify the context for procedures on that base procedure
I see.
Last question is
How do i attach a context to a router?
Or do i need to attach it to the
createHttpServer
instead?
Also another valid question that is derivative of my previous one would be: Is it possible to attach contexts to routers that are later on going to be merged together, which would result in some of the api routes not having context ?Sidenote:
This image seems to suggest that contexts are api wide, regardless if i want to merge in routers that have no use for these contexts with others that actually do
That’s where the logic should be passed, yes. Typescript will tell you what the args and result are
Is there a type for the
createContext
object, so i can define it elsewhere in my code and not inline of the createHttpServer
?All routers come from t.router which already has the Context type associated. createContext then needs writing once
Hover over it in your editor and you’ll see the type
Ughm... i think i'm not explaining what i want clearly enough.
the
TypeHere
is what i am looking for.You can either dig into that type and understand what tRPC is doing, or you can write out the bits you actually need into a factory
createContext: (req, res) => makeMyContext(req.etc)
Isn't there an already exported type for the
createContext
option of the createHTTPServer
?Feel free to open an issue about it, it's not a problem I've ever needed to solve
The docs I think do some of this, but not using a dedicated type
But i think you get where i'm coming from, right?
Context | tRPC
Your context holds data that all of your tRPC procedures will have access to, and is a great place to put things like database connections or authentication information.
I know you've seen this, but here the docs do have a separated function
I appreciate it's for Next though, we should change that
Well
The example code section did not show how the
createContext
was attached to the TRPC instance
And like i mentioned at the start, it was very confusing to see that the createContext
function wasn't used anywhere but in the exported Context
infer type, and the fact that the opts
parameter was defined as CreateNextContextOptions
Anyway, i think i got the info i needed.
Regardless of the little pains here and there, i would say that i'm just not used to the library yet.
I really do like tRPC and i feel like this is what will power the future of API interfaces.
+1 star
Thanks for the kind help ❤️Please do share your thoughts on the RFC, we need this kind of feedback and I agree as I got tripped up in this area too 🙂
By the way, i found this to be a solution to my problem with the type inference of the context function