Difference Between Query and Mutation Endpoints in RTK Query
RTK Query distinguishes between two types of endpoints — queries and mutations — based on the kind of operation being performed. Queries are used to fetch and read data, while mutations are used to modify data on the server.
Defined using builder.query().
Used for fetching and retrieving data from an API.
Results are automatically cached by RTK Query for performance.
Support features like polling, background refetching, and cache sharing.
Example hooks: useGetUsersQuery, useFetchPostsQuery.
Defined using builder.mutation().
Used for creating, updating, or deleting data on the server.
Do not automatically cache results (but can trigger cache invalidation).
Useful for actions like submitting forms or updating user profiles.
Example hooks: useAddUserMutation, useDeletePostMutation.
In short, queries are for reading data and mutations are for writing data. RTK Query handles both efficiently while providing automatic caching and refetching where appropriate.
You're trying to update a user's profile and you used a query hook instead of a mutation — what happens when you call the function, and why does the UI not reflect the change?
How would you fix it if a user says 'I clicked save but nothing changed' after calling a query to update their settings?
A feature where users edit a list of items breaks intermittently — sometimes the list updates, sometimes it doesn’t. You notice the team is using a query to POST changes. What’s likely going wrong, and how do you debug it?
After a mutation, a related list of items doesn’t refresh even though you expected it to. What are three possible reasons, and how would you fix each?
You’re optimizing a dashboard with 10+ queries that depend on a single user profile update. How do you design the mutation to efficiently invalidate only the relevant queries without over-fetching?
A mutation triggers a cascade of refetches across multiple components, causing performance lag. How would you redesign this using optimistic updates or cache manipulation to improve UX and reduce network load?
You’re migrating a legacy app from Redux Toolkit to RTK Query, and hundreds of components use manual dispatches to update state after API calls. How do you systematically replace those with mutations while ensuring data consistency and minimizing regressions?
Your team has inconsistent patterns: some use mutations for reads, others use queries for writes. How do you enforce correct usage at scale across multiple squads, and what tooling or architecture changes would you propose to prevent future misuse?