Ignition 98
Ignition 9810mo ago

TRPC to connect a client app with a backend apigateway ?

Hi everyone! Is it a good practice to use TRPC to connect my client application (Next.Js) with my backend RESTful APIGateway app (Nest.Js) that plays a role of an entry point to other micro-services. Are there best practices for such use cases ? Would appreciate some examples of recommendations! Thanks!
2 Replies
Nick
Nick10mo ago
Trpc is at its best as a replacement to your nestjs API, but it’s also very common to use it as a BFF and sit between the frontend and an existing central backend
Ignition 98
Ignition 9810mo ago
@Nick Lucas Thanks! Do you have an example how TRPC can call a service on a backend ? Would create a caller-function be the right choice for this purpose?
// 1. create a caller-function for your router
const createCaller = createCallerFactory(appRouter);

// 2. create a caller using your `Context`
const caller = createCaller({
foo: 'bar',
});

// 3. use the caller to add and list posts
const addedPost = await caller.post.add({
title: 'How to make server-side call in tRPC',
});

const postList = await caller.post.list();
// 1. create a caller-function for your router
const createCaller = createCallerFactory(appRouter);

// 2. create a caller using your `Context`
const caller = createCaller({
foo: 'bar',
});

// 3. use the caller to add and list posts
const addedPost = await caller.post.add({
title: 'How to make server-side call in tRPC',
});

const postList = await caller.post.list();