Skip to content

Conversation

@ScriptType
Copy link
Contributor

Summary

This PR introduces configuration-based query invalidation support, initially implemented for angular-query. This allows developers to define cache invalidation rules directly in [orval.config.ts] rather than manually calling queryClient.invalidateQueries() in every component.
This is based of #2676 but without the optimistic update for now.

Usage

In your [orval.config.ts], you can now specify mutationInvalidates within the query options:

export default defineConfig({
  petstore: {
    // ...
    output: {
      override: {
        query: {
         useInvalidate: true,
          mutationInvalidates: {
             // When createPets succeeds, invalidate 'listPets' query
            createPets: ['listPets'],
             // When deletePet succeeds, invalidate multiple queries
            deletePet: ['listPets', 'showPetById'],
          },
        },
      },
    },
  },
});

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.

To support this in other clients (e.g., react-query, vue-query, svelte-query), generators need to:

  1. Read the Config: Access mutationInvalidates from the query options object (available in [NormalizedQueryOptions]).

    const invalidatesConfig = query.mutationInvalidates?.[operationName];
  2. Check for Invalidations: If an entry exists for the current mutation operation, it will be a string array of query operation names.

  3. Generate Invalidation Code:

    • Inside the mutation's onSuccess callback (or equivalent lifecycle hook), generate code to invalidate the target queries.
    • Use the existing query key generators (e.g., getListPetsQueryKey()) to ensure type safety and correctness.

    Abstract Example:

    // In generated mutation hook
    onSuccess: (data, variables, context) => {
      // Loop through config targets
      queryClient.invalidateQueries({ queryKey: getListPetsQueryKey() });
      
      // Call original onSuccess if provided
      options?.onSuccess?.(...);
    }

Checklist

  • Core config types added
  • Logic implemented for angular-query
  • Verification sample added
  • Tests passed

@ScriptType ScriptType marked this pull request as draft December 23, 2025 15:31
@ScriptType ScriptType marked this pull request as ready for review December 25, 2025 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant