Skip to content

RFC: Easier calling of tRPC queries with Postman etc #2480

Closed as not planned
Closed as not planned

Description

@KATT

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

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!

Activity

  1. changed the title [-]feat: Easier usage with things like Postman etc[/-] [+]feat: Prefix our query params in preparation of alternative ways of calling[/+] on Aug 18, 2022
  2. moved this to ‼️ Ahead of public RC in Roadmap and prioritieson Aug 18, 2022
  3. added
    RFC
    on Aug 18, 2022
  4. changed the title [-]feat: Prefix our query params in preparation of alternative ways of calling[/-] [+]RFC: Prefix our query params in preparation of alternative ways of calling[/+] on Aug 18, 2022
  5. changed the title [-]RFC: Prefix our query params in preparation of alternative ways of calling[/-] [+]RFC: Easier calling of tRPC queries with Postman etc[/+] on Aug 18, 2022
  6. jlalmes commented on Aug 18, 2022

    @jlalmes
    Contributor

    This is completely out of tRPC scope. I am wholly against the @trpc/server & @trpc/client supporting and maintaining this functionality.

    The correct solution would be Postman (or similar API testing platform) implementing a new integration/plugin for tRPC, just like they already have with gRPC(https://blog.postman.com/postman-now-supports-grpc/).

    Alternative solution for exploratory tRPC API testing is @sachinraja's trpc-playground.

    If someone is still keen to call their tRPC endpoints via Postman and unwilling to deal with the JSONRPC format then just encourage them to add trpc-openapi.

  7. sachinraja commented on Aug 18, 2022

    @sachinraja
    Member

    I agree with @jlalmes. This is not something that should be solved in core. trpc-openapi already handles this very well although you do have to specify the output schema. We could just adapt the solution there to only generate a REST API and only require output schemas / openapi meta when generating openapi documents.

  8. KATT commented on Aug 18, 2022

    @KATT
    MemberAuthor

    I'm happy with these answers. Thank you so much for the input, I will close this!

    Also, method overloading will make this easier too.

    What's your feels about the part of adding transformer as a query param?

  9. jlalmes commented on Aug 18, 2022

    @jlalmes
    Contributor

    What's your feels about the part of adding transformer as a query param?

    Meh, I'd leave it as-is. Ultimately I'd imagine transformer will be deprecated anyway after we implement Content-Types.

  10. felix-wooliesx commented on Aug 20, 2022

    @felix-wooliesx

    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?

  11. Repository owner moved this from ‼️ Ahead of public RC to ✅ Done in Roadmap and prioritieson Aug 20, 2022
  12. KATT commented on Aug 20, 2022

    @KATT
    MemberAuthor

    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?

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

  13. felix-wooliesx commented on Aug 23, 2022

    @felix-wooliesx

    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 🙂

  14. github-actions commented on Oct 4, 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.

  15. locked as resolved and limited conversation to collaborators on Oct 4, 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

    RFC

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions