Relation Selectors
Relation selector components provide a powerful way to select related entities in your dashboard forms. They support both single and multi-selection modes with built-in search, infinite scroll pagination, and complete TypeScript type safety.
Features
- Real-time Search: Debounced search with customizable filters
- Infinite Scroll: Automatic pagination loading 25 items by default
- Single/Multi Select: Easy toggle between selection modes
- Type Safe: Full TypeScript support with generic types
- Customizable: Pass your own GraphQL queries and field mappings
- Accessible: Built with Radix UI primitives
Components Overview
The relation selector system consists of three main components:
RelationSelector: The abstract base component that handles all core functionalitySingleRelationInput: Convenient wrapper for single entity selectionMultiRelationInput: Convenient wrapper for multiple entity selection
Basic Usage
Single Selection
Multi Selection
Configuration Options
The createRelationSelectorConfig function accepts these options:
Rich Label Display
The label prop allows you to customize how items are displayed in both the dropdown and selected item chips. This enables rich content like images, badges, and multi-line information.
Product Selector with Images and Pricing
Customer Selector with Status Badges
Advanced Examples
Custom Entity with Complex Search
Asset Selector with Type Filtering
Multi-Select with Status Filtering
Registration
Register your relation selector components in your dashboard extension:
Built-in Configurations
The relation selector package includes pre-configured setups for common Vendure entities:
Best Practices
Query Optimization
- Select only needed fields: Include only the fields you actually use to improve performance
- Use fragments: Create reusable fragments for consistent data fetching
- Optimize search filters: Use database indexes for the fields you search on
Performance Tips
- Appropriate page sizes: Balance between fewer requests and faster initial loads
- Debounced search: The default 300ms debounce prevents excessive API calls
- Caching: Queries are automatically cached by TanStack Query
Type Safety
Leverage TypeScript generics for full type safety:
Rich Label Design
When using the label prop for custom rendering:
- Keep it simple: Avoid overly complex layouts that might impact performance
- Handle missing data: Always check for optional fields before rendering
- Maintain accessibility: Use proper semantic HTML and alt text for images
- Consider mobile: Ensure labels work well on smaller screens
Troubleshooting
Common Issues
1. "Cannot query field X on type Query"
Error: Cannot query field "myEntities" on type "Query"
Solution: Ensure your GraphQL query field name matches your schema definition exactly.
2. Empty results despite data existing
3. TypeScript errors with config
Performance Issues
If you experience slow loading:
- Check your GraphQL query: Ensure it's optimized and uses appropriate filters
- Verify database indexes: Make sure searched fields are indexed
- Adjust page size: Try smaller page sizes for faster initial loads
- Optimize buildSearchFilter: Use efficient query patterns