-
Notifications
You must be signed in to change notification settings - Fork 1
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
blog/leveraging-the-query-function-context #32
Comments
Great post as always! 👍 I may be wrong, but it seems to me that I found typos in the examples. In all examples ... fetchTodo(...) should be replaced (?) with: ... fetchTodos(...) and in the const todoKeys = {
// ✅ all keys are arrays with exactly one object
all: [{ scope: 'todos' }] as const,
lists: () => ({ ...todoKeys.all[0], entity: 'list' } as const),
list: (state: State, sorting: Sorting) =>
({ ...todoKeys.lists()[0], state, sorting } as const),
} missing extra square brackets for the const todoKeys = {
// ✅ all keys are arrays with exactly one object
all: [{ scope: 'todos' }] as const,
lists: () => ([{ ...todoKeys.all[0], entity: 'list' }] as const),
list: (state: State, sorting: Sorting) =>
({ ...todoKeys.lists()[0], state, sorting } as const),
} |
And if I understand correctly the const todoKeys = {
...
...
list: (state: State, sorting: Sorting) =>
([{ ...todoKeys.lists(), state, sorting }] as const),
} |
I thought query key arrays must begin with strings, is that rule obsolete? |
@rchubatov you are of course right on both accounts. I guess I was rushing the publishing a bit this time 😅. If you want, I’d accept a PR to fix it (https://github.com/tkdodo/blog), otherwise I’ll do it later 😊 |
@joshwilsonvu the query key must be a string or an array. There is no restriction of what can go in the array. The type is defined here: https://github.com/tannerlinsley/react-query/blob/4d762a4ffe16ea126c432460699f04bf73463f23/src/core/types.ts#L6 |
Cool! |
wow awesome |
This is amazing |
Awesome work! Loving the blog, I'm just getting started with I don't know if this will help anyone but I created a small utility to help type the context: type QueryContext<
keys extends { [K: string]: QueryKey | ((...params: any[]) => QueryKey) },
mode extends keyof keys
> = keys[mode] extends (...params: any[]) => QueryKey
? QueryFunctionContext<ReturnType<keys[mode]>>
: keys[mode] extends QueryKey
? QueryFunctionContext<keys[mode]>
: never;
// Can then be used like this:
const fetchTodos = async ({ queryKey: [{ state, sorting }] }: QueryContext<typeof todoKeys, "list">) => {
...
} |
Feels like |
@Faithfinder yes, possibly. It's just an example 😅 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Leveraging the Query Function Context | TkDodo's blog
Use what React Query provides for optimal type safety
https://tkdodo.eu/blog/leveraging-the-query-function-context
The text was updated successfully, but these errors were encountered: