Crate leptos_query_rs

Crate leptos_query_rs 

Source
Expand description

Leptos Query - A React Query inspired data fetching library for Leptos

This library provides a powerful and flexible way to manage server state in Leptos applications, with features like caching, background updates, optimistic updates, and more.

§Features

  • Declarative Data Fetching: Write queries as simple functions
  • Automatic Caching: Built-in cache with configurable stale times
  • Background Updates: Keep data fresh with background refetching
  • Optimistic Updates: Update UI immediately with rollback on error
  • 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

§Quick Start

use leptos::prelude::*;
use leptos_query_rs::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
struct User {
    id: u32,
    name: String,
    email: String,
}

async fn fetch_user(id: u32) -> Result<User, QueryError> {
    // Your async function here
    Ok(User {
        id,
        name: "John Doe".to_string(),
        email: "john@example.com".to_string(),
    })
}

#[component]
fn UserProfile(user_id: u32) -> impl IntoView {
    let user_query = use_query(
        move || QueryKey::new(&["user", &user_id.to_string()]),
        move || async move { fetch_user(user_id).await },
        QueryOptions::default(),
    );

    view! {
        <div>
            {move || {
                if let Some(user) = user_query.data.get() {
                    format!("User: {}", user.name)
                } else if user_query.is_loading.get() {
                    "Loading...".to_string()
                } else {
                    "No user found".to_string()
                }
            }}
        </div>
    }
}

Re-exports§

pub use client::QueryClient;
pub use client::SerializedData;
pub use client::CacheEntry;
pub use query::use_query;
pub use query::QueryOptions;
pub use query::QueryResult;
pub use mutation::use_mutation;
pub use mutation::MutationOptions;
pub use mutation::MutationResult;
pub use retry::QueryError;
pub use retry::RetryConfig;
pub use retry::execute_with_retry;
pub use types::QueryKey;
pub use types::QueryStatus;
pub use types::QueryMeta;
pub use types::QueryKeyPattern;
pub use types::QueryObserverId;
pub use infinite::use_infinite_query;
pub use infinite::InfiniteQueryOptions;
pub use infinite::InfiniteQueryResult;
pub use infinite::Page;
pub use infinite::PageInfo;
pub use persistence::PersistenceManager;
pub use persistence::PersistenceConfig;
pub use persistence::StorageBackend;
pub use persistence::LocalStorageBackend;
pub use persistence::IndexedDBBackend;
pub use optimistic::OptimisticManager;
pub use optimistic::OptimisticConfig;
pub use optimistic::OptimisticUpdate;
pub use optimistic::OptimisticStats;
pub use devtools::DevToolsManager;
pub use devtools::DevToolsConfig;
pub use devtools::DevToolsServer;
pub use devtools::QueryMetrics;
pub use devtools::NetworkRequest;
pub use devtools::CacheOperation;
pub use devtools::DevToolsEvent;
pub use devtools::DevToolsExport;
pub use sync::SyncManager;
pub use sync::ConflictResolutionStrategy;
pub use sync::NetworkStatus;
pub use sync::SyncResult;

Modules§

client
Query Client
dedup
Request deduplication
devtools
infinite
mutation
Mutation Hooks and Options
optimistic
persistence
query
Query Hooks and Options
retry
Retry logic and error handling for queries
sync
Synchronization module for leptos-sync-core integration
types
Core types and data structures for the query system

Structs§

QueryClientProviderProps
Props for the QueryClientProvider component.

Functions§

QueryClientProvider
Provide the QueryClient context to the app