Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependent queries are not working with tanstack query #5

Open
Irutom opened this issue Aug 26, 2024 · 6 comments
Open

Dependent queries are not working with tanstack query #5

Irutom opened this issue Aug 26, 2024 · 6 comments

Comments

@Irutom
Copy link

Irutom commented Aug 26, 2024

Convex query is executed even if the enabled condition is not met.

  const { fetchStatus, data: ordersResponse, isError, isPending: isOrderPending, isSuccess } = useQuery({
    ...convexQuery(api.orders.getOrdersByCartIds, { cartDocIds: cartDocIds ?? [], fetchProducts: true }),
    enabled: !!cartDocIds?.length,
  });

Query

export const getOrdersByCartIds = zQuery({
  args: {
    cartDocIds: z.array(zid(CollectionNameEnum.enum.shopping_carts)).min(1),
    fetchProducts: z.boolean().optional(),
  },
...

Below is the error

Uncaught ConvexError: {"ZodError":[{"code":"too_small","minimum":1,"type":"array","inclusive":true,"exact":false,"message":"Array must contain at least 1 element(s)","path":["cartDocIds"]}]}

https://tanstack.com/query/latest/docs/framework/react/guides/dependent-queries

@Irutom Irutom changed the title Dependent queries not working with tanstack query Dependent queries are not working with tanstack query Aug 26, 2024
@thomasballinger
Copy link
Collaborator

For anyone looking for a workaround, you can use the non-TanStack hook useConvexQuery():

  const ordersResponse = useConvexQuery(
    api.orders.getOrdersByCartIds, 
    !!cartDocIds?.length ? { cartDocIds: cartDocIds ?? [], fetchProducts: true }) : "skip"
    // "skip" is a special value that causes the query not to run https://docs.convex.dev/client/react#skipping-queries
  });

We should support this though, shouldn't be hard.

@jamalsoueidan
Copy link

Why not just this method below?

const convex = useConvex();
// later
const result = await convex.query(api.asd);

@thomasballinger
Copy link
Collaborator

The dependent queries @Irutom wants here are still reactive, just off sometimes when the arguments aren't available.

@github-staff github-staff deleted a comment from Lxx-c Oct 23, 2024
@github-staff github-staff deleted a comment from Lxx-c Oct 23, 2024
@github-staff github-staff deleted a comment from Lxx-c Oct 23, 2024
@thomasballinger
Copy link
Collaborator

This works for "skip" now, but not enabled: false

@Irutom
Copy link
Author

Irutom commented Nov 6, 2024

@thomasballinger Could you clarify whether you’re referring to tanstack’s useQuery or convex’s useConvexQuery()? An example would be helpful for better understanding.

@thomasballinger
Copy link
Collaborator

thomasballinger commented Nov 6, 2024

@Irutom here's an example

// This is a conditional query

This is convexQuery used to create options for TanStack's useQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@jamalsoueidan @thomasballinger @Irutom and others