-
-
Notifications
You must be signed in to change notification settings - Fork 540
Feat: config based invalidation #2679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: config based invalidation #2679
Conversation
9320178 to
8c4ca53
Compare
|
can you update the /docs too for this new configuration? |
|
Sure, are there any other regards? Like stuff which you think could change? Or do you like the way? |
|
Let's get some others to weigh in. |
|
I think this is great! |
soartec-lab
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ScriptType
It's a great idea, and I'm in favor of moving forward with it. However, I have one suggestion for improvement:
As written in the example, invalidating listPets depends on updating createPets, but the optional keys implicitly state the dependency.
Rather than assigning meaning to keys, make the keys explicitly descriptive and the values explicit.
Also, there may be multiple operations that can be disabled.
Taking this into consideration, how about doing it like this?
For example:
mutationInvalidates: [
{
invalidates: ['listPets', 'showPetById'],
dependsOn: ['createPets', 'deletePet']
},
{
invalidates: ['listPets'],
dependsOn: ['createPets']
},
]8c4ca53 to
68d47d1
Compare
|
@soartec-lab i agree, its a "bit" more complex to do it for the generation, but if we imagine when we have like 20 queries its much easier to maintain in the config. |
Previously, getHasSignal() only enabled signal support for GET and POST verbs. This caused DELETE, PUT, and PATCH mutations to generate code that referenced an undefined 'options' parameter. - Remove verb restriction from getHasSignal() - Remove unused getIsBodyVerb and Verbs imports - Update call sites to not pass verb parameter
Add ability to specify mutation variable-to-query param mappings:
- string: broad invalidation (no params)
- { query, params: string[] }: same-name shorthand
- { query, params: Record<string, string> }: explicit mapping
This enables targeted cache invalidation based on mutation variables.
Add code generation for targeted cache invalidation:
- normalizeTarget(): converts string to { query } format
- serializeTarget(): deduplicates by query+params combination
- generateVariableRef(): handles nested variables (data.petId)
- generateParamArgs(): generates query key function arguments
- generateInvalidateCall(): builds invalidateQueries() calls
Generated onSuccess now passes mutation variables to query key
functions for precise cache invalidation.
Expand petstore.yaml with mutation operations:
- DELETE /pets/{petId}: deletePet
- PUT /pets/{petId}: updatePet
- PATCH /pets/{petId}: patchPet
Update orval.config.ts:
- Enable signal: true for all verbs
- Configure deletePet, updatePet, patchPet to invalidate both
listPets (broad) and showPetById with petId param (targeted)
Generated files include: - deletePet, updatePet, patchPet mutations with signal support - onSuccess handlers with targeted invalidation: - getListPetsQueryKey() for broad list invalidation - getShowPetByIdQueryKey(variables.petId) for specific pet - New PatchPetBody model type - Updated MSW handlers for new endpoints
Add UI controls for mutation operations: - Update Pet: calls updatePet mutation - Patch Tag: calls patchPet mutation - Delete Pet: calls deletePet mutation Each mutation triggers automatic cache invalidation via mutationInvalidates, demonstrating the feature in action.
Test coverage for mutationInvalidates feature: - getCreatePetsMutationOptions: onSuccess, mutationFn, list invalidation - getDeletePetMutationOptions: dual invalidation (list + specific pet) - getUpdatePetMutationOptions: dual invalidation - getPatchPetMutationOptions: dual invalidation - Params invalidation: verifies only matching petId is invalidated - User callback chaining: confirms custom onSuccess is called All 15 tests passing.
Wrap function references in arrow functions to satisfy unicorn/no-array-callback-reference rule.
b5e6d28 to
ad11ea9
Compare
|
ok i rebased your branch and its all working now |
|
Build is all passing now! |
soartec-lab
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
Summary
This PR introduces configuration-based query invalidation support, initially implemented for
angular-query. This allows developers to define cache invalidation rules directly inorval.config.tsrather than manually callingqueryClient.invalidateQueries()in every component.This is based on #2676 but without the optimistic update for now.
Usage
In your
orval.config.ts, you can now specifymutationInvalidateswithin thequeryoptions:Invalidation Target Formats
'listPets'getListPetsQueryKey(){ query: 'showPetById', params: ['petId'] }getShowPetByIdQueryKey(variables.petId){ query: 'showPetById', params: { petId: 'id' } }getShowPetByIdQueryKey(variables.id)Generated Code Example
For
deletePetwith the config above, orval generates:Implementation Guide for Other Generators
The core types and normalization logic have been added to
@orval/core, making this feature available for all generators to implement.Types
Steps to Support in Other Clients
To support this in other clients (e.g.,
react-query,vue-query,svelte-query), generators need to:Read the Config: Access
mutationInvalidatesfrom thequeryoptions object (available inNormalizedQueryOptions).Filter Rules for Current Operation:
Normalize and Deduplicate Targets:
Generate Invalidation Code:
onSuccesscallback, generate code to invalidate the target queries.getListPetsQueryKey()) to ensure type safety.variables.<paramName>from the mutation.Checklist
InvalidateTarget,MutationInvalidatesRule)angular-query