Query Structure
Queries are defined using thegql template tag and exported as constants:
Common Query Patterns
Single Item Query
Fetch a single item by ID:packages/codegen/query/user.ts
List Query with Edges
Fetch a list of items using the connection pattern:packages/codegen/query/organization.ts
Paginated Query
Fetch paginated data with cursor-based pagination:packages/codegen/query/organization.ts
Filtered Query
Fetch items with filtering and ordering:packages/codegen/query/organization.ts
Nested Query
Fetch related data in a single query:packages/codegen/query/organization.ts
Specific Field Query
Fetch only specific fields for performance:packages/codegen/query/organization.ts
Using Queries in Components
Here’s a real example from the codebase:apps/console/src/components/pages/protected/profile/user-settings/profile-name-form.tsx
Query Hook Options
Generated query hooks support React Query options:Common Options
| Option | Description |
|---|---|
enabled | Whether the query should run |
refetchOnMount | Refetch when component mounts |
refetchOnWindowFocus | Refetch when window regains focus |
staleTime | How long data stays fresh (ms) |
cacheTime | How long to keep unused data (ms) |
retry | Number of retry attempts on failure |
Conditional Queries
Only fetch data when conditions are met:Query States
Handle different query states:Cache Management
Queries are automatically cached by React Query using query keys:Manual Refetch
Invalidate Cache
Best Practices
Request Only Needed Fields
Only query the fields you need to reduce payload size and improve performance.
Use Fragments for Reuse
Define GraphQL fragments for commonly requested field sets.
Enable Conditionally
Use the
enabled option to prevent queries from running until dependencies are ready.Handle Loading States
Always handle loading, error, and empty states in your components.
Leverage Caching
Use React Query’s caching to avoid unnecessary network requests.