-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Shallow merge meta with defaults #9785
-
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:
- BC break is reasonably edge case to just introduce the merging behavior
- Be able to pass a function to
metawhich receives the "default meta", e.g.{ meta: (meta: QueryMeta | undefined) => ({ ...meta, querySpecific: 'Foo' }) } - Introduce
containeroption that has merging behavior by default and otherwise behaves likemeta(maybe deprecate meta)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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 ?
Beta Was this translation helpful? Give feedback.