Skip to content

refactor(queryClient): accept generics on setQueryDefaults and setMutationDefaults#5983

Closed
DarioSiroki wants to merge 9 commits intoTanStack:rcfrom
DarioSiroki:feature/generics-on-mutation-defaults
Closed

refactor(queryClient): accept generics on setQueryDefaults and setMutationDefaults#5983
DarioSiroki wants to merge 9 commits intoTanStack:rcfrom
DarioSiroki:feature/generics-on-mutation-defaults

Conversation

@DarioSiroki
Copy link

@DarioSiroki DarioSiroki commented Sep 9, 2023

This change improves type inferring and improves DX.

As an example, before this PR, onSettled and onError's parameters would not be typed/automatically inferred. After this PR, and thanks to v5's default Error, the whole thing is now typed after you specify mutationFn and create a context. This means developer doesn't have to manually type cast anymore if he is working with data, context, error or response variables inside setMutationDefaults or setQueryDefaults methods.

queryClient.setMutationDefaults(key, {
  mutationFn: async (text: string) => text,
  onMutate: () => {
    return 1
  },
  onSettled: (data, error, context) => {

  },
  onError(error, variables, context) {

  },
})

…ationDefaults

This change improves type inferring and improves DX.

As an example, before this PR, variables accepted by onSettled and onError would not be typed. After this PR, and thanks to v5's default Error,
the whole thing is now typed after you specify mutationFn and create a context. This means developer doesn't have to manually type cast anymore if
he is working with data, context, error or response variables inside setMutationDefaults or setQueryDefaults methods.

    queryClient.setMutationDefaults(key, {
      mutationFn: async (text: string) => text,
      onMutate: () => {
        return 1
      },
      onSettled: (data, error, context) => {

      },
      onError(error, variables, context) {

      },
    })
@vercel
Copy link

vercel bot commented Sep 9, 2023

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

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Visit Preview Oct 17, 2023 0:02am

@nx-cloud
Copy link

nx-cloud bot commented Sep 11, 2023

☁️ Nx Cloud Report

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

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@DarioSiroki
Copy link
Author

I see the cause, I'll fix it

@TkDodo
Copy link
Collaborator

TkDodo commented Sep 25, 2023

there's still a build error for the vue adapter:

src/queryClient.ts(255,3): error TS2416: Property 'setQueryDefaults' in type 'QueryClient' is not assignable to the same property in base type 'QueryClient'.
  Type '<TData = unknown, TError = Error, TVariables = void, TContext = unknown>(queryKey: MaybeRefDeep<QueryKey>, options: MaybeRefDeep<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>) => void' is not assignable to type '<TData = unknown, TError = Error, TVariables = void, TContext = unknown>(queryKey: QueryKey, options: Partial<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>) => void'.
    Types of parameters 'options' and 'options' are incompatible.
      Type 'Partial<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>' is not assignable to type 'MaybeRefDeep<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>'.
        Type 'Partial<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>' is not assignable to type '{ throwOnError?: MaybeRefDeep<ThrowOnError<TData, TError, TContext, QueryKey> | undefined>; staleTime?: MaybeRefDeep<number | undefined>; ... 26 more ...; _optimisticResults?: MaybeRefDeep<...>; }'.
          Types of property 'initialData' are incompatible.
            Type 'TContext | InitialDataFunction<TContext> | undefined' is not assignable to type 'MaybeRefDeep<TContext | InitialDataFunction<TContext> | undefined>'.
              Type 'TContext' is not assignable to type 'MaybeRefDeep<TContext | InitialDataFunction<TContext> | undefined>'.
                Type 'TContext' is not assignable to type 'Ref<InitialDataFunction<TContext> | (TContext extends Function ? TContext : TContext extends object ? { [Property in keyof TContext]: MaybeRefDeep<...>; } : TContext) | undefined>'.

DTS Build error

@TkDodo TkDodo changed the base branch from beta to rc September 25, 2023 08:27
@DarioSiroki
Copy link
Author

there's still a build error for the vue adapter:

src/queryClient.ts(255,3): error TS2416: Property 'setQueryDefaults' in type 'QueryClient' is not assignable to the same property in base type 'QueryClient'.
  Type '<TData = unknown, TError = Error, TVariables = void, TContext = unknown>(queryKey: MaybeRefDeep<QueryKey>, options: MaybeRefDeep<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>) => void' is not assignable to type '<TData = unknown, TError = Error, TVariables = void, TContext = unknown>(queryKey: QueryKey, options: Partial<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>) => void'.
    Types of parameters 'options' and 'options' are incompatible.
      Type 'Partial<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>' is not assignable to type 'MaybeRefDeep<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>'.
        Type 'Partial<Omit<QueryObserverOptions<TData, TError, TVariables, TContext, QueryKey, never>, "queryKey">>' is not assignable to type '{ throwOnError?: MaybeRefDeep<ThrowOnError<TData, TError, TContext, QueryKey> | undefined>; staleTime?: MaybeRefDeep<number | undefined>; ... 26 more ...; _optimisticResults?: MaybeRefDeep<...>; }'.
          Types of property 'initialData' are incompatible.
            Type 'TContext | InitialDataFunction<TContext> | undefined' is not assignable to type 'MaybeRefDeep<TContext | InitialDataFunction<TContext> | undefined>'.
              Type 'TContext' is not assignable to type 'MaybeRefDeep<TContext | InitialDataFunction<TContext> | undefined>'.
                Type 'TContext' is not assignable to type 'Ref<InitialDataFunction<TContext> | (TContext extends Function ? TContext : TContext extends object ? { [Property in keyof TContext]: MaybeRefDeep<...>; } : TContext) | undefined>'.

DTS Build error

Sorry, been a bit busy lately. I'd probably fix it already but I'm having difficulties with running the vue adapter which I haven't figured out yet. The workspace imports of tanstack core are broken. Is there any additional step to do besides pnpm install that I've missed?

@TkDodo
Copy link
Collaborator

TkDodo commented Sep 26, 2023

@DamianOsipiuk could you have a look maybe?

@DarioSiroki
Copy link
Author

@DamianOsipiuk could you have a look maybe?

I figured out what was the issue, I had to build query-core since vue-query imports from query-client's /build folder

Still need a bit more time to check some things, I'll push a fix soon™

BTW contributing.md says "we are using pnpm v7" which wasted some of my time - I thought that the fact that I'm using v8 might be the issue. I was fighting corepack to use v7, but it kept using v8 because that's what's specified in package.json. Might wanna change that note on the contributing.md because it's outdated

@DarioSiroki DarioSiroki force-pushed the feature/generics-on-mutation-defaults branch from 3a2da21 to 8b125c0 Compare October 1, 2023 08:52
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 1, 2023

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.

Latest deployment of this branch, based on commit d8730c3:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@codecov-commenter
Copy link

Codecov Report

All modified lines are covered by tests ✅

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Files Coverage Δ
packages/query-core/src/queryClient.ts 97.00% <ø> (ø)

... and 71 files with indirect coverage changes

📢 Thoughts on this report? Let us know!.

@TkDodo TkDodo deleted the branch TanStack:rc October 17, 2023 12:16
@TkDodo TkDodo closed this Oct 17, 2023
@simonecervini
Copy link
Contributor

Hey @TkDodo,

I saw that this PR got closed, maybe when the rc branch was deleted? Just wanted to check if that was accidental or if there was another reason behind it.

I’m interested in this feature and would love to help out if needed!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants