Skip to content

Error boundary handling for useQueries #4177

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

Merged
merged 14 commits into from
Sep 24, 2022

Conversation

zorzysty
Copy link
Collaborator

@zorzysty zorzysty commented Sep 15, 2022

closes #2395

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 15, 2022

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 e5ab278:

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

@codecov-commenter
Copy link

codecov-commenter commented Sep 16, 2022

Codecov Report

Base: 96.36% // Head: 96.59% // Increases project coverage by +0.23% 🎉

Coverage data is based on head (e5ab278) compared to base (eab6e2c).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4177      +/-   ##
==========================================
+ Coverage   96.36%   96.59%   +0.23%     
==========================================
  Files          45       73      +28     
  Lines        2281     2969     +688     
  Branches      640      816     +176     
==========================================
+ Hits         2198     2868     +670     
- Misses         80       99      +19     
+ Partials        3        2       -1     
Impacted Files Coverage Δ
src/core/utils.ts
src/devtools/tests/utils.tsx
src/core/mutationObserver.ts
src/react/useQuery.ts
src/react/utils.ts
src/react/Hydrate.tsx
src/react/QueryErrorResetBoundary.tsx
src/devtools/devtools.tsx
src/core/mutation.ts
src/core/mutationCache.ts
... and 108 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@zorzysty zorzysty changed the title Error boundary for usequeries Error boundary for useQueries Sep 16, 2022
@zorzysty zorzysty changed the title Error boundary for useQueries Error boundary handling for useQueries Sep 16, 2022
Copy link
Collaborator

@TkDodo TkDodo left a comment

Choose a reason for hiding this comment

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

very good work ❤️

@@ -188,5 +190,35 @@ export function useQueries<T extends any[]>({
observer.setQueries(defaultedQueries, { listeners: false })
}, [defaultedQueries, observer])

const errorResetBoundary = useQueryErrorResetBoundary()

defaultedQueries.forEach((query) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we rename this from queries to options ? I think this is highly confusing right now 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

sorry, I meant more globally that defaultedQueries should also probably be defaultedOptions. It's not a query 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

okay nevermind, this is actually a bigger refactoring. we have queries everywhere and also setQueries...

Copy link
Collaborator Author

@zorzysty zorzysty Sep 19, 2022

Choose a reason for hiding this comment

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

EDIT:
Just to make it clear, I'm responding to this comment:

sorry, I meant more globally that defaultedQueries should also probably be defaultedOptions. It's not a query

ORIGINAL COMMENT BELOW:
I was struggling to grasp the naming conventions here, and when I thought I finally got it, this comment got me confused again 😁

Let me explain how I understand it. First, here are 3 facts about the code in this PR its current state:

  1. useQueries takes queries array as an argument
export function useQueries<T extends any[]>({
  queries,
  context,
}: {
  queries: readonly [...QueriesOptions<T>]
  context?: UseQueryOptions['context']
})
  1. Each item of queries array is a query, but query is basically an options object
  2. These queries are then defaulted and stored as defaultedQueries:
const defaultedQueries = React.useMemo(
  () =>
    queries.map((options) => {
      const defaultedOptions = queryClient.defaultQueryOptions(options)

      // Make sure the results are already in fetching state before subscribing or updating options
      defaultedOptions._optimisticResults = isRestoring
        ? 'isRestoring'
        : 'optimistic'

      return defaultedOptions
    }),
  [queries, queryClient, isRestoring],
)

It would be strange if mapping with default values would suddenly convert queries to options. Furthermore in the code below QueriesObserver constructor has second parameter named queries and not options (although queries is typed as QueryObserverOptions, because why not xD).

const [observer] = React.useState(
  () => new QueriesObserver(queryClient, defaultedQueries),
)

So here are my thoughts to sum it up:
If we rename defaultedQueries to defaultedOptions it would be confusing because options are of a single query and not of queries array (useQueries does not have options on its own). To make it more consistent and less confusing, I would instead suggest to take the example from how queries are typed in useQueries arguments (type is called QueriesOptions) and do the name change this way:

defaultedQueries -> defaultedQueriesOptions

WDYT?

If that makes sense, maybe we could also do this to make it even more consistent:

export function useQueries<T extends any[]>({
  queries: queriesOptions,
  context,
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah the naming is weird, I agree. But we have a public method called setQueries that actually takes options in, but they are named queries :/

so for now, let's:

  • revert the lats commit (sorry) and just keep it queries for now. Being consistent is better I think.

there are also conflicts now, sorry. If those are solved we can go ahead and merge 🚀

@zorzysty zorzysty force-pushed the error_boundary_for_usequeries branch from 5552172 to 7f81e88 Compare September 18, 2022 13:16
@TkDodo TkDodo merged commit 34ca558 into TanStack:main Sep 24, 2022
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.

useErrorBoundary option works in useQueries?
3 participants