-
Notifications
You must be signed in to change notification settings - Fork 463
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
fix(vue): fix variables typing #3734
base: main
Are you sure you want to change the base?
Conversation
|
aceb2cc
to
d0b6106
Compare
- Revert the definitions of MaybeRefObj, UseQueryArgs, and UseSubscriptionArgs introduced in d07602d to resolve issue urql-graphql#3733 - Rename MaybeRef, MaybeRefObj, unwrap to MaybeRefOrGetter, MaybeRefOrGetterObj, and toValue for consistency with Vue 3.3+ - Replace deep unwrapping (introduced in 068df71) of variables with a simple toValue - Revert test changes introduced in 068df71 - Fix 'reacts to variables changing' test by wrapping variables in reactive() since deep unwrapping is no longer performed
Rename the variables for consistency between the doc and the implementation Add description of variable extensions
@JoviDeCroock Would you mind taking a look when you have a chance? Thank you! |
Hi @JoviDeCroock, I received on email from GitHub on January 23th with a response from you, but I can't see any in this thread, did you delete it? Just some more info (since you were concerned with the modifications to the tests): basically I reverted the test to their state before 068df71. Many of the test introduced in this commit are testing the deep unwrapping behavior, which is IMO not desirable, that's why I reverted most of it. I'm not exactly sure how to test that type safety is working, but I can see two solutions:
|
Summary
Background
This PR addresses issues introduced by the deep unwrapping of the
variables
field:variables
becomes extremely challenging (if not impossible)variables
(and maybe more)The proposal is to revert to simple unwrapping, along with several improvements detailed below.
Should this proposal be accepted, I'd be happy to update the documentation.
Changes
MaybeRefObj
,UseQueryArgs
, andUseSubscriptionArgs
definitions from d07602dMaybeRef
→MaybeRefOrGetter
MaybeRefObj
→MaybeRefOrGetterObj
unwrap
→toValue
variables
(introduced in 068df71) with a simpletoValue
'reacts to variables changing'
test to usereactive()
wrapper since deep unwrapping is no longer performedpnpm-lock.yaml
to use updated dependenciescreateRequest
documentationImplementation Note
In
createRequestWithArgs
, I had to use a type assertion for_args.variables
as TypeScript struggles with type inference in this case. While the typing appears correct, I welcome review of this approach. There is also a related question on theGraphQLRequestParams
definition (discussed here). Alternative suggestions for a more elegant solution are appreciated.Discussion Point: Query Field Reactivity
While this PR doesn't modify it, I question whether the
query
field should support reactivity. Creating a new query viauseQuery
or the client handle seems more appropriate. Additionally, TypeScript users wouldn't be able to replace thequery
value with a different type, which arguably makes this feature useless.Discussion Point 2: Optional Variables
As noted here, the current implementation of
MaybeRefObj
also breaks the behavior ofGraphQLRequestParams
, which makes thevariables
field optional in certain cases, for example for queries that do not require variables. In the definition ofUseQueryArgs
, the intersection with this type is wrapped in an outerMaybeRefObj
:This causes the
variables
field to be non-optional even for queries that do not require variables. The reason comes from the definition ofMaybeRefObj
and the fact that thatExact<{ [key: string]: never; }>
extends{}
.Proposed solutions
MaybeRefObj
definition:This allows omitting
variables
when not needed while maintaining type safety, but note that other edge cases may exist.UseQueryArgs
definition:This approach would resolve all the possible edge cases and improve type safety maintainability. It would also make the
query
field non-reactive, which, as I argued in the previous point, aligns with best practices.