question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Easier calling of tRPC queries with Postman etc

See original GitHub issue

Summary

This is a two-part thing:

  1. Prefix “tRPC query params” with a _
  2. Enable users to easier query the endpoints with Postman, etc.

Describe the solution you’d like to see

Part 1 - Prefix queries

Prefix queries

  • We should likely prefix batch, input, etc with a _ (or maybe _trpc_)
  • We should also make the use of a transformer being stated in the query params, like _transform=1 so we can know on the backend if something was called with - already on the roadmap

Part 2 - allow querying without JSON.stringifying query input

Given a postById procedure with this input:

z.object({
  id: z.string(),
})

It would be nice to be able to call the endpoint in both these ways:

  • /api/trpc/postById?_input=%7B%22id%22%3A%221%22%7D (encodeURIComponent(JSON.stringify({ id: "1" }))})
  • /api/trpc/postById?id=1

Caveats / drawbacks

  • How to deal with id was a z.number()?
    • Enforce coercion with a .transform?
  • How do we deal with input not being a z.object()?
  • Maybe a rule could be added on the t’s RootConfig to enforce it to be an object
  • Maybe this should be dynamically opt-in or out so one could only allow it in i.e. development
  • How to deal with more complex inputs? I.e. z.object({ arr: z.array(z.string()) })

Desribe alternate solutions

  • Not doing it or encouraging people to use trpc-openapi
  • #1939 helps here too by allowing users to call query endpoints with a POST

Additional information

Alternative new solutions to tRPC (that have way fewer other features) use this as a selling point w/o recognizing that we have a good reason of doing it this way.

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I’d be down to file a PR implementing this feature!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
felix-wooliesxcommented, Aug 20, 2022

My solution to this problem was to use a Pre-req script in postman to convert a standard JSON body into the encoded and stringified input query string param using the following snippet:

// move the request body into an `input` query param in the format trpc expects

// body.raw is already stringified json
const input = encodeURIComponent(pm.request.body.raw);
pm.request.addQueryParams(`input=${input}`);
// remove the request body
pm.request.update({
    body: undefined
})

This allows me to easily edit the data that’s sent as part of a query as it’s simply just editing JSON in the Body page in postman.

Perhaps a docs page outlining something like the above is all that’s needed in this case?

2reactions
felix-wooliesxcommented, Aug 23, 2022

Hey @felix-wooliesx, this looks awesome, do you wanna add something to the docs?

Yeah sure I can look into that at some point soon 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

useQuery() - tRPC
The hooks provided by @trpc/react-query are a thin wrapper around @tanstack/react-query. For in-depth information about options and usage patterns, ...
Read more >
Connect-Web: TypeScript library for calling RPC servers from ...
Connect-Web: TypeScript library for calling RPC servers from web browsers (buf.build) ... implementations make it easier to interop with the gRPC ecosystem.
Read more >
Implementing a tRPC API with a Solid-Start Application
In this tutorial we hope to show you how to create Solid-Start application using a tRPC API implemented inside the Solid-Start application.
Read more >
Build a full-stack TypeScript app using tRPC and React
Build a simple, full-stack TypeScript app using tRPC that will be type-safe when it comes to the code and across the API boundary....
Read more >
Syntax - Tasty Web Development Treats – Podcast – Podtail
Full Stack Developers Wes Bos and Scott Tolinski dive deep into web development topics, explaining how they work and talking about their own...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found