trpc / v10-playground

tRPC v10 procedure play
https://stackblitz.com/github/trpc/v10-playground?file=src%2Fserver%2Findex.ts,src%2Fclient.ts,src%2Fserver%2Frouters%2FpostRouter.ts&view=editor
13 stars 3 forks source link

Awkward reference to dot-separated strings #6

Closed VinSpee closed 2 years ago

VinSpee commented 2 years ago

https://github.com/trpc/v10-playground/blob/cfee1c2c2b03baa131d3bfcb50addc10ffe53d81/src/client.ts#L13

In my opinion, this syntax is unnatural in JS/TS. It’s acceptable when the value is just a string, but in this case, it’s a dot-separated string which is the property of an object. It feels quirky and IMO really exposes this as a DSL, a “magic string”.

Since we’re considering an alternative syntax anyway, is there a stance against making procedures referenceable as objects? Example:

const posts = await client.query(queries.post.all);

thanks!

KATT commented 2 years ago

I'd ideally like this too. However that would require the underlying object to be in this format:

const procedures = {
  queries: {
    post: {
      all: () => {}
    }
}

While this is nice, it might be hard to achieve this recursively and lead to the same sort of complexity costs that we have now. Not sure if it's possible even. Might have a stab at it.

KATT commented 2 years ago

Had a quick stab at 693b067

Screenshot 2022-01-20 at 00 11 48

Got any ideas?

KATT commented 2 years ago

I guess it could also be done in that proxy object.... sort of parsing the strings and turning them into a new object based on the dot-notation, that should be possible.

KATT commented 2 years ago

Can also be solved by not using dot notation, you can use _ as a separator or CamelCase your procedure names.

VinSpee commented 2 years ago

True, but there’s still the “magic” part On Jan 20, 2022, 00:56 -0500, Alex Johansson @.***>, wrote:

Can also be solved by not using dot notation, you can use _ as a separator or CamelCase your procedure names. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

KATT commented 2 years ago

Seems doable.

Have a look at this: https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgNgQ2FAvFA3gKCjqAzAewIC4oA7AVwFsAjCAJwG5tcByGhegOjIgGdgEACYBGVqQH0AlmQDmzXFHace-QUIBM48tTr0A2gF0FbDt14DhAZi5Cy2yrQZGTOZebXXbZLRODS5F0wAX0xMUEgoAFUyPERgQTIAHgAVAD5UdGCoADIcLEV9AAV6Akh6UCgZKABrCBACPCgUqAQ+KABRAA8AYzgKIQgkkrKGUAAaKAADABJ0SRlZYK45hblgqbTDUhTi0vLQYxDcjBYcPdGKkCqyWvrG5tb2os4IMmAAeTxh-bGQLdIUAAkkIkjE4khEkkCoooPoAMIACykcCEIwO12qSJRQi+P0uoABOF2s3QL3ob0+33RfzSyzm2NRNKuUA2JzqDSaKUMZ1ZaTSITCEWg5MpePSqEeEC6iSE7VJMjwDCgove9Pm-kWbIA-CrXu8oKReAA3BjMYVQRm474StAtaWy+WrTXrFboRXKq06y3I1GG8gQU1MIXgaAg1IZNBZE4w4o3O6c5rbZrFHmhcKhqAATUl4PiUPgSDSQA

Then we'd need a recursive proxy object that would return the concatenated string as a result. Wanna have a stab at it?

KATT commented 2 years ago

I do think that the current DX is superior once you get used to it though:

KATT commented 2 years ago

The new recommended convention will be to camelCase or snake_case rather than dot notation.