0

This is my first attempt to use tRPC. I created a mutation named "add", which receive a URL as parameter and returns a hardcoded slug

Router

export const entryRouter = router({
  add: publicProcedure
    .input(input)
    .output(output)
    .mutation(async ({ ctx, input }) => {
      const slug = "test"

      return { slug }
    }),
})

Usage

const addEntry = trpc.entry.add.useMutation()

...

const { slug } = await addEntry.mutateAsync({ url: 'https://example.com' })

console.log(JSON.stringify({ slug })

However, it only prints an empty object ({})

What am I doing wrong?

Full source-code https://github.com/skhaz/url-shortener

PS. I am using with NextJS, on the inspector, on the network, I can see the slug value on the response JSON

Rodrigo
  • 135
  • 4
  • 45
  • 107

2 Answers2

0

The problem is that I was using superjson as the transformer. Removing it solved the issue

Rodrigo
  • 135
  • 4
  • 45
  • 107
  • Did you figure out exactly what superjson was messing up? I've been using trpc with superjson on pretty big apps and I've never had any issue with it, i'm curious. – Sheraff Dec 05 '22 at 11:43
  • I have no idea. The only diff is when using superjson, the payload gets wrapped into a json field (the name of the field is json) – Rodrigo Dec 05 '22 at 15:07
0

This happened to me also in T3Stack(https://github.com/t3-oss/create-t3-turbo) after upgrading superjson, @trpc/client and @trpc/server and packages:

"@trpc/client": "^10.9.0 -> ^ 10.13.2"
"@trpc/server": "^10.9.0 -> ^10.13.2",
"superjson": "1.9.1 -> ^1.12.1",

But it did not mess only with mutateAsync but with whole trpc types as every function that trpc route object provides was inferred as any.

After bumping it back to lower version all started working as expected.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • I opened the issue here so you can follow up if they come up with a better solution than downgrading versions or removing superjson. https://github.com/t3-oss/create-t3-turbo/issues/191 – Aleksa Živanović Mar 02 '23 at 11:35