RTK Query
Base Client Configuration
Base Query Setup
// src/services/baseQuery.ts
import { fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { RootState } from '@/app/store';
export const baseQuery = fetchBaseQuery({
baseUrl: process.env.NEXT_PUBLIC_API_URL, // e.g., https://api.decentraland.org
prepareHeaders: (headers, { getState }) => {
const state = getState() as RootState;
// Add authentication token
const token = state.user.session?.authToken;
if (token) {
headers.set('authorization', `Bearer ${token}`);
}
// Add chain ID for web3 context
const chainId = state.user.chainId;
if (chainId) {
headers.set('x-chain-id', String(chainId));
}
// Standard headers
headers.set('accept', 'application/json');
headers.set('content-type', 'application/json');
return headers;
},
credentials: 'omit', // or 'include' if backend requires cookies
});Client Instance
Tag Conventions
Tag Naming
Resource Type
Query Tags
Mutation Invalidates
Tag Examples
Creating Endpoints
Query Endpoint (Read)
Mutation Endpoint (Write)
Optimistic Updates
Advanced Query Options
Polling
Skip Query
Lazy Query
Transform Response
Custom Serialization
Error Handling
Custom Error Handling
Cache Management
Manual Cache Updates
Invalidate Cache
Reset Client State
Prefetch Data
Best Practices
1. Use Descriptive Endpoint Names
2. Provide Comprehensive Tags
3. Handle Loading and Error States
4. Use Type Guards
Next Steps
Last updated