Skip to content
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

feat!: upgrade to use @tanstack/react-query@v5 #44

Merged
merged 1 commit into from
Feb 15, 2024
Merged

Conversation

jkdowdle
Copy link
Contributor

@jkdowdle jkdowdle commented Feb 12, 2024

Motivation

This pr makes the breaking change adopting @tanstack/react-query@v5

It includes

  • upgrade react-query package,
  • setup migration guide
  • align with latest version of react-query for supported api
    • new min typescript version
    • Set error types to DefaultError over unknown (react-query has a way to over ride it to change it from Error)
    • loading => pending
    • Callbacks on useQuery have been removed (e.g., onSuccess, onError and onSettled)
    • no longer able to pass page params in fetch next / previous methods
    • cacheTime => gcTime
    • refetchPage => maxPages

Next Steps

This allows us to now support api typed suspense hooks that are available in @tanstack/react-query@v5

@lifeomic-probot
Copy link

lifeomic-probot bot commented Feb 12, 2024

⚠️ Detected a breaking change in commit aa2afb5

Merging this PR will result in a major version bump.

Created by lifeomic-probot (Enforce Semantic Commits)

Copy link
Contributor

@swain swain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question.


- The minimum required TypeScript version is now 4.7

- TypeScript: `Error` is now the default type for errors instead of `unknown`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's cool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure! we could even globally override the default error value, if someone wanted to keep unknown, or to an AxiosError

https://tanstack.com/query/v5/docs/framework/react/typescript#registering-a-global-error

since do rely on axios for this package we could make that assumption I suppose

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!


- Removed `keepPreviousData` in favor of `placeholderData` identity function

- Rename `cacheTime` to `gcTime`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a much better name

src/hooks.ts Show resolved Hide resolved
src/types.ts Show resolved Hide resolved
Comment on lines -76 to -90
type RestrictedUseInfiniteQueryResult<Response, Request> = Omit<
UseInfiniteQueryResult<Response>,
'fetchNextPage' | 'fetchPreviousPage'
> & {
fetchNextPage: (
options?: Omit<FetchNextPageOptions, 'pageParam'> & {
pageParam: Partial<Request>;
},
) => Promise<InfiniteQueryObserverResult<Response>>;
fetchPreviousPage: (
options?: Omit<FetchPreviousPageOptions, 'pageParam'> & {
pageParam: Partial<Request>;
},
) => Promise<InfiniteQueryObserverResult<Response>>;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this type going away?

Copy link
Contributor Author

@jkdowdle jkdowdle Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swain good question. We no longer need to override any of the types for the result of useInfiniteAPIQuery

As part of the v5 migration guide they make it clear that they no longer allow passing pageParam through fetchNextPage or fetchPreviousPage. see here

There main reasoning being

This feature didn't work at all with refetches and wasn't widely known or used

so we can rely on the default types for these methods

@jkdowdle
Copy link
Contributor Author

jkdowdle commented Feb 12, 2024

Proposal

This PR does not include this change - but you can view some code changes to get an idea at #45. it is incomplete and only makes the change for useAPIQuery

To Better align with with the api changes in react-query, I proposed that the api change to providing the request "input" or payload as part of the third argument options. Making options required for at least that property.

- useAPIQuery(route, payload, options)
+ useAPIQuery(route, { payload, ...options })

This is mostly to try to come closer to the api react-query exposes. in v5 all hooks now take a single options object. See more here


This was proposed to a small group and receptions were mixed so interested to hear others ideas. Here are some brief notes. Was curious if anyone else has had thoughts

  • Pro: it does get a bit closer to their api and will be a bit more familiar for anyone who has worked with @tanstack/react-query@v5

  • Pro: generally prefer a single object to multiple arguments

  • Con: this introduces a larger breaking change to one-query.

  • Con: The main reason react-query made this change was to simplify their implementation (typings + runtime object check) with the side benefit of "one way to do something". We don't get any of those benefits here in one-query

@jkdowdle jkdowdle mentioned this pull request Feb 12, 2024

- Removed `refetchPage` in favor of `maxPages`

- The experimental `suspense: boolean` flag on the query hooks has been removed - new hooks for suspense have been added
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #43

Copy link
Contributor

@swain swain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work!

@jkdowdle jkdowdle requested a review from MMFane February 12, 2024 22:47
.request(route, payload, options?.axios)
.then((res) => res.data);
.request(route, payload, options.axios)
.then((res) => res.data) as any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we need this new cast here? It seems like it was strongly typed before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see if I can fix this up in a follow up. Types have changed quite a bit in latest

@@ -92,7 +100,7 @@ export const createAPIHooks = <Endpoints extends RoughEndpoints>({
client
.request(endpoint, payload, options?.axios)
.then((res) => res.data),
})),
})) as [...QueriesOptions<any>],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Why did we lose the strong typing we had before? I think it's worth commenting on why we need the explicit casts now if there's no way around it.

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, @ts-expect-error is used many times below with comments, but now need to disable the eslint rule too

This document provides instructions for upgrading between major
versions of `@lifeomic/one-query`

### 3.x -> 4.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be 4.x -> 5.x?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react-query went from 4 to 5, but v4 will be the next major version of one-query

@jkdowdle jkdowdle merged commit 4a3793b into master Feb 15, 2024
4 checks passed
@jkdowdle jkdowdle deleted the react-query@v5 branch February 15, 2024 17:20
Copy link

🎉 This PR is included in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions github-actions bot added the released Has been released to the package repository (NPM, etc) label Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Has been released to the package repository (NPM, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants