Leptos Query
A React Query inspired data fetching library for Leptos applications, providing powerful caching, background updates, and error handling capabilities.
🚀 Now fully compatible with Leptos 0.8!
Features
- Declarative Data Fetching: Write queries as simple async functions
- Automatic Caching: Built-in cache with configurable stale times
- Background Updates: Keep data fresh with background refetching
- Error Handling: Comprehensive error handling with retry logic
- Type Safety: Full type safety with Rust's type system
- WASM Compatible: Works in both native and web environments
- Leptos 0.8 Ready: Full compatibility with the latest Leptos framework
Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.4.0"
= "0.8"
= { = "1.0", = ["derive"] }
Basic Usage
use *;
use *;
use ;
async
Setup
Wrap your app with the QueryClientProvider:
API Reference
Query Hook
Parameters:
key_fn: Function that returns aQueryKeyfor cachingquery_fn: Async function that fetches the dataoptions: Configuration options for the query
Returns:
QueryResult<T>: Object containing data, loading state, and actions
Query Options
let options = default
.with_stale_time
.with_cache_time
.with_refetch_interval;
Query Result
Mutation Hook
Example:
async
let mutation = use_mutation;
// Execute mutation
mutation.mutate.call;
Error Handling
The library provides comprehensive error handling:
Retry Configuration
let retry_config = new
.with_max_delay
.with_fixed_delay
.no_network_retry;
Advanced Features
Query Keys
Query keys are used for caching and invalidation:
// Simple key
from
// Compound key
from
// With parameters
from_parts.unwrap
Cache Invalidation
// Invalidate specific queries
client.remove_query;
// Invalidate by pattern
client.invalidate_queries;
Background Refetching
let options = default
.with_refetch_interval;
Examples
See the examples/ directory for complete working examples:
basic.rs: Basic query usagemutations.rs: Mutation examplescaching.rs: Advanced caching examples
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.