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:
user {
id
username
avatar
}

user {
id
username
avatar
email
updatedAt
}
user {
id
username
avatar
}

user {
id
username
avatar
email
updatedAt
}
how would you design that in tRPC? do you have to implement an interface like this?
getUser({
select: ["id", "avatar", "username"],
orderBy: "ASC"
})
getUser({
select: ["id", "avatar", "username"],
orderBy: "ASC"
})
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
Brendonovich
Brendonovich2y ago
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
ippo
ippoOP2y ago
my question is more how to use tRPC correctly when using arguments to control the query result, like selecting fields or relations
Brendonovich
Brendonovich2y ago
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
ippo
ippoOP2y ago
I do not see that because you can express optional values in TS
Brendonovich
Brendonovich2y ago
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
ippo
ippoOP2y ago
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
Brendonovich
Brendonovich2y ago
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 it
ippo
ippoOP2y ago
this 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?
Brendonovich
Brendonovich2y ago
I don't quite get what you mean but that approach sounds fine
ippo
ippoOP2y ago
in your frontend, while using tRPC, you never passed an argument to select specific fields 🙂 ?
Brendonovich
Brendonovich2y ago
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
ippo
ippoOP2y ago
hmm... 🤔 so for each view you create a custom endpoint? (even if you can control the return values with arguments)
Brendonovich
Brendonovich2y ago
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
Want results from more Discord servers?
Add your server