Skip to content

Badrequest using superjson transformer #752

Closed

Description

@donchev7

Hi,

I have a very simple app that I am testing:

import * as trpc from '@trpc/server'
import superjson from 'superjson'
import { z } from 'zod'

const appRouter = trpc
  .router()
  .transformer(superjson)
  .mutation('hello', {
    input: z.string(),
    resolve({ input }) {
      console.log(input)

      return { input }
    }
  })

export type AppRouter = typeof appRouter

// http server
const { listen } = trpc.createHttpServer({
  router: appRouter,
  createContext: () => ({})
})

listen(5050)

when I call this endpoint with:

=> http POST 'localhost:5050/hello' input=asdasd                                                                                                                                    
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 1735
Content-Type: application/json
Date: Wed, 04 Aug 2021 07:41:14 GMT
Keep-Alive: timeout=5

{
    "error": {
        "json": {
            "code": -32600,
            "data": {
                "code": "BAD_REQUEST",
                "path": "hello",
                "stack": "TRPCError: [\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"string\",\n    \"received\": \"undefined\",\n    \"path\": [],\n    \"message\": \"Required\"\n  }\n]\n    at ProcedureWithInput.parseInput (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:46:13)\n    at /home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:62:21\n    at Array.<anonymous> (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:66:20)\n    at ProcedureWithInput.call (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:71:36)\n    at Router.call (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:295:22)\n    at Object.mutation (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:309:21)\n    at Object.callProcedure (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/callProcedure-dfe3505e.cjs.dev.js:57:19)\n    at /home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/index-5eec7158.cjs.dev.js:239:44\n    at Array.map (<anonymous>)\n    at Object.requestHandler (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/index-5eec7158.cjs.dev.js:235:45)"
            },
            "message": "[\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"string\",\n    \"received\": \"undefined\",\n    \"path\": [],\n    \"message\": \"Required\"\n  }\n]"
        }
    },
    "id": null
}

If I remove the transfomer(superjson) from the server it works as expected:

=> http POST 'localhost:5050/hello' input=asdasd                                                                                                                                    
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 62
Content-Type: application/json
Date: Wed, 04 Aug 2021 07:42:23 GMT
Keep-Alive: timeout=5

{
    "id": null,
    "result": {
        "data": {
            "input": "asdasd"
        },
        "type": "data"
    }
}

Activity

  1. KATT commented on Aug 4, 2021

    @KATT
    Member

    It's because it expects a superjson-serialized object - try disabling superjson on your backend or send ?input=%7B%22json%22%3A%22asdasd%22%7D

  2. KATT commented on Aug 4, 2021

    @KATT
    Member

    I got that input payload from doing copy(encodeURIComponent(JSON.stringify({json: "asdasd"}))) in my inspector (the fmt that superjson expects)

  3. KATT commented on Aug 4, 2021

    @KATT
    Member

    Without superjson you would need to send input=%22hello%22 (copy(encodeURIComponent(JSON.stringify("hello"))))

  4. donchev7 commented on Aug 4, 2021

    @donchev7
    Author

    Without superjson I am able to do

    http POST 'localhost:5050/hello' input=world
    

    I am using HTTPie. And note that I am calling a mutation endpoint with POST and not a query with queryParameters

  5. donchev7 commented on Aug 4, 2021

    @donchev7
    Author

    But nevermind, superjson is the problem here you are right. Even on a passthorugh:

    const appRouter = trpc
      .router()
      .transformer(superjson)
      .mutation('hello', {
        input: (v) => v,
        resolve(input) {
          console.log(input)
    
          return { input }
        }
      })
    

    I can see:

    {
    type: 'mutation',
    ctx: {},
    path: 'hello',
    rawInput: undefined,
    input: undefined
    }
    
  6. github-actions commented on Oct 5, 2022

    @github-actions
    Contributor

    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.

  7. locked as resolved and limited conversation to collaborators on Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions