Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Shallow merge meta with defaults #9785

Unanswered
backbone87 asked this question in Ideas
Discussion options

When having default options with meta, the meta object is completely replaced when "later" options also have a meta key. This can be quite cumbersome when using meta as a "DI container" , e.g.:

// client.ts
export const FETCH = Symbol('FETCH');
declare module '@tanstack/react-query' {
 interface Register {
 queryMeta: {
 [FETCH]?: typeof globalThis.fetch;
 };
 mutationMeta: {
 [FETCH]?: typeof globalThis.fetch
 };
 }
}
const fetchWithTracing = createFetchWithTracing(globalThis.fetch);
const client = new QueryClient({
 defaultOptions: {
 queries: {
 meta: {
 [FETCH]?: fetchWithTracing,
 }
 },
 mutations: {
 meta: {
 [FETCH]?: fetchWithTracing,
 }
 },
 },
});
// SomeComponent.ts
const fetchWithTracing = useContext(FetchWithTracingContext);
const {} = useQuery({
 meta: {
 [FETCH]: fetchWithTracing, // !!! have to respecify the fetch client, because we also want to pass other meta props
 querySpecific: 'Foo',
 }
})

I think it would be better if meta objects are automatically merged with default/common options. This might be a breaking change so i see 3 options how to do that:

  1. BC break is reasonably edge case to just introduce the merging behavior
  2. Be able to pass a function to meta which receives the "default meta", e.g. { meta: (meta: QueryMeta | undefined) => ({ ...meta, querySpecific: 'Foo' }) }
  3. Introduce container option that has merging behavior by default and otherwise behaves like meta (maybe deprecate meta)
You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Additionally, the type for QueryFunctionContext is

type QueryFunctionContext<...> = [TPageParam] extends [never] ? {
 meta: QueryMeta | undefined;
 // ...
}

That means that even if you specify a custom QueryMeta, meta could always be undefined, so meta.[FETCH] isn't valid without checks or type-casting.

You must be logged in to vote
1 reply
Comment options

yes that’s one of the drawbacks of not having a type parameter on useQuery, but only a global type augmentation. one fix would be to always just fall back to an empty object for meta and then we could make it defined on type level ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /