Skip to content

refactor(query-core): Make MutationMeta and QueryMeta generic - #5457

Closed
vojvodics wants to merge 2 commits into
TanStack:alphafrom
vojvodics:refactor/query-mutation-meta-generics
Closed

refactor(query-core): Make MutationMeta and QueryMeta generic#5457
vojvodics wants to merge 2 commits into
TanStack:alphafrom
vojvodics:refactor/query-mutation-meta-generics

Conversation

@vojvodics

Copy link
Copy Markdown

This is a rework of #5412 for v5

The basic idea is that we can infer types from meta fields passed in as functions:

Screenshot 2023-05-24 at 10 51 26 Without inference Screenshot 2023-05-24 at 10 51 58 With inference

This, however, requires some setup from the user:


interface MyMeta<
TData = unknown,
TError = unknown,
TVariables = unknown,
TContext = unknown
> {
errorMessage: string | ((error: TError, variables: TVariables, context: TContext) => string);
// I can also infer the type of data correctly
successMessage: string | ((data: TData, variables: TVariables, context: TContext) => string);
}
declare module '@tanstack/react-query' {
interface Register<
TData = unknown,
TError = unknown,
TVariables = unknown,
TContext = unknown
> {
mutationMeta: MyMeta<TData, TError, TVariables, TContext>;
}
}

Also, as mentioned in #5412 - the inference will only work if used in useMutation/useQuery.

Because all generic parameters are optional, this should work exactly the same as before and you don't need to use generics if you don't have to.

@vercel

vercelBot commented May 24, 2023

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
NameStatusPreviewCommentsUpdated (UTC)
query⬜️ Ignored (Inspect)May 26, 2023 3:42pm

@codesandbox-ci

codesandbox-ciBot commented May 24, 2023

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@nx-cloud

nx-cloudBot commented May 26, 2023

Copy link
Copy Markdown

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 6928516. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@TkDodo

Copy link
Copy Markdown
Collaborator

what's problematic about using functions inside meta is that you really can't close over anything in your component, because there is only one meta object per query, and you'd likely read stale values:

function MyComponent(props) {
useQuery({
queryKey,
queryFn,
meta: () => props.foo
})
}

so, I'm unsure if we should make it generic just to support that case 🤔

@vojvodics

Copy link
Copy Markdown
Author

yeah, I see the reason to discourage the usage of functions in meta. That's a problem even without generics.
I'd argue that the example you provided is more of a case in favor of generics - I would have done precisely that instead of reading the values from the query.

@TkDodo
TkDodo deleted the branch TanStack:alphaJuly 24, 2023 09:59
@TkDodoTkDodo closed this Jul 24, 2023
@TkDodo

Copy link
Copy Markdown
Collaborator

please reopen to the beta branch, thanks.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@vojvodics@TkDodo@lachlancollins