# Optimistic mutation: temporary duplicate responses

**URL:** <https://community.apollographql.com/t/optimistic-mutation-temporary-duplicate-responses/6662>\
**Category:** Client SDKs\
**Tags:** client, web\
**Created:** [August 30, 2023, 7:24pm UTC](https://community.apollographql.com/t/optimistic-mutation-temporary-duplicate-responses/6662 "2023-08-30T19:24:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![katmh](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/katmh/32/4549_2.png) [@katmh](https://community.apollographql.com/u/katmh)\
**Post date:** [August 30, 2023, 7:24pm UTC](https://community.apollographql.com/t/optimistic-mutation-temporary-duplicate-responses/6662/1 "2023-08-30T19:24:02Z")

</div>

Hi. I’m trying to add optimistic response to a mutation `sendMessage`. In terms of business logic, this mutation adds a new message to a chat channel. I’m encountering a bug where if I send messages “1” “2” “3” in rapid succession on an artificially throttled network, I’ll see:

[Screen recording optimistic mutation duplicate messages.mov - Google Drive](https://drive.google.com/file/d/1KolgX62xGZx9HolYm_BIKd6CWPYaREoX/view?usp=sharing) (screen recording)

I’ve consulted these docs:

- `https://www.apollographql.com/docs/react/performance/optimistic-ui`
- `https://www.apollographql.com/docs/react/caching/cache-interaction`
- `https://www.apollographql.com/docs/react/data/mutations#updating-the-cache-directly`

My code is something like:

```auto
const content = /* get content from input field */
/* clear input field so user can immediately start typing another message */
const temporaryId = `temp-id-${Date.now()}`;
const optimisticResponse = {
  sendMessage: {
    __typename: "Message" as const,
    id: temporaryId,
    content,
    ...
  }
}
await sendMessage({
  variables: {...},
  optimisticResponse,
  update: (cache, { data: { sendMessage: newMessage } }) => {
    ... /* I've tried various approaches here */
  }
});

```

The UI renders messages that are nested in the results of another query, `channelById`, which returns a `Channel` type.

My understanding is:

- Since I’ve specified `optimisticResponse`, the Apollo client will immediately add the optimistic message to the cache once the mutation is triggered.
- The `messages` list deep inside the `channelById` result may not necessarily be automatically updated, so I would have to manually define how to update that part of the cache by writing an `update` function. In practice, it seems that `messages` does update automatically, and the `update` function isn’t needed?

I’ve tried passing several different `update` functions into the mutation (including no function) and they all lead to this behavior.

Does anyone know how to fix this?

---

<div class="post-metadata">

**Author:** ![katmh](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/katmh/32/4549_2.png) [@katmh](https://community.apollographql.com/u/katmh)\
**Post date:** [September 5, 2023, 3:12pm UTC](https://community.apollographql.com/t/optimistic-mutation-temporary-duplicate-responses/6662/2 "2023-09-05T15:12:18Z")

</div>

Bump, still would appreciate help with this! Happy to provide more details/context if useful.

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:48pm UTC](https://community.apollographql.com/t/optimistic-mutation-temporary-duplicate-responses/6662/3 "2025-02-06T20:48:22Z")

</div>


