-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Introduce skipToken to useQuery #11524
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
Comments
Hi @vezaynk 👋
We agree |
@alessbell Is there a reason against it? Currently I'm writing a lot of code that looks like this: const maybeVar: string | null;
useQuery({
variables: {
myVar: maybeVar as string // this cast shouldn't be necessary
},
skip: !maybeVar
}) code like this is better: const maybeVar: string | null;
useQuery({
variables: maybeVar ? {
myVar: maybeVar
} : skipToken,
}) |
We're big fans of We first introduced |
What you could do to avoid the nasty cast, is conditionally change the variables object: const maybeVar: string | null;
useQuery(QUERY, {
variables: maybeVar ? {
myVar: maybeVar
} : undefined,
skip: !maybeVar
})
// Or:
useQuery(QUERY, maybeVar ? {
variables: {
myVar: maybeVar
},
} : { skip: true }) It's definitely better if you have multiple variables, but I think the skipToken is a much better approach. EDIT: See my next comment for a better workaround I think a good discussion would be where useQuery(QUERY, {
variables: maybeVar ? {
myVar: maybeVar
} : skipToken,
})
useQuery(QUERY, maybeVar ? {
variables: {
myVar: maybeVar
},
} : skipToken) The reason the latter approach is better is because it would make non-variable queries skippable with useQuery(QUERY, !maybeVar ? skipToken : undefined)
// Otherwise you end up with this nasty syntax for non-variable queries:
useQuery(QUERY, { variables: !maybeVar ? skipToken : {} }) // Doesn't make much sense But that opens the question for the following pattern: // Somewhat identical to RTK-Query.
useQuery(maybeVar ? QUERY : skipToken) But then again, that's why we have the useQuery(QUERY, { skip: !maybeVar }) It's not realistic to add all the above accepts for skipToken, so my proposal would be either only as the whole options object (worse DX for non-variable queries), or both the options object and query object (good DX for both cases). If we support both, it should skip if at least any of the arguments have skipToken: useQuery(skipToken, { variables: { ... } }) // Skip
useQuery(QUERY, skipToken) // Skip
useQuery(skipToken, skipToken) // Skip
useQuery(QUERY, { variables: { ... } })) // Don't skip Perhaps unnecessary complexity, so for non-variable queries, stick with |
This form opens up a good point: useQuery(QUERY, maybeVar ? {
variables: {
myVar: maybeVar
},
} : { skip: true }) Because you can simply define export const skipToken = { skip: true }
useQuery(QUERY, maybeVar ? {
variables: {
myVar: maybeVar
},
} : skipToken) That said, |
@AlexanderArvidsson for consistency reasons, we'll make useQuery(QUERY, skipToken) // valid
useQuery(QUERY, id ? { variables: { id } } : skipToken) // valid
useQuery(skipToken) // invalid
useQuery(QUERY, { variables: skipToken }) // invalid The reason we went with this approach is to provide better TypeScript errors when your query has required variables and you haven't provided them. We should require you to define For that reason, we don't suggest this approach: useQuery(QUERY, maybeVar ? {
variables: {
myVar: maybeVar
},
} : { skip: true }) Once we get this work done, the above would be invalid (assuming We have already started this work in #11241, but need some time to come back to it (6 million priorities don't help 😆). As @alessbell said, we're big fans of this pattern and its more of "when" not an "if" at this point. |
@jerelmiller I absolutely agree! I definitely think the right path is the full options object, for type safety and consistency reasons. My approach I added at the end was as a workaround that works today but shouldn't work in the future, and people could define that themselves in their code until we have proper skipToken support. Then, it would simply be switching the import to the proper skipToken. |
@jerelmiller could you further explain your point here? You still aren't providing a In a project I work on we use What is so wrong with the example above in the meantime? |
Hey @jrnxf 👋 Great question! We want to do a bit of prework to the types on As a result of that change, this means that the I'm simply saying that we don't recommend that form of the ternary options for |
Currently the documentation says that skiptokens are for suspense queries:
apollo-client/docs/source/api/react/hooks.mdx
Line 669 in 038629c
Can it be expanded to cover all query types?
The text was updated successfully, but these errors were encountered: