Skip to content

Conversation

@kamranayub
Copy link
Contributor

@kamranayub kamranayub commented Jul 29, 2020

Context

See test cases but when using useQuery with no config provider and a custom query cache provider, the defaultConfig was not being merged correctly in queryCache.buildQuery.

A previously failing test:

test('uses defaultConfig for queries when they don\'t provide their own config', async () => {
    const cache = makeQueryCache({
      defaultConfig: {
        queries: {
          staleTime: Infinity,
        },
      },
    })

    function Page() {
      const { data } = useQuery('test', async () => {
        await sleep(10)
        return 'test'
      })

      return (
        <div>
          <h1>{data}</h1>
        </div>
      )
    }

    const rendered = render(
      <ReactQueryCacheProvider queryCache={cache}>
        <Page />
      </ReactQueryCacheProvider>
    )

    await waitFor(() => rendered.getByText('test'))

    expect(cache.getQuery('test')).toBeDefined()
    expect(cache.getQuery('test').config.staleTime).toBe(Infinity)
    cache.clear({ notify: false })
  })

useConfigContext was falling back to defaultConfigRef but when using makeQueryCache, the ref is not updated (on purpose) when merging in defaultConfig.

Now the actual merged configRef for the query cache is exposed so that we can access it in useConfigContext.

Wrong queryCache variable being passed

I fixed another two issues with buildQuery and notifyGlobalListeners that was passing the queryCache module variable instead of this in the new refactored TS code.

Exposing the config ref

I'm not sure whether you want it exposed off the instance like that or through some other means (since this would also consumers to mess with it?). However, it does address the issue.

I am exposing it through QueryCache.getDefaultConfig which could be used to freeze or clone the config to prevent mutation.

Exposing it like this would mean:

  • It's in the public API
  • It should be added to API docs
  • People could mess with it

I can document this as a new API, I could freeze it, whatever we think would make sense. We just need to get a reference to the merged queryCache-specific config.

Changes

  • Use useQueryCache in useConfigContext to fallback to queryCache's configRef
  • Expose QueryCache.getDefaultConfig() method
  • Fix passing default queryCache ref in notifyGlobalListeners
  • Fix passing default queryCache ref in buildQuery

@vercel
Copy link

vercel bot commented Jul 29, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/tannerlinsley/react-query/hwdovaqmd
✅ Preview: https://react-query-git-fork-kamranayub-fix-default-config-merge.tannerlinsley.vercel.app

export function useConfigContext() {
return React.useContext(configContext) || defaultConfigRef.current
const queryCache = useQueryCache()
return React.useContext(configContext) || queryCache.configRef.current
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default queryCache.configRef will be defaultConfigRef out-of-the-box unless you're using makeQueryCache.

@kamranayub
Copy link
Contributor Author

I am currently "integration" testing this in our repo.

@kamranayub
Copy link
Contributor Author

kamranayub commented Jul 30, 2020

Just seeing weird issues locally when testing where the query cache I am creating and providing via ReactQueryCacheProvider isn't actually being used in the component. Not sure yet what is causing that, whether it's a package linking issue or an actual issue with the code.

edit: not sure why GH thinks there are 58 changes after rebasing, those aren't actual changes from master.

edit 2: there we go 🤷‍♂️

@kamranayub kamranayub changed the base branch from master to docs July 30, 2020 16:47
@kamranayub kamranayub changed the base branch from docs to master July 30, 2020 16:48
@kamranayub
Copy link
Contributor Author

OK, the issue is that there were two major bugs with makeQueryCache and QueryCache usage because they were unknowingly passing queryCache module variable in a few places accidentally. I will fix those issues in this too.

@kamranayub
Copy link
Contributor Author

Alrighty, this is working now as I would expect!

@kamranayub kamranayub marked this pull request as ready for review July 31, 2020 13:17
@kamranayub kamranayub changed the title fix(useConfigContext): Use queryCache configRef which has merged defaultConfigRef fix(QueryCache): Use default configuration used for QueryCache instance for context Jul 31, 2020
@tannerlinsley tannerlinsley merged commit 21d942b into TanStack:master Jul 31, 2020
@tannerlinsley
Copy link
Member

Fantastic!

@tannerlinsley
Copy link
Member

🎉 This PR is included in version 2.5.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants