Google Antigravity Directory

The #1 directory for Google Antigravity prompts, rules, workflows & MCP servers. Optimized for Gemini 3 agentic development.

Resources

PromptsMCP ServersAntigravity RulesGEMINI.md GuideBest Practices

Company

Submit PromptAntigravityAI.directory

Popular Prompts

Next.js 14 App RouterReact TypeScriptTypeScript AdvancedFastAPI GuideDocker Best Practices

Legal

Privacy PolicyTerms of ServiceContact Us
Featured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver ToolsFeatured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver Tools

© 2026 Antigravity AI Directory. All rights reserved.

The #1 directory for Google Antigravity IDE

This website is not affiliated with, endorsed by, or associated with Google LLC. "Google" and "Gemini" are trademarks of Google LLC.

Antigravity AI Directory
PromptsMCPBest PracticesUse CasesLearn
Home
Prompts
TanStack Query Advanced Patterns

TanStack Query Advanced Patterns

Production-ready data fetching with TanStack Query including caching, optimistic updates, and infinite queries

TanStack QueryReactData FetchingCaching
by Antigravity Team
⭐0Stars
.antigravity
# TanStack Query Advanced Patterns for Google Antigravity

Master data fetching with TanStack Query using Google Antigravity's Gemini 3 engine. This guide covers caching strategies, optimistic updates, infinite queries, and prefetching patterns.

## Query Client Configuration

```typescript
// lib/query-client.ts
import { QueryClient, QueryCache, MutationCache } from '@tanstack/react-query';
import { toast } from 'sonner';

export function createQueryClient() {
  return new QueryClient({
    defaultOptions: {
      queries: {
        staleTime: 60 * 1000, // 1 minute
        gcTime: 5 * 60 * 1000, // 5 minutes (formerly cacheTime)
        retry: (failureCount, error: any) => {
          if (error?.status === 404 || error?.status === 401) {
            return false;
          }
          return failureCount < 3;
        },
        refetchOnWindowFocus: false,
      },
      mutations: {
        retry: false,
      },
    },
    queryCache: new QueryCache({
      onError: (error: any, query) => {
        if (query.state.data !== undefined) {
          toast.error(`Background refresh failed: ${error.message}`);
        }
      },
    }),
    mutationCache: new MutationCache({
      onError: (error: any) => {
        toast.error(`Operation failed: ${error.message}`);
      },
    }),
  });
}
```

## Query Keys Factory

```typescript
// lib/query-keys.ts
export const queryKeys = {
  products: {
    all: ['products'] as const,
    lists: () => [...queryKeys.products.all, 'list'] as const,
    list: (filters: ProductFilters) =>
      [...queryKeys.products.lists(), filters] as const,
    details: () => [...queryKeys.products.all, 'detail'] as const,
    detail: (id: string) => [...queryKeys.products.details(), id] as const,
  },
  users: {
    all: ['users'] as const,
    current: () => [...queryKeys.users.all, 'current'] as const,
    profile: (id: string) => [...queryKeys.users.all, 'profile', id] as const,
  },
  orders: {
    all: ['orders'] as const,
    lists: () => [...queryKeys.orders.all, 'list'] as const,
    list: (filters: OrderFilters) =>
      [...queryKeys.orders.lists(), filters] as const,
    detail: (id: string) => [...queryKeys.orders.all, 'detail', id] as const,
  },
} as const;
```

## Custom Query Hooks

```typescript
// hooks/use-products.ts
import {
  useQuery,
  useMutation,
  useQueryClient,
  useInfiniteQuery,
} from '@tanstack/react-query';
import { queryKeys } from '@/lib/query-keys';
import { api } from '@/lib/api';

interface ProductFilters {
  categoryId?: string;
  search?: string;
  sortBy?: string;
}

export function useProducts(filters: ProductFilters = {}) {
  return useQuery({
    queryKey: queryKeys.products.list(filters),
    queryFn: () => api.products.list(filters),
    placeholderData: (previousData) => previousData,
  });
}

export function useInfiniteProducts(filters: ProductFilters = {}) {
  return useInfiniteQuery({
    queryKey: [...queryKeys.products.list(filters), 'infinite'],
    queryFn: ({ pageParam = 1 }) =>
      api.products.list({ ...filters, page: pageParam }),
    getNextPageParam: (lastPage) =>
      lastPage.pagination.page < lastPage.pagination.pages
        ? lastPage.pagination.page + 1
        : undefined,
    initialPageParam: 1,
  });
}

export function useProduct(id: string) {
  const queryClient = useQueryClient();

  return useQuery({
    queryKey: queryKeys.products.detail(id),
    queryFn: () => api.products.getById(id),
    initialData: () => {
      // Try to find product in list cache
      const lists = queryClient.getQueriesData<ProductListResponse>({
        queryKey: queryKeys.products.lists(),
      });

      for (const [, data] of lists) {
        const product = data?.items.find((p) => p.id === id);
        if (product) return product;
      }
      return undefined;
    },
  });
}

export function useCreateProduct() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: api.products.create,
    onSuccess: (newProduct) => {
      queryClient.setQueryData(
        queryKeys.products.detail(newProduct.id),
        newProduct
      );
      queryClient.invalidateQueries({ queryKey: queryKeys.products.lists() });
    },
  });
}

export function useUpdateProduct() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<Product> }) =>
      api.products.update(id, data),
    onMutate: async ({ id, data }) => {
      await queryClient.cancelQueries({ queryKey: queryKeys.products.detail(id) });

      const previousProduct = queryClient.getQueryData<Product>(
        queryKeys.products.detail(id)
      );

      queryClient.setQueryData<Product>(
        queryKeys.products.detail(id),
        (old) => (old ? { ...old, ...data } : old)
      );

      return { previousProduct };
    },
    onError: (err, { id }, context) => {
      if (context?.previousProduct) {
        queryClient.setQueryData(
          queryKeys.products.detail(id),
          context.previousProduct
        );
      }
    },
    onSettled: (_, __, { id }) => {
      queryClient.invalidateQueries({ queryKey: queryKeys.products.detail(id) });
      queryClient.invalidateQueries({ queryKey: queryKeys.products.lists() });
    },
  });
}
```

## Optimistic Updates Pattern

```typescript
// hooks/use-cart.ts
import { useMutation, useQueryClient } from '@tanstack/react-query';

interface CartItem {
  id: string;
  productId: string;
  quantity: number;
  product: Product;
}

export function useAddToCart() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (item: { productId: string; quantity: number }) =>
      api.cart.addItem(item),
    onMutate: async (newItem) => {
      await queryClient.cancelQueries({ queryKey: ['cart'] });
      const previousCart = queryClient.getQueryData<CartItem[]>(['cart']);

      const product = queryClient.getQueryData<Product>(
        queryKeys.products.detail(newItem.productId)
      );

      if (product) {
        const optimisticItem: CartItem = {
          id: `temp-${Date.now()}``,
          productId: newItem.productId,
          quantity: newItem.quantity,
          product,
        };

        queryClient.setQueryData<CartItem[]>(['cart'], (old = []) => {
          const existing = old.find((i) => i.productId === newItem.productId);
          if (existing) {
            return old.map((i) =>
              i.productId === newItem.productId
                ? { ...i, quantity: i.quantity + newItem.quantity }
                : i
            );
          }
          return [...old, optimisticItem];
        });
      }

      return { previousCart };
    },
    onError: (err, newItem, context) => {
      if (context?.previousCart) {
        queryClient.setQueryData(['cart'], context.previousCart);
      }
    },
    onSettled: () => {
      queryClient.invalidateQueries({ queryKey: ['cart'] });
    },
  });
}
```

## Prefetching Strategies

```typescript
// components/ProductList.tsx
import { useQueryClient } from '@tanstack/react-query';
import { queryKeys } from '@/lib/query-keys';
import Link from 'next/link';

export function ProductList({ products }: { products: Product[] }) {
  const queryClient = useQueryClient();

  const prefetchProduct = (id: string) => {
    queryClient.prefetchQuery({
      queryKey: queryKeys.products.detail(id),
      queryFn: () => api.products.getById(id),
      staleTime: 60 * 1000,
    });
  };

  return (
    <div className="grid gap-4">
      {products.map((product) => (
        <Link
          key={product.id}
          href={`/products/${product.id}`}
          onMouseEnter={() => prefetchProduct(product.id)}
          onFocus={() => prefetchProduct(product.id)}
        >
          <ProductCard product={product} />
        </Link>
      ))}
    </div>
  );
}

// Server-side prefetching in Next.js
// app/products/[id]/page.tsx
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';

export default async function ProductPage({ params }: { params: { id: string } }) {
  const queryClient = new QueryClient();

  await queryClient.prefetchQuery({
    queryKey: queryKeys.products.detail(params.id),
    queryFn: () => api.products.getById(params.id),
  });

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <ProductDetails id={params.id} />
    </HydrationBoundary>
  );
}
```

## Best Practices

Google Antigravity's Gemini 3 engine recommends these TanStack Query patterns: Use query key factories for type-safe and consistent cache keys. Implement optimistic updates for immediate user feedback. Leverage placeholderData for smoother transitions between cached and fresh data. Prefetch data on hover for instant navigation. Configure proper staleTime and gcTime for optimal caching.

When to Use This Prompt

This TanStack Query prompt is ideal for developers working on:

  • TanStack Query applications requiring modern best practices and optimal performance
  • Projects that need production-ready TanStack Query code with proper error handling
  • Teams looking to standardize their tanstack query development workflow
  • Developers wanting to learn industry-standard TanStack Query patterns and techniques

By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their tanstack query implementations.

How to Use

  1. Copy the prompt - Click the copy button above to copy the entire prompt to your clipboard
  2. Paste into your AI assistant - Use with Claude, ChatGPT, Cursor, or any AI coding tool
  3. Customize as needed - Adjust the prompt based on your specific requirements
  4. Review the output - Always review generated code for security and correctness
💡 Pro Tip: For best results, provide context about your project structure and any specific constraints or preferences you have.

Best Practices

  • ✓ Always review generated code for security vulnerabilities before deploying
  • ✓ Test the TanStack Query code in a development environment first
  • ✓ Customize the prompt output to match your project's coding standards
  • ✓ Keep your AI assistant's context window in mind for complex requirements
  • ✓ Version control your prompts alongside your code for reproducibility

Frequently Asked Questions

Can I use this TanStack Query prompt commercially?

Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.

Which AI assistants work best with this prompt?

This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.

How do I customize this prompt for my specific needs?

You can modify the prompt by adding specific requirements, constraints, or preferences. For TanStack Query projects, consider mentioning your framework version, coding style, and any specific libraries you're using.

Related Prompts

💬 Comments

Loading comments...