Tanstack query

From HopeArtOrg/hope-re · View interactive version

Tanstack query is an AI agent skill from the GitHub repository HopeArtOrg/hope-re. Create TanStack Svelte Query hooks following Hope:RE patterns with createQuery and createMutation

How to use this skill

Skill file

TanStack Query Pattern

When creating query hooks in Hope:RE, follow these conventions:

Query Hook (reading data)

import type { MyDataType } from "./types";

import { createQuery } from "@tanstack/svelte-query";
import { invoke } from "@tauri-apps/api/core";

export function useMyData() {
  return createQuery(() => ({
    queryKey: ["my-data"],
    queryFn: async () => await invoke<MyDataType>("get_my_data"),
    staleTime: 30 * 1000,
    gcTime: 60 * 1000,
    retry: 1,
  }));
}

Mutation Hook (writing data)

import type { MyInput, MyResult } from "./types";

import { createMutation } from "@tanstack/svelte-query";
import { invoke } from "@tauri-apps/api/core";

export function useMyAction() {
  return createMutation(() => ({
    mutationFn: async (input: MyInput) =>
      await invoke<MyResult>("perform_action", { ...input }),
  }));
}

Using in Stores (composable pattern)

import { useMyData, useMyAction } from "$lib/queries";
import { toast } from "svelte-sonner";

export function useMyFeature() {
  const query = useMyData();
  const mutation = useMyAction();
  const isLoading = $derived(query.isPending || mutation.isPending);
  const data = $derived(query.data ?? null);

  async function handleAction(input: MyInput) {
    try {
      await mutation.mutateAsync(input);
      toast.success("Action completed");
    }
    catch (error) {
      toast.error("Action failed");
      console.error("Action error:", error);
    }
  }

  return {
    get data() { return data; },
    get isLoading() { return isLoading; },
    handleAction,
  };
}

Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.