-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tRPC mutations hang with Koa adapter (solved; due to bodyParser middleware conflict) #24
Comments
Hi @aseemk. Thank you for this detailed writeup, very helpful. I see the issue as you describe. I am doing a little more research but will definitely incorporate your feedback here to make it easier for others. Those requiring the old koa-bodyparser may be able to utilize use its const prefix = "/trpc";
const app = new Koa();
app.use(async (ctx, next) => {
if (ctx.path.startsWith(prefix)) ctx.disableBodyParser = true;
await next();
});
app.use(bodyParser());
app.use(
createKoaMiddleware({
router: appRouter,
prefix,
})
); FWIW this should work with @koa/bodyparser as well. If it happens that you can add the bodyparser middleware after this Also wondering if there is anything more we can do in this library to handle this (or fail more gracefully) such as a looking at the raw body, but not sure if that ultimately makes sense. EDIT: Actually, I think the |
- fixes body parsing issue raised here: #24 - data stream gets consumed by body parser - changed middleware to look for parsed body and make available where trpc expects it
I made a fix that solves it across the board (confirmed with I added a check for the parsed body on EDIT: This definitely seems like the way to go. I played around with the trpc express adapter and tried to produce the same error. Parsing the body with |
* chore: update packageManager declaration - lockfile produced with more recent pnpm and version in package.json was fairly old * docs: update readme with koa bodyparser usage example * chore: fix typos, change example order, other minor edits * chore: more minor readme edits * fix: mutation hang when data stream consumed by parser - fixes body parsing issue raised here: #24 - data stream gets consumed by body parser - changed middleware to look for parsed body and make available where trpc expects it * test: refactor and add bodyparser integration cases * chore: rm unecessary ts options - recently added but ultimately determined they arent required. one of them causes the build to fail * docs: mention/link gh issue in body parser note
Hi there. Thanks for this library! I hit an issue using it, where queries worked, but mutations would hang indefinitely, and never actually execute my procedure/function.
I managed to figure this issue out, but I thought I'd document it here for anyone else that runs into it (since I spent more than an hour debugging it).
The cause was that:
Mutation inputs come through the request body (whereas query string inputs come through the query string)
tRPC's Node adapter (which this library wraps) thus attempts to read/stream the request body
I was already using Koa's
@koa/bodyparser
middleware for other uses in my app. And that middleware already reads/streams the request body itself, so tRPC was indefinitely waiting around fordata
andend
events that never came.Fortunately:
tRPC's Node adapter looks for a pre-populated
body
property on the native Node request if one is set.@koa/bodyparser
provides apatchNode
option to add this property to the native Node request (in addition to the Koa context's request).So the solution is: if you're using
@koa/bodyparser
, setpatchNode: true
for tRPC to work with Koa!One last note: we were actually using
koa-bodyparser
— a subtly different package name that stopped getting updates after v4 — while thepatchNode
option was only added in v5! So fixing this also required changing & upgrading fromkoa-bodyparser
to@koa/bodyparser
.Consider adding a note about this to the readme. Even though this isn't directly related to this Koa tRPC adapter, it might not be uncommon for Koa users to be using the
bodyparser
middleware too.Thanks and hope this helps others!
The text was updated successfully, but these errors were encountered: