08 / 22

What’s the difference between query and mutation endpoints in RTK Query?

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.

Query Endpoints
  1. 1

    Defined using builder.query().

  2. 2

    Used for fetching and retrieving data from an API.

  3. 3

    Results are automatically cached by RTK Query for performance.

  4. 4

    Support features like polling, background refetching, and cache sharing.

  5. 5

    Example hooks: useGetUsersQuery, useFetchPostsQuery.

Mutation Endpoints
  1. 1

    Defined using builder.mutation().

  2. 2

    Used for creating, updating, or deleting data on the server.

  3. 3

    Do not automatically cache results (but can trigger cache invalidation).

  4. 4

    Useful for actions like submitting forms or updating user profiles.

  5. 5

    Example hooks: useAddUserMutation, useDeletePostMutation.

Example: Query vs Mutation Endpoints

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.

Difficulty: 6/10
Topics: query vs mutation semantics, cache invalidation, side effects in RTK Query

Scenario Questions

0-2 years experience
  1. 1

    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?

  2. 2

    How would you fix it if a user says 'I clicked save but nothing changed' after calling a query to update their settings?

2-5 years experience
  1. 1

    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?

  2. 2

    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?

5-8 years experience
  1. 1

    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?

  2. 2

    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?

8+ years experience
  1. 1

    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?

  2. 2

    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?

Follow-up Questions

  • What happens if you use a query to update data instead of a mutation?
  • How do you handle cache invalidation when a mutation affects multiple queries?
  • When would you use an optimistic update versus a refetch after a mutation?