alexcole
CCConvex Community
•Created by n8 on 5/5/2023 in #support-community
Websocket closed error
For anyone else following along, we're discussing the same problem at https://discord.com/channels/1019350475847499849/1108758055677599795/1108772913114521740
16 replies
CCConvex Community
•Created by whoami on 5/18/2023 in #support-community
Bad URL errors "WebSocket closed unexpectedly with code 1006" in vercel preview env + convex dev
Sorry this has been such a pain to debug. Let's try a few more things to get to the bottom of this:
1. Can you DM me preview link? I'd like to see if this happens for me too.
2. This happens in multiple browsers (Chrome and Firefox), correct?
3. What kind of network are you on? Is this home wifi? Is there a firewall?
4. Does this happen on different devices on the same network? Does it persist on a different network (ex your phone on data)?
5. What does this WebSocket connectivity test page show? https://connectivity-test.asana.com/
At my previous company (Asana) we had some major issues with a small number users who had networks that blocked WebSockets. We made that test page to help debug problems.
16 replies
CCConvex Community
•Created by allen on 5/16/2023 in #support-community
Optimistic update from action?
Got it. And is this a single mutation thats fired after the action or could it be one of many mutations?
Not knowing your React components, maybe there is still a way to set some state like
savingAccessToken: true
when the action is fired and rolled back when it completes? Then components know this is happening and render appropriate UI? Definitely hacky.
One other idea from @Indy is that you could persist this state to Convex. In this model, your users
table could have a property like savingAccessToken: v.boolean()
. Then the client could:
1. Fire a saveAccessToken
mutation and use an optimistic update to set savingAccessToken: true
2. On the server, the mutation will set savingAccessToken
and schedule an action
3. The action will get the access token and run a mutation to save it and set savingAccessToken: false
In this way, the client can look at user.savingAccessToken
and know when we're in this flow. Kinda annoying though that this creates a new state in your database to represent what is really only a client concern.6 replies
CCConvex Community
•Created by allen on 5/16/2023 in #support-community
Optimistic update from action?
Sorry, calling actions with optimistic updates isn't supported right now. It's definitely a limitation with calling actions from the client. Because actions can run multiple mutations, take a long time, etc it's hard for us to build correct optimistic updates here.
Some ideas on workarounds:
If possible, could you restructure your code as a mutation that schedules an action for the work that can't be done in the mutation? Then you can apply an optimistic update to the mutation on the client.
If thats not an option, can you handle this manually in your React components? I'm imagining that you set some state along with firing the action and roll that state back when the action completes (the promise resolves).
Happy to brainstorm more if you want to share more details about your use case. Sorry again, I know this is a pain.
6 replies
CCConvex Community
•Created by whoami on 5/15/2023 in #support-community
Use `v.` schema to validate object types?
Idk if this works for your use case
11 replies
CCConvex Community
•Created by whoami on 5/15/2023 in #support-community
Use `v.` schema to validate object types?
Oh actually, one more idea: if you use the discriminated union pattern, you don't actually need the validator to tell which case of the union you're in.
I'm imagining something like:
11 replies
CCConvex Community
•Created by whoami on 5/15/2023 in #support-community
Use `v.` schema to validate object types?
Yeah, for right now there isn't a good way to share validators between your schema and your UI. The best approach is probably to duplicate between Convex validators and Zod (or skip the Convex schema validation entirely and call your Zod validator just before
db.insert
and db.replace
).
But thanks for the feedback! I'll count this as a vote for Zod support and/or the ability to call Convex validators from JS.11 replies
CCConvex Community
•Created by whoami on 5/15/2023 in #support-community
Use `v.` schema to validate object types?
Did you want to reuse this validator outside of argument validation and schemas? That's not supported right now, but it is something we could add in the future.
11 replies
CCConvex Community
•Created by allen on 5/10/2023 in #support-community
Optimistic Update on Paginated Query
Yep, I'd also be excited to eventually build more powerful optimistic updates in our client!
In the meantime, if you want some example code, there is a
optimisticallyUpdateValueInPaginatedQuery
helper for editing results of paginated queries (https://github.com/get-convex/convex-js/blob/main/src/react/use_paginated_query.ts#L405).
Writing a helper to delete a value should be similar (and maybe we should add a helper for this to our npm package).13 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
Oops. I'm a meeting. Around this afternoon?
46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
But I think that the
any
s you found are unrelated to the difficulty updating the types. The any
we needed in withSession
was because TypeScript wasn't simplifying a type automatically anymore46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
Wanna hop on a call and get your middleware fixed up? Seeing how yours works might inform what helpers I should put in the npm package to make this easier for other devs.
46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
But back to your middleware - It's definitely possible to write it given the new validator types, but I wouldn't stress over removing every
any
from your implementation if it isn't exposed in the interface. Happy to hop on a call and help you figure out how to do this for your project.46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
No doubt! And it is possible!
But if you read the Zod source, they are doing the exact same thing. I see over 300
any
s in https://github.com/colinhacks/zod/blob/master/src/types.ts46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
Counterintuitively, when writing complex TS types, I'm a big fan of separating interfaces and implementation and only using the fancy types on the interfaces.
Our main goal is to give developers great type safety in their apps. We're really serious about this and do things like add type-level tests to make sure they work (for example https://github.com/get-convex/convex-js/blob/main/src/api/api.test.ts).
It's nice when the types can also catch bugs that Convex devs make, but thats a secondary goal. In a lot of these cases, convincing TS that all of our implementation matches these complex types is difficult and would slow down our work. Sometimes it's impossible (as @ian and I found with some middleware).
To both move fast on the impls and have correct types we often split them up entirely. Ex https://github.com/get-convex/convex-js/blob/main/src/server/query.ts and https://github.com/get-convex/convex-js/blob/main/src/server/impl/query_impl.ts
We do this in codegen too with the
.d.ts
vs .js
files. TypeScript won't totally catch if they don't match but thats okay. We can test for this in other ways.46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
Thanks for the thoughts! I'm not sure that any of this changes are too high priority given that they don't change the interface of our npm package.
In general,
any
can be a cancer that spreads throughout a TS codebase. That being said, all of these examples are using any
in internal types that aren't exposed in the Convex interface.46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
Lmk if you have questions or want to pair on your middleware! And yeah I think splitting this into
.d.ts
and .ts
files would make this all more clear46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
And this pattern of separating the interface with fancy types from the implementation with simple ones is IMO a good one. If you're doing complex enough stuff it's too hard to do otherwise.
46 replies
CCConvex Community
•Created by RJ on 4/24/2023 in #support-community
Upgrading "middleware" functions to Convex 0.13
Yeah, with the addition of argument validators, the types for middleware have definitely gotten more complicated. The
any
s should all be constrained to the implementation and not the external interface so you're not losing much type safety.46 replies
CCConvex Community
•Created by Chad Maycumber on 4/27/2023 in #support-community
Best practices for modeling "conditional" queries
But this thread has inspired me to work on just getting
enabled
in soon. Should be in Convex 0.14.0 or 0.15.017 replies