TRPC max retries on endpoint

export const authRouter = router({
getSession: publicProcedure.query(({ ctx }) => {
// if (!ctx.session || !ctx.session.user?.accessToken) {
if (true) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Not logged in",
cause: "No session",
});
}
return ctx.session;
}),
export const authRouter = router({
getSession: publicProcedure.query(({ ctx }) => {
// if (!ctx.session || !ctx.session.user?.accessToken) {
if (true) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Not logged in",
cause: "No session",
});
}
return ctx.session;
}),
const session = trpc.auth.getSession.useQuery(undefined, {
onError: (err) => {
console.log(1)
return signIn();
},
});
const session = trpc.auth.getSession.useQuery(undefined, {
onError: (err) => {
console.log(1)
return signIn();
},
});
Hi, for some reason, when the api endpoint errors it reruns the request 3 times, and then on the last time if it errors again it will then emit the onError event. Is there a setting which I am unaware of which trys to rerun the requests x amount of times? This results in the page loading for a number of seconds before the user is forced to login with the nextauth signIn function. Checking the console, the error is thrown every time. (In the picture, this is the result of one unauthorized page load) I've had a look at the docs here: https://trpc.io/docs/v9/error-handling and didn't see anything about a auto retry or max retries function/property?
10 Replies
Neto
Neto•2y ago
your questions isnt trpc itself is on react query land
Neto
Neto•2y ago
Query Retries | TanStack Query Docs
When a useQuery query fails (the query function throws an error), React Query will automatically retry the query if that query's request has not reached the max number of consecutive retries (defaults to 3) or a function is provided to determine if a retry is allowed. You can configure retries both on a global level and an individual query level.
rustclan
rustclan•2y ago
oh apologies. I was not aware trpc used react query
Neto
Neto•2y ago
trpc itself is independent of rq t3 just setup rq and the adapter for convenience (and nice usage)
rustclan
rustclan•2y ago
oh right okay. Thank you 🙂 I'm looking for where the rq/adapter is made and I cannot find where I can configure the queryclient. i ASSUME IT IS IN trpc.ts
const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape;
},
});
const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape;
},
});
after looking at the trpc docs, it appears this is where it is intialized. But cannot find any information about adding custom reactquery options.
Neto
Neto•2y ago
GitHub
t3-auth0/trpc.ts at main · STNeto1/t3-auth0
Contribute to STNeto1/t3-auth0 development by creating an account on GitHub.
Neto
Neto•2y ago
here?
rustclan
rustclan•2y ago
Thank you so much!
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
transformer: superjson,
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
],
queryClientConfig: {
defaultOptions: {
queries: {
retry: false,
},
},
},
};
},
ssr: false,
});
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
transformer: superjson,
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
],
queryClientConfig: {
defaultOptions: {
queries: {
retry: false,
},
},
},
};
},
ssr: false,
});
I had to insert queryClientConfig inside of this initaliser 🙂
Neto
Neto•2y ago
if you want to change the global behaviour you can define at single query behaviour
rustclan
rustclan•2y ago
yep. I want to change global. Useful to know that i can change specific query tho. thanks!
Want results from more Discord servers?
Add your server