Skip to main content

MobxQueryClient

An enhanced version of TanStack's Query QueryClient.
Adds specialized configurations for library entities like MobxQuery or MobxMutation.

Reference to source code

API Signature

import { QueryClient } from "@tanstack/query-core";

class MobxQueryClient extends QueryClient {
constructor(config?: MobxQueryClientConfig);
}

Configuration

When creating an instance, you can provide:

import { DefaultOptions } from '@tanstack/query-core';

interface MobxQueryClientConfig {
defaultOptions?: DefaultOptions & {
queries: MobxQueryFeatures;
mutations: MobxMutatonFeatures;
};
hooks?: MobxQueryClientHooks;
}

Key methods and properties

queryFeatures

Features configurations exclusively for MobxQuery/MobxInfiniteQuery

mutationFeatures

Features configurations exclusively for MobxMutation

hooks

Entity lifecycle events. Available hooks:

HookDescription
onQueryInitTriggered when a MobxQuery is created
onInfiniteQueryInitTriggered when a MobxInfiniteQuery is created
onMutationInitTriggered when a MobxMutation is created
onQueryDestroyTriggered when a MobxQuery is destroyed
onInfiniteQueryDestroyTriggered when a MobxInfiniteQuery is destroyed
onMutationDestroyTriggered when a MobxMutation is destroyed

Inheritance

MobxQueryClient inherits all methods and properties from QueryClient, including:

Usage Example

import { MobxQueryClient } from 'mobx-tanstack-query';

// Create a client with custom hooks
const client = new MobxQueryClient({
hooks: {
onQueryInit: (query) => {
console.log('[Init] Query:', query.queryKey);
},
onMutationDestroy: (mutation) => {
console.log('[Destroy] Mutation:', mutation.options.mutationKey);
}
},
defaultOptions: {
queries: {
enableOnDemand: true,
},
mutations: {
invalidateByKey: true,
}
},
});

// Use standard QueryClient methods
const data = client.getQueryData(['todos']);

When to Use?

Use MobxQueryClient if you need:

  • Customization of query/mutation lifecycle
  • Tracking entity initialization/destruction events
  • Advanced configuration for MobX-powered queries and mutations.