Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Make the RPC call as property access on the Proxy #21

Merged
merged 18 commits into from
Feb 1, 2022
Merged

Make the RPC call as property access on the Proxy #21

merged 18 commits into from
Feb 1, 2022

Conversation

KATT
Copy link
Member

@KATT KATT commented Jan 28, 2022

Fork of #17

Allows you to call your API like this:

import type { appRouter } from './server';
import { createClient } from '@trpc/client';

const client = createClient<typeof appRouter>();

async function main() {
  // you can CMD+click `postById` here and jump straight into your backend
  const byId = await client.query.postById({ id: '1' });

  if (byId.ok) {
    console.log('data', byId.data);
  } else {
    console.log(byId.error.code);
  }
}

main();

Upsides

  • Client code gets super intuitive and clean

Downsides

  • We're actually lying to the compiler and server code will also need a similar proxy to call or some other mapping in order to propagate context
  • Requires Proxy
  • Requires all procedures to use a trpc.resolver(() => /* ... */)

@KATT KATT added the ❕ RFC Request for comments - please comment! label Jan 28, 2022
@esamattis
Copy link
Collaborator

Really cool!

We're actually lying to the compiler

Does it matter if it is just the internals? Should not affect the users in any way? 🤔

@KATT
Copy link
Member Author

KATT commented Jan 28, 2022

Does it matter if it is just the internals? Should not affect the users in any way? 🤔

In some cases it might, if they miss using the proxy object for instance or are doing backend-stuff and trying to explicitly call the functions

@KATT
Copy link
Member Author

KATT commented Jan 28, 2022

Furthermore... Wondering how the React-api could look like. I've noticed that when I try to remap functions, I seem to lose CMD+click ability...

A few options

// this is a bit ugly but will work to do CMD+click on `greeting` with to jump to the backend
const { queries } = myQueries
trpc.useQuery(queries.greeting({ name: 'world' }), { /* .. further options */})

// these below won't work to CMD+click on :(
trpc.useQuery.greeting({ name: 'world' }, { /* .. further options */})
trpc.useGreetingQuery({ name: 'world' }, { /* .. further options */})

@KATT
Copy link
Member Author

KATT commented Jan 28, 2022

@colinhacks maybe you have ideas/input on the above?

@esamattis
Copy link
Collaborator

@KATT

In some cases it might, if they miss using the proxy object for instance or are doing backend-stuff and trying to explicitly call the functions

Hmm, direct proxy usage might a bit too magical. Explicit query argument does indeed communicate better that there's something more than just a function call going on.

And if it does not work out for the React hook, there some value having them be consistent:

client.query(query.greeting, { name: "world" });
useQuery(query.greeting, { name: "world" });

@colinhacks colinhacks mentioned this pull request Jan 29, 2022
@KATT KATT marked this pull request as ready for review February 1, 2022 20:42
Base automatically changed from katt/response-error-marker to main February 1, 2022 20:43
# Conflicts:
#	.big/client.ts
#	src/server.test.ts
#	src/server.ts
#	src/trpc/server/middlewares/zod.ts
#	src/trpc/server/procedure.ts
#	src/trpc/server/router.ts
#	src/trpc/server/types.ts
@KATT KATT changed the title [RFC] Make the RPC call as property access on the Proxy Make the RPC call as property access on the Proxy Feb 1, 2022
@KATT
Copy link
Member Author

KATT commented Feb 1, 2022

Merging for now - can be discussed in #26

@KATT KATT merged commit 9c53240 into main Feb 1, 2022
@KATT KATT deleted the katt/proxy branch February 1, 2022 20:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
❕ RFC Request for comments - please comment!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make the RPC call as property access on the Proxy?
2 participants