|
| 1 | +# Transform useSuspenseQuery to useSuspenseQueries |
| 2 | + |
| 3 | +This example demonstrates how to use Codegen to automatically convert multiple `useSuspenseQuery` calls to a single `useSuspenseQueries` call in React codebases. The migration script makes this process simple by handling all the tedious manual updates automatically. |
| 4 | + |
| 5 | +## How the Migration Script Works |
| 6 | + |
| 7 | +The script automates the entire migration process in a few key steps: |
| 8 | + |
| 9 | +1. **File Detection** |
| 10 | + ```python |
| 11 | + for file in codebase.files: |
| 12 | + if "useSuspenseQuery" not in file.source: |
| 13 | + continue |
| 14 | + ``` |
| 15 | + - Automatically identifies files using `useSuspenseQuery` |
| 16 | + - Skips irrelevant files to avoid unnecessary processing |
| 17 | + - Uses Codegen's intelligent code analysis engine |
| 18 | + |
| 19 | +2. **Import Management** |
| 20 | + ```python |
| 21 | + import_str = "import { useQuery, useSuspenseQueries } from '@tanstack/react-query'" |
| 22 | + file.add_import_from_import_string(import_str) |
| 23 | + ``` |
| 24 | + - Uses Codegen's import analysis to add required imports |
| 25 | + - Preserves existing import structure |
| 26 | + - Handles import deduplication automatically |
| 27 | + |
| 28 | +3. **Query Transformation** |
| 29 | + ```python |
| 30 | + # Convert multiple queries to single useSuspenseQueries call |
| 31 | + new_query = f"const [{', '.join(results)}] = useSuspenseQueries({{queries: [{', '.join(queries)}]}})" |
| 32 | + ``` |
| 33 | + - Collects multiple `useSuspenseQuery` calls |
| 34 | + - Combines them into a single `useSuspenseQueries` call |
| 35 | + - Maintains variable naming and query configurations |
| 36 | + |
| 37 | +## Why This Makes Migration Easy |
| 38 | + |
| 39 | +1. **Zero Manual Updates** |
| 40 | + - Codegen SDK handles all the file searching and updating |
| 41 | + - No tedious copy-paste work |
| 42 | + |
| 43 | +2. **Consistent Changes** |
| 44 | + - Ensures all transformations follow the same patterns |
| 45 | + - Maintains code style consistency |
| 46 | + |
| 47 | +3. **Safe Transformations** |
| 48 | + - Validates changes before applying them |
| 49 | + - Easy to review and revert if needed |
| 50 | + |
| 51 | +## Common Migration Patterns |
| 52 | + |
| 53 | +### Multiple Query Calls |
| 54 | +```typescript |
| 55 | +// Before |
| 56 | +const result1 = useSuspenseQuery(queryConfig1) |
| 57 | +const result2 = useSuspenseQuery(queryConfig2) |
| 58 | +const result3 = useSuspenseQuery(queryConfig3) |
| 59 | + |
| 60 | +// Automatically converted to: |
| 61 | +const [result1, result2, result3] = useSuspenseQueries({ |
| 62 | + queries: [queryConfig1, queryConfig2, queryConfig3] |
| 63 | +}) |
| 64 | +``` |
| 65 | + |
| 66 | +## Key Benefits to Note |
| 67 | + |
| 68 | +1. **Reduced Re-renders** |
| 69 | + - Single query call instead of multiple separate calls |
| 70 | + - Better React performance |
| 71 | + |
| 72 | +2. **Improved Code Readability** |
| 73 | + - Cleaner, more consolidated query logic |
| 74 | + - Easier to maintain and understand |
| 75 | + |
| 76 | +3. **Network Optimization** |
| 77 | + - Batched query requests |
| 78 | + - Better resource utilization |
| 79 | + |
| 80 | +## Running the Migration |
| 81 | + |
| 82 | +```bash |
| 83 | +# Install Codegen |
| 84 | +pip install codegen |
| 85 | + |
| 86 | +# Run the migration |
| 87 | +python run.py |
| 88 | +``` |
| 89 | + |
| 90 | +The script will: |
| 91 | +1. Initialize the codebase |
| 92 | +2. Find files containing `useSuspenseQuery` |
| 93 | +3. Apply the transformations |
| 94 | +4. Print detailed progress information |
| 95 | + |
| 96 | +## Learn More |
| 97 | + |
| 98 | +- [React Query Documentation](https://tanstack.com/query/latest) |
| 99 | +- [useSuspenseQueries API](https://tanstack.com/query/latest/docs/react/reference/useSuspenseQueries) |
| 100 | +- [Codegen Documentation](https://docs.codegen.com) |
| 101 | + |
| 102 | +## Contributing |
| 103 | + |
| 104 | +Feel free to submit issues and enhancement requests! |
0 commit comments