hasanaktasTR
hasanaktasTR6mo ago

useQuery single request

I have a query and I have multiple tables that affect it. What I want is to throw the query once and use it on the client. I can send a request as an http end but what I want is an internal solution. For example const data= useQuery(api....) //web socket const data=useFetchQuery(api....) //http request We can turn the API from query function to mutation function on the server and use it on the client but this time we give up the backend cache
8 Replies
Michal Srb
Michal Srb6mo ago
Convex React | Convex Developer Hub
Convex React is the client library enabling your React application to interact
hasanaktasTR
hasanaktasTROP6mo ago
Thanks. We couldn't find it in the documentation. But as I said for React, an internal hook would be nice for these scenarios.
Michal Srb
Michal Srb6mo ago
Did you look at this page but missed it because it was collapsed, or because it was badly named, or did you not look at this page at all (and if so, why, is it hard to find?). We're always looking for feedback! I see, you want to do this inside a render. We haven't provided that API as it's usually an anti-pattern. What's the use case for showing a potentially stale result from the DB? You could put the value in state if you don't want it to change.
Michal Srb
Michal Srb6mo ago
Help, my app is overreacting!
Reactive backends like Convex make building live-updating apps a cinch, but default behavior might be too reactive for some use cases. Not to worry! L...
hasanaktasTR
hasanaktasTROP6mo ago
My database tables are simply like this:
const brand=defineTable({
name:v.string(),
}).index("by_name",["name"])

const category=defineTable({
name:v.string(),
}).index("by_name",["name"])


const product=defineTable({
name:v.string(),
brandId:v.id("brands"),
categoryIds:v.array(v.id("categories")),
price:v.float64(),
stock:v.float64(),


}).index("by_name",["name"]).index("by_brandId",["brandId"]).index("by_categoryIds",["categoryIds"])


const likedProducts=defineTable({
userId:v.id("users"),
productId:v.id("products"),
}).index("by_userId",["userId"]).index("by_productId",["productId"]).index("by_userId_productId",["userId","productId"])

const dislikedProducts=defineTable({
userId:v.id("users"),
productId:v.id("products"),
}).index("by_userId",["userId"]).index("by_productId",["productId"]).index("by_userId_productId",["userId","productId"])
const brand=defineTable({
name:v.string(),
}).index("by_name",["name"])

const category=defineTable({
name:v.string(),
}).index("by_name",["name"])


const product=defineTable({
name:v.string(),
brandId:v.id("brands"),
categoryIds:v.array(v.id("categories")),
price:v.float64(),
stock:v.float64(),


}).index("by_name",["name"]).index("by_brandId",["brandId"]).index("by_categoryIds",["categoryIds"])


const likedProducts=defineTable({
userId:v.id("users"),
productId:v.id("products"),
}).index("by_userId",["userId"]).index("by_productId",["productId"]).index("by_userId_productId",["userId","productId"])

const dislikedProducts=defineTable({
userId:v.id("users"),
productId:v.id("products"),
}).index("by_userId",["userId"]).index("by_productId",["productId"]).index("by_userId_productId",["userId","productId"])
We show the user 1 product that they can react to by removing the products they like and dislike from all products. Our problem is that when a new product is added while the user is on the screen, the user's screen changes because the result changes. In fact, we only want the query to be refreshed when the user reacts. You can think of it like Tinder. That's why we wanted to use the solution I mentioned. If you have different suggestions, I'd love to hear them. We also thought about calculating and keeping the products that will be shown to the user in a separate table, but it seemed to us that it would make things even more complicated. Because the user can customize the result set by brand and category.
Michal Srb
Michal Srb6mo ago
What is the order in which you want to show the products? (My intuition tells me that React state or ref is probably what you want. Similar to the article I linked, you can decide when to update the state or ref based on the user liking or disliking)
hasanaktasTR
hasanaktasTROP6mo ago
We have read almost all your blog posts. useConvex().query will do the trick for now. Thanks.
ian
ian6mo ago
you can also use TanStack query and make your queryFn do the convex.query but I agree - holding onto the current product client-side, and explicitly changing that so the product doesn't change underneath sounds like the right call.

Did you find this page helpful?