Closed delaneyj closed 3 years ago
Never actually worked with fastify so would need to dig in a bit more to understand how their requests and responses work. Seems like you return rather than .send()
responses
Do you have an existing fastify-project that you want to add tRPC on or is it a greenfield project and you're used and like fastify for the non-tRPC things? Because you can do a HTTP standalone server pretty easily.
@delaneyj - see #528
Wow you are fast @KATT . I was afraid because it uses send + reply has a different interface than express middleware it might be a headache to do.
Reasons for Fastify
The standalone http server is great but middlewares, docgen, health/status, jwt/paseto, sessions, etc are well vetted. This is for a greenfield that was starting to use giraphql, which is amazing, but there is enough dynamic route generation stuff happening that I think this could be a better fit.
So with your example the plugin is dead simple.
import {
AnyRouter,
CreateHttpHandlerOptions,
requestHandler,
} from "@trpc/server";
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
export interface CreateFastifyHandlerOptions<TRouter extends AnyRouter>
extends CreateHttpHandlerOptions<TRouter> {
prefix?: string;
}
export default fp(
async function <TRouter extends AnyRouter>(
app: FastifyInstance,
opts: CreateFastifyHandlerOptions<TRouter>
) {
app.decorate("trpc", opts);
const { prefix, router, createContext } = opts;
const bind = `/${prefix || "trpc"}/:path`;
app.all(bind, async (req, reply) => {
await requestHandler({
req: req.raw,
res: reply.raw,
router,
createContext,
path: (req.params as any).path,
});
});
},
{ fastify: "3.x", name: "fastify-trpc" }
);
use with
const app = fastify();
app.register(fastTRPC, {
prefix: "foo",
router: appRouter,
createContext() {
return {};
},
});
app.listen(1234);
Given that plugins are a normal way to package this kind of middleware would you like me to make a PR? I could use a bit more typechecking but is on the same terms as the other server options for now.
Fastify won't make tRPC faster, but if you have api routes that you don't want to have tRPC on it's alright to do it this way.
You can make an adapter similar to the express adapter, but this is fine as well.
Great that it worked out - will close this for now.
This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.
Did you have any plans for Fastify integration? Feels like its a perfect fix as its decently low level and all TS.
I started writing a plugin but getting a little stuck in the types and the middie/express examples ported over (fastify has bridge support for express middleware ) don't seem to be working.
fastTRPC
called using
This doesn't give any type errors but fails weirdly enough it seems to fail in the client. Never get to a breakpoint at first line in
app.all()
However the raw http example this is based on works fine.I don't know enough about the internals to really grok what's happening. would be nice if the npm version also exported the sourcemaps or ts code to step through.