data-fetching
π―Skillfrom qingqishi/shiqingqi.com
Streamlines API data retrieval with intelligent caching, error handling, and flexible request configurations for modern web applications.
Installation
npx skills add https://github.com/qingqishi/shiqingqi.com --skill data-fetchingSkill Details
Data fetching patterns for server/client components using fetch API, TanStack Query, useSuspenseQuery, apiRouteWrapper, and apiRequestWrapper. Use when implementing data loading, API calls, server functions, queries, mutations, API routes, or when the user mentions TanStack Query, useSuspenseQuery, apiRouteWrapper, apiRequestWrapper, tmdb-server-functions, or data fetching.
Overview
# Data Fetching Architecture
Overview
This project uses a structured data fetching pattern that differs between server and client components.
Core Principles
- Server components call server functions directly
- Client components use TanStack Query to fetch from API routes
- API routes wrap server functions using
apiRouteWrapper - Mutations from client components call server functions directly
- TMDB API uses auto-generated server functions from
tmdb-server-functions.ts
Server Components
Server components can directly await server functions.
```tsx
import { discoverMovies } from "@/utils/tmdb-server-functions";
function serverFunction() {
return fetch(URL, headers);
}
function ServerComponent() {
const movies = await discoverMovies({
page: 1,
with_genres: "28",
});
const myResults = await serverFunction();
return
}
```
Use await directly in server components. Call TMDB server functions from @/utils/tmdb-server-functions.
Client Components + API Routes
Client components use TanStack Query to call API routes, which wrap server functions.
Step 1: Create API Route
API routes use apiRouteWrapper to wrap server functions:
```ts
// src/app/api/tmdb/discover-movies/route.ts
import { apiRouteWrapper } from "@/utils/api-route-wrapper";
import { discoverMovies } from "@/utils/tmdb-server-functions";
export const GET = apiRouteWrapper(discoverMovies);
```
For custom server functions:
```ts
// src/app/api/some-api-route/route.ts
import { apiRouteWrapper } from "@/utils/api-route-wrapper";
import { myServerFunction } from "@/some-server-function";
export const GET = apiRouteWrapper(async (params) => {
return myServerFunction(params);
});
```
Step 2: Call from Client Component
Use TanStack Query with apiRequestWrapper:
```tsx
"use client";
import { useSuspenseQuery } from "@tanstack/react-query";
import { apiRequestWrapper } from "@/utils/api-request-wrapper";
function ClientComponent() {
const { data: movies } = useSuspenseQuery({
queryKey: [{ scope: "movies", page: 1, with_genres: "28" }],
queryFn: () =>
apiRequestWrapper("/api/tmdb/movie-list", {
page: 1,
with_genres: "28",
}),
});
const { data: results } = useSuspenseQuery({
queryKey: [{ scope: "someApiRoute" }],
queryFn: () =>
apiRequestWrapper("/api/some-api-route", {
foo: "bar",
}),
});
return
}
```
Mutations from Client Components
For mutations, client components call server functions directly (no API route needed).
```tsx
"use client";
import { useMutation } from "@tanstack/react-query";
import { updateMovie } from "@/server-functions/movies";
function ClientComponent() {
const mutation = useMutation({
mutationFn: updateMovie,
});
const handleUpdate = () => {
mutation.mutate({ id: 1, title: "New Title" });
};
return ;
}
```
TMDB API Integration
TMDB server functions are auto-generated from src/_generated/tmdb-server-functions.ts.
Important: These are auto-generated - DO NOT edit manually. Use pnpm codegen:tmdb to regenerate.
```tsx
// Import TMDB functions
import {
discoverMovies,
getMovieDetails,
searchMovies,
} from "@/utils/tmdb-server-functions";
// Server component usage
async function MovieList() {
const movies = await discoverMovies({
page: 1,
with_genres: "28",
});
return
}
// API route for client components
// src/app/api/tmdb/discover-movies/route.ts
export const GET = apiRouteWrapper(discoverMovies);
```
Pattern Summary
Server Component Data Flow
```
Server Component β Server Function β External API/Database
```
Client Component Data Flow (Queries)
```
Client Component β TanStack Query β API Route β Server Function β External API/Database
```
Client Component Data Flow (Mutations)
```
Client Component β TanStack Query Mutation β Server Function β External API/Database
```
Best Practices
- Server components: Call server functions directly with
await - Client queries: Use TanStack Query + API routes
- Client mutations: Call server functions directly
- TMDB: Use auto-generated functions, never edit manually
- API routes: Use
apiRouteWrapper - Client fetching: Use
apiRequestWrapper
Common Mistakes
β Calling API routes from server components (use server functions directly)
β Using fetch directly in client components (use TanStack Query)
β Editing tmdb-server-functions.ts manually (regenerate with pnpm codegen:tmdb)
β Creating API routes for mutations (call server functions directly)
More from this repository5
Manages package dependencies using pnpm and corepack, automatically handling package manager versions and installations via the packageManager field.
Streamlines React 19 context and compiler patterns, providing optimized syntax for Context.Provider, use() hook, and automatic memoization.
Enables responsive and theme-aware styling using StyleX design tokens, breakpoints, and custom CSS prop for consistent component design.
Generates selective Zod schemas and TypeScript types for TMDB API endpoints, optimizing performance and enabling AI-powered structured outputs.
Enables user-centric end-to-end testing with Playwright using semantic locators and best practices that focus on visible behavior.