Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose inputParser on Procedure? #703

Closed
mmkal opened this issue Jul 22, 2021 · 10 comments
Closed

Expose inputParser on Procedure? #703

mmkal opened this issue Jul 22, 2021 · 10 comments

Comments

@mmkal
Copy link
Collaborator

mmkal commented Jul 22, 2021

Hi - have just started using trpc and it is 👌

One request: what do you think about exposing the (zod) schema on the Procedure object? The use-case being, I would like to create a UI in an internal admin app using react-json-schema-form for various whitelisted "admin" queries/mutations. If we could access appRouter._def.queries.myQuery.inputParser we could pass it to zod-to-json-schema which would give us some schemas we could use with react-json-schema-form.

It's possible to do now by suppressing the typescript error about using a private field, but I wanted to see if you'd be open to making it public so that it wouldn't disappear and break our workflow in a minor update.

@KATT
Copy link
Member

KATT commented Jul 22, 2021

On phone, will give example tomorrow, but

  1. Define schema in React component
  2. Import said schema into your the trpc router

@mmkal
Copy link
Collaborator Author

mmkal commented Jul 23, 2021

Interested to see the example, but I think I may not have explained the use case properly. I want to take an appRouter defined on the server side, with @trpc/server and zod inputs, and use it to get json-schema POJOs. Those POJOs can be used as the model for a json-schema based form in retool which we already use to build admin UIs. Retool happens to support json-schema forms - we just need to serve the schema POJO(s) for its model. We have done this in the past with AWS lambdas which validate their inputs with zod too.

So given that (desired) workflow I'm not your suggestion applies - the schema has already been defined on the server side, and there isn't actually a react app to move the schema definition to even if we wanted to.

As mentioned this is possible already by just accessing the private variable, which works at runtime, and the compiler error can be suppressed. Likely the pull request to fix this would just be removing private from the inputParser.

@KATT
Copy link
Member

KATT commented Jul 23, 2021

Ohhh, using trpc with Retool is exciting, I'd love for that to work well.

I'll have a peek later today and see if we can find a solution.

KATT added a commit that referenced this issue Jul 23, 2021
@KATT
Copy link
Member

KATT commented Jul 23, 2021

This is using the private property Procedure::inputParser so it's a bit of an unstable hack but it works, see b3427be - accessible at http://localhost:3000/api/trpc/posts.edit.input

Screenshot 2021-07-23 at 12 55 33

@KATT
Copy link
Member

KATT commented Jul 23, 2021

You can use the above, just be aware it may stop working in the future without a major version change.

If you can make a proof of concept and document it in the official docs I would happily add better support.

@KATT
Copy link
Member

KATT commented Jul 23, 2021

@all-contributors add @mmkal for ideas

@allcontributors
Copy link
Contributor

@KATT

I've put up a pull request to add @mmkal! 🎉

KATT added a commit that referenced this issue Jul 23, 2021
This reverts commit b3427be.
KATT added a commit that referenced this issue Jul 23, 2021
@KATT
Copy link
Member

KATT commented Jul 23, 2021

did an example that does it dynamically in 43f328c
Screenshot 2021-07-23 at 13 31 21

@mmkal
Copy link
Collaborator Author

mmkal commented Jul 23, 2021

Amazing, thank you! Yes that example is exactly along the lines I was thinking. I will try to open a docs PR and make it non-private soon if I can get some spare cycles.

KATT added a commit that referenced this issue Jul 23, 2021
This reverts commit 43f328c.
@KATT
Copy link
Member

KATT commented Jul 23, 2021

@mmkal - I'll leave the PR + docs to you then. Would be really cool get an example of this up and running on retool, such a timesaver.

@trpc trpc locked and limited conversation to collaborators Jul 24, 2021
@KATT KATT closed this as completed Jul 24, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants