a7eb8d2
Thanks @lukemorales! - FixcontextQueries
not working with possibly undefined values
9b5401c
Thanks @lukemorales! - ImprovemergeQueryKeys
type inference and improve type-safety for dynamic query keys
- #70
1a9f74d
Thanks @lukemorales! - Fix type inference allowing extra keys
- #67
e6b8389
Thanks @lukemorales! - Loosen types forTypedUseQueryOptions
and allow inference of dynamic query options generation
- #65
1cf53a1
Thanks @lukemorales! - Refactor codebase and expose newTypedUseQueryOptions
type
- #51
142024d
Thanks @s-bondarenko! - Implementedcreate-mutation-keys
function for creating of mutation keys
- #51
142024d
Thanks @s-bondarenko! - Added possibility to merge mutation keys with query keys
-
#47
7013e19
Thanks @lukemorales! - Add support for longer query keys in theQueryOptions
pattern -
#34
abb3596
Thanks @wconnorwalsh! - Replace internal types with official@tanstack/query-core
types
- #46
0518ac4
Thanks @lukemorales! - Fix conflicting types
- #32
f74ec8e
Thanks @lukemorales! - Fix published types
d8bdb58
Thanks @lukemorales! - Fix wrong published types for contextual queries
-
#20
ba47907
Thanks @lukemorales! - ## Generate query options and add support for nested keysNew in
@lukemorales/query-key-factory
is support for nested keys and generation of query options, adopting the query options overload as first class citizen, in preparation for React Query v5 roadmap.const people = createQueryKeys("people", { person: (id: number) => ({ queryKey: [id], queryFn: () => api.getPerson({ params: { id } }), contextQueries: { ships: { queryKey: null, queryFn: () => api.getShipsByPerson({ params: { personId: id }, }), }, film: (filmId: string) => ({ queryKey: [filmId], queryFn: () => api.getFilm({ params: { id: filmId }, }), }), }, }), });
Each entry outputs an object that can be used in the query options overload in React Query:
console.log(people.person("person_01")); // => output: // { // queryKey: ['people', 'person', 'person_01'], // queryFn: () => api.getPerson({ params: { id: 'person_01' } }), // _ctx: { ...queries declared inside "contextQueries" } // }
Then you can easily just use the object in
useQuery
or spread it and add more query options to that observer:export const Person = ({ id }) => { const personQuery = useQuery(people.person(id)); return { /* render person data */ }; }; export const Ships = ({ personId }) => { const shipsQuery = useQuery({ ...people.person(personId)._ctx.ships, enabled: !!personId, }); return { /* render ships data */ }; }; export const Film = ({ id, personId }) => { const filmQuery = useQuery(people.person(personId)._ctx.film(id)); return { /* render film data */ }; };
All query key values should now be an array. Only the first level keys (those not dynamically generated) can still be declared as
null
, but if you want to pass a value, you will need to make it an array.export const todosKeys = createQueryKeys('todos', { mine: null, - all: 'all-todos', + all: ['all-todos'], - list: (filters: TodoFilters) => ({ filters }), + list: (filters: TodoFilters) => [{ filters }], - todo: (todoId: string) => todoId, + todo: (todoId: string) => [todoId], });
You can still use objects to compose a query key, but now they must be inside an array because plain objects are now used for the declaration of the query options:
export const todosKeys = createQueryKeys('todos', { - list: (filters: TodoFilters) => ({ filters }), + list: (filters: TodoFilters) => ({ + queryKey: [{ filters }], + }), });
With the new API, the output of an entry will always be an object according to what options you've declared in the factory (e.g.: if you returned an array or declared an object with only
queryKey
, your output will be{ queryKey: [...values] }
, if you also declaredqueryFn
it will be added to that object, andcontextQueries
will be available inside_ctx
):export const todosKeys = createQueryKeys('todos', { todo: (todoId: string) => [todoId], list: (filters: TodoFilters) => { queryKey: [{ filters }], queryFn: () => fetchTodosList(filters), }, }); - useQuery(todosKeys.todo(todoId), fetchTodo); + useQuery(todosKeys.todo(todoId).queryKey, fetchTodo); - useQuery(todosKeys.list(filters), fetchTodosList); + useQuery(todosKeys.list(filters).queryKey, todosKeys.list(filters).queryFn); // even better refactor, preparing for React Query v5 + useQuery({ + ...todosKeys.todo(todoId), + queryFn: fetchTodo, + }); + useQuery(todosKeys.list(filters));
The helper types to infer query keys in the created store reflect the new output, to account for all possible use cases:
type TodosKeys = inferQueryKeys<typeof todosKeys>; - type SingleTodoQueryKey = TodosKeys['todo']; + type SingleTodoQueryKey = TodosKeys['todo']['queryKey'];
-
#20
ba47907
Thanks @lukemorales! - ## Remove deprecated methods Sincev0.6.0
, thedefault
key and andtoScope
method have been deprecated from the package.default
key andtoScope
method have been officially removed from the code, which means that if you try to access them, you will either receiveundefined
or your code will throw for trying to invoke a function ontoScope
that does not exist anymore.
- Fix query key breaking devtools because of Proxy
- Introduce internal
_def
key and deprecatedefault
andtoScope
- Create
createQueryKeyStore
to allow declaring all feature keys in one place
- Create
mergeQueryKeys
to create single access point to all query keys
- Add
inferQueryKeys
type to create interface from result ofcreateQueryKeys
- Fix new types not published
- Allow tuples of any size in dynamic keys
- Removes yarn engine restriction
- Make serializable keys less strict