Commit 3539f82
authored
fix(query): namespace non-GET query keys with the HTTP verb (orval-labs#3312)
* fix(query): namespace non-GET query keys with the HTTP verb
When a non-GET operation is routed to a Query hook (today only via
operation-level `query.useQuery: true`, and the upcoming fix for orval-labs#2376),
its generated cache key was path-only — e.g. both `GET /pets` and
`POST /pets` produced keys starting with `'/pets'`. TanStack Query then
mixed their cached data and
`invalidateQueries({ queryKey: ['/pets'] })` matched both, which is a
silent runtime bug (no type error, no snapshot diff).
Insert the uppercased HTTP verb as a leading segment when:
- `verb !== GET`, AND
- `useOperationIdAsQueryKey` is not enabled (operation IDs are already
unique across verb + path, so the prefix would be redundant).
Existing GET keys and operation-id-based keys are unchanged.
This unblocks orval-labs#2376 by making it safe for the eventual fix to globally
route non-GET operations to Query hooks without cache key collisions.
BREAKING CHANGE: Users that already opt non-GET operations into Query
hooks via `override.operations.<id>.query.useQuery: true` will see their
cache keys gain a leading verb segment (e.g. `['/pets', body]` →
`['POST', '/pets', body]`). Update any `invalidateQueries` /
`getQueryData` calls that filter by these keys.
* fix(query): keep broad invalidation working for verb-prefixed cache keys
Address Copilot review on orval-labs#3312: when a non-GET operation is routed to
a Query hook and a `mutationInvalidates` rule targets it without
providing the required path params, the broad-invalidation fallback
emitted a predicate / partial key that no longer matched the cache
key (because the cache key now starts with the verb segment).
Mirror `getQueryKeyVerbPrefix`'s behavior in `mutation-generator.ts`:
- Default mode: predicate becomes
`query.queryKey[0] === 'DELETE' && query.queryKey[1].startsWith('/pets/')`.
- Split-key mode: partial key becomes `['DELETE', 'pets']`.
Verb prefix is suppressed for GET targets (existing behavior preserved)
and when `useOperationIdAsQueryKey` is enabled (operation IDs are
already verb+path unique, so `getQueryKeyVerbPrefix` does not insert a
prefix and neither should the invalidation logic).
Also extends the `getQueryKeyVerbPrefix` unit tests to cover HEAD per
Copilot feedback, so all `Verbs` enum members are exercised.
Two new snapshot configs document the fixed behavior:
- `invalidatesNonGetQueryTarget` — default mode.
- `invalidatesNonGetQueryTargetSplitKey` — split-key mode.
* refactor(query): reuse getQueryKeyVerbPrefix in mutation-generator
Address CodeRabbit nitpick on orval-labs#3312: the verb prefix logic in
`createGenerateInvalidateCall` duplicated the rules in
`getQueryKeyVerbPrefix`. Importing the helper directly removes that
duplication so the two sites stay in sync if the prefix rules ever
change.
`info.method` is widened by the spec walker to HTTP_METHODS (a superset
of `Verbs` that also includes `options`/`trace`), but the helper only
branches on `Verbs.GET`, so casting is safe — any non-GET method is
treated identically.1 parent f62674c commit 3539f82
47 files changed
Lines changed: 3052 additions & 8 deletions
File tree
- packages/query/src
- tests
- __snapshots__/react-query
- invalidates-non-get-query-target-split-key
- model
- invalidates-non-get-query-target
- model
- invalidates-query-conflict
- configs
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
| |||
108 | 113 | | |
109 | 114 | | |
110 | 115 | | |
111 | | - | |
| 116 | + | |
112 | 117 | | |
113 | 118 | | |
114 | 119 | | |
| |||
121 | 126 | | |
122 | 127 | | |
123 | 128 | | |
124 | | - | |
| 129 | + | |
125 | 130 | | |
126 | 131 | | |
127 | 132 | | |
| |||
204 | 209 | | |
205 | 210 | | |
206 | 211 | | |
| 212 | + | |
207 | 213 | | |
208 | 214 | | |
209 | 215 | | |
| |||
228 | 234 | | |
229 | 235 | | |
230 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
231 | 251 | | |
232 | | - | |
| 252 | + | |
| 253 | + | |
233 | 254 | | |
234 | 255 | | |
235 | 256 | | |
236 | 257 | | |
237 | 258 | | |
238 | 259 | | |
239 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
240 | 264 | | |
241 | 265 | | |
242 | | - | |
243 | | - | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
244 | 273 | | |
245 | 274 | | |
246 | 275 | | |
| |||
422 | 451 | | |
423 | 452 | | |
424 | 453 | | |
| 454 | + | |
425 | 455 | | |
426 | 456 | | |
427 | 457 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
4 | 8 | | |
5 | 9 | | |
6 | 10 | | |
| |||
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
77 | 103 | | |
78 | 104 | | |
79 | 105 | | |
| |||
887 | 913 | | |
888 | 914 | | |
889 | 915 | | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
890 | 921 | | |
891 | 922 | | |
892 | 923 | | |
| |||
896 | 927 | | |
897 | 928 | | |
898 | 929 | | |
| 930 | + | |
899 | 931 | | |
900 | 932 | | |
901 | 933 | | |
| |||
0 commit comments