[Refactor/Chore] Standardize TanStack Query key organization pattern #21559

Closed
opened 2026-02-21 20:13:12 -05:00 by yindo · 0 comments
Owner

Originally created by @lyzno1 on GitHub (Jan 10, 2026).

Description

Standardize TanStack Query key organization pattern. Not an immediate refactor — just a guideline for new code and opportunistic improvements.

Related: #30342

Problem

Most service files (~20+) have inconsistent query key patterns:

  • Inline keys in useQuery calls
  • Scattered variables, not exported
  • Missing as const
  • No hierarchy for batch invalidation

Solution

Follow the hierarchical pattern with all root key:

const NAME_SPACE = 'myModule'

export const myModuleKeys = {
  all: [NAME_SPACE] as const,
  list: (params: Params) => [...myModuleKeys.all, 'list', params] as const,
  detail: (id: string) => [...myModuleKeys.all, 'detail', id] as const,
} as const

Why this works:

  • all root → invalidateQueries({ queryKey: keys.all }) clears entire module
  • [...parent, 'child'] → TanStack Query uses prefix matching
  • as const → TypeScript infers exact tuple types

Reference Files

https://github.com/langgenius/dify/blob/a2e03b811e581674603617a4b13d9d25760c44be/web/app/components/plugins/marketplace/query.ts#L8-L13

https://github.com/langgenius/dify/blob/a2e03b811e581674603617a4b13d9d25760c44be/web/service/use-common.ts#L32-L62

Migration

  1. New code → follow this pattern
  2. Existing code → refactor when modifying for other reasons

Files to improve when touched: use-apps.ts, use-plugins.ts, use-tools.ts, use-workflow.ts, use-triggers.ts, use-log.ts, use-explore.ts, knowledge/use-*.ts

Motivation

Enable batch cache invalidation, improve type safety, reduce copy-paste errors.

Originally created by @lyzno1 on GitHub (Jan 10, 2026). ## Description Standardize TanStack Query key organization pattern. **Not** an immediate refactor — just a guideline for new code and opportunistic improvements. Related: #30342 ### Problem Most service files (~20+) have inconsistent query key patterns: - Inline keys in `useQuery` calls - Scattered variables, not exported - Missing `as const` - No hierarchy for batch invalidation ### Solution Follow the hierarchical pattern with `all` root key: ```typescript const NAME_SPACE = 'myModule' export const myModuleKeys = { all: [NAME_SPACE] as const, list: (params: Params) => [...myModuleKeys.all, 'list', params] as const, detail: (id: string) => [...myModuleKeys.all, 'detail', id] as const, } as const ``` **Why this works:** - `all` root → `invalidateQueries({ queryKey: keys.all })` clears entire module - `[...parent, 'child']` → TanStack Query uses prefix matching - `as const` → TypeScript infers exact tuple types ### Reference Files https://github.com/langgenius/dify/blob/a2e03b811e581674603617a4b13d9d25760c44be/web/app/components/plugins/marketplace/query.ts#L8-L13 https://github.com/langgenius/dify/blob/a2e03b811e581674603617a4b13d9d25760c44be/web/service/use-common.ts#L32-L62 ### Migration 1. **New code** → follow this pattern 2. **Existing code** → refactor when modifying for other reasons Files to improve when touched: `use-apps.ts`, `use-plugins.ts`, `use-tools.ts`, `use-workflow.ts`, `use-triggers.ts`, `use-log.ts`, `use-explore.ts`, `knowledge/use-*.ts` ## Motivation Enable batch cache invalidation, improve type safety, reduce copy-paste errors.
yindo added the 💪 enhancement label 2026-02-21 20:13:12 -05:00
yindo closed this issue 2026-02-21 20:13:12 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21559