how to handle non array api responses #324
-
Hey guy , am really loving this liband the flexibility it's unlocking I have a question on when the api returns an object with pageOptions like page and totalPage and items as the array of data export const communityWatchlistCollection = ({
keyword,
page = 1,
}: CommunityWatchlistCollectionProps) => {
return createCollection(
queryCollectionOptions({
queryKey: ["watchlist", "community", keyword, page],
queryFn: async () => {
const response = await pb.from("watchlist").getList(page, 50, {
filter: and(
or(like("title", `%${keyword ?? ""}%`),
like("overview", `%${keyword ?? ""}%`),
like("user_id.username", `%${keyword ?? ""}%`)
),
eq("visibility", "public")
),
sort: "-created",
select: {
expand: {
user_id: true,
items: true,
},
},
});
return response.items;
},
queryClient: queryClient,
getKey: (item) => item.id,
schema: WatchlistResponseSchema,
})
);
}; i was wondering is there was a way to let the queryoption cache the whole object and pass a partial (data.items) into the collectionQurryotions and be able to create a local query collection with the page options since i don't care about optimistically updating them , hey're just metadta to know what pagination controls to render the regular query options have a select field for shaping data while keeping the whole object cached wish we hav=de something similar here export const communityWatchlistqueryoptions = ({
keyword,
page = 1,
}: CommunityWatchlistCollectionProps) => {
return queryOptions({
queryKey: ["watchlist", "community", keyword, page],
queryFn: async () => {
const response = await pb.from("watchlist").getList(page, 50, {
filter: and(
or(
like("title", `%${keyword ?? ""}%`),
like("overview", `%${keyword ?? ""}%`),
like("user_id.username", `%${keyword ?? ""}%`)
),
eq("visibility", "public")
),
sort: "-created",
select: {
expand: {
user_id: true,
items: true,
},
},
});
return response
},
select(data) {
return data.items;
},
}); kindly advice |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
👋 Thanks for the questions! On returning arrays — this is necessary as DB has tighter data requirements than Query. A query cache can have anything while a collection is an array of typed objects. On storing other metadata. Perhaps just manually write it to the query cache? We could perhaps support the |
Beta Was this translation helpful? Give feedback.
👋 Thanks for the questions!
On returning arrays — this is necessary as DB has tighter data requirements than Query. A query cache can have anything while a collection is an array of typed objects.
On storing other metadata. Perhaps just manually write it to the query cache? We could perhaps support the
select
API as well — how does Query normally store the whole response? It's on a different key than the selected response? I'm sure we could do the same thing then.