Query tRPC the right way
If you use graphql, you can precisely query what you exactly need and avoid over fetching, so you can do something like this:
how would you design that in tRPC?
do you have to implement an interface like this?
and what is if you want to query related stuff, like the user and its pets and from the pets you just want the ids but in other cases the names and ids of the pets
13 Replies
You probs wanna just use GraphQL for that stuff, tRPC and the like are more suited to cases where backend-defined return types are acceptable
my question is more how to use tRPC correctly when using arguments to control the query result, like selecting fields or relations
That’s what I mean - tRPC isn’t really built for that
You can’t have the return type of a query change based on the inputs you provide, unless you just want to make everything optional and lose some type safety
I do not see that because you can express optional values in TS
I mean if you're fine with everything being optional then go ahead I guess
I'd just think that's not the best DX
no no, I want to give the user the option to search for data and also select specific things AND to fetch in one view 3 fields and in other views 42 fields of the same entity
I don't see how that's any different - use the
select
array, select the specific fields/relations on the server, return it
How you handle selecting/including fields & relations is up to you - tRPC isn't designed for itthis is really hard to see, because in the end you have function that you call and that accepts inputs from your side
so in my frontend, what speaks against an argument where I define the fields I want from the backend OR the relations that I want from the backend?
I don't quite get what you mean but that approach sounds fine
in your frontend, while using tRPC, you never passed an argument to select specific fields 🙂 ?
Nah, we just hardcode the return types of our queries on the server and would do client-side selection if we really needed to
If selecting fields and relations was really important I wouldn't even consider using tRPC
hmm...
🤔
so for each view you create a custom endpoint?
(even if you can control the return values with arguments)
not necessarily, we just opt to return more data to the frontend than the frontend actually needs
ofc we pass search/filter arguments but not specific field/relation selections