Redux-toolkit: Final RTKQ alpha feedback, fixes, and tweaks

Created on 27 Apr 2021  路  10Comments  路  Source: reduxjs/redux-toolkit

Gotta resolve any issues that get reported during the alpha, and apply any final changes left over from the old repo.

Most helpful comment

@Shrugsy that snippet seems worth adding to "Customizing Queries"

All 10 comments

Can't wait to test RTK-Query in my new projects!

Minor note, in the changelog the import path is import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/react' when it looks like it should be import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'.

@mgcrea Thanks, updated it.

Yeah, we need to go through and add visibility attributes to all types so we stop getting extraction warnings.

Running list of merged PRs since alpha.2 for reference:

  • #1026: add export for copyWithStructuralSharing
  • #1029: fix/default serialize query args
  • #1022: export Result Definition types
  • #1035: simplify selectFromResult usage
  • #1038: fix MiddlewareArray for new build
  • #1039: remove last traces of TSDX
  • #1042: Docs - RTKQ - Convert codeblocks to TS & enable codeblock transpilation
  • #1036: Docs - add 'Cached data' concept page
  • #1034: split up middleware, add OptionalPromise, add cache entry lifecycle
  • #1041: prevent badly typed Middleware<any> from casting dispatch to any
  • #1049: Docs - add Streaming Updates page
  • #1053: add a per-endpoint keepUnusedDataFor option
  • #1051: remove alpha.2 deprecation fallbacks

just some minor tweaks :laughing:

Still missing on that list:

  • #1056 allow using skipSymbol as arg in hooks
  • add a merge (or mergeWithLastResult option to endpoints)
  • add a docs page w/ an example for infinite scrolling (actually, without using merge, that's only gonna be useful in a "streaming" context)
  • add visibility attributes to all types and decide what is public and what is internal

And then I think I'm actually really out of ideas on what else to add.

First of all, excellent functionalities for RTK query, start refactoring our codes by adopting alpha.2, can't wait the official release.
A quick question ,Is there any way to change baseUrl after calling createApi?
In our app we allow user to change the server address which is stored in the redux state, and will be used in following REST API call. It seems that baseUrl for RTKQ can't be modified after initialization.
Thanks!

@bravew : phryneas mentioned on Discord that this could be done by wrapping baseQuery, as that have access to getState. I had a similiar usecase, where i wanted to add a projectId to the url. Here is an (untested, but hopefully working) example:

import { fetchBaseQuery } from '@rtk-incubator/rtk-query';
import { createApi } from '@rtk-incubator/rtk-query/react';

const baseQuery = fetchBaseQuery({
  baseUrl: 'http://localhost:8000/'
});

const wrappedBaseQuery = (
  ...[args, api, extraOptions]: Parameters<typeof baseQuery>
) => {
  const partialUrl = typeof (args) === 'string' ? args : args.url
  const projectId = (api.getState() as any).auth.projectId // circular reference if as RootState 
  const url = `project/${projectId}/${partialUrl}`
  const newArgs = typeof (args) === 'string' ? url : { ...args, url }
  return baseQuery(newArgs, api, extraOptions)
}

export const api = createApi({
  baseQuery: wrappedBaseQuery,
...

(Note that I haven't updated to next RTK, so i'm using the old imports)

@StefanBRas , Thanks a lot , your code provides the last piece of our puzzle.

@Shrugsy that snippet seems worth adding to "Customizing Queries"

:rocket:

Was this page helpful?
0 / 5 - 0 ratings