ippo
ippo12mo ago

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 do 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
3 Replies
Nick
Nick12mo ago
Trpc doesn’t offer anything like this directly, but as you’ve proposed it’s possible to do with plain typescript. The problem of over fetching fields is generally not a problem for most apps though
ippo
ippo12mo ago
@Nick Lucas so is my idea okay and common in tRPC?
Nick
Nick12mo ago
Not really common, no, though I have thought it could be an interesting gap for a library to fill You'll run into issues with varying output types based on input types. TS functions can do that, but tRPC can't since the input/output are isolated from each other