facebook / facebook/relay

Relay 10: difficulty with optimistic response and @raw_response_type

Open
#3,129 11 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Hi, we have an issue with an optimistic response when using the new Relay 10. This is our mutation:

```graphql
mutation SidebarLayoutMarkAsSeenMutation($leadID: ID!) @raw_response_type {
updateLead(input: { id: $leadID, wasSeen: true }) {
... on Lead {
wasSeen
}
}
}
```

And here is how we do the optimistic response:

```js
markAsSeen({
variables: { leadID },
optimisticResponse: {
updateLead: {
__typename: 'Lead',
// __isNode: 'Lead', <<< should this be here or not?
id: leadID,
wasSeen: true,
},
},
});
```

This works fine with Relay 9, however, Relay 10 generates different Flow (or in our case TS) types which forces us to write also `__isNode` into the optimistic response. That's however also not okay because Relay complains with the following error:

```text
Warning: Expected `optimisticResponse` to match structure of server response for mutation `SidebarLayoutMarkAsSeenMutation`, please remove all fields of
{
"updateLead": {
"__isNode": {}
}
}
```

Here are the generated types for Flow:

```js
export type SidebarLayoutMarkAsSeenMutationRawResponse = {|
+updateLead: ?({|
+__typename: "Lead",
+__isNode: "Lead",
+id: string,
+wasSeen: ?boolean,
|} | {|
+__typename: string,
+__isNode: string,
+id: string,
|})
|};
```

Or for TypeScript which looks fairly similar so I don't think that's the problem:

```typescript
export type SidebarLayoutMarkAsSeenMutationRawResponse = {
readonly updateLead: ({
readonly __typename: "Lead";
readonly __isNode: "Lead";
readonly id: string;
readonly wasSeen: boolean | null;
} | {
readonly __typename: string;
readonly __isNode: string;
readonly id: string;
}) | null;
};
```

My question is: how should we handle this situation? I am not sure what is the right strategy here and what does `__isNode` exactly mean in this case. 🤔

Thank you very much for having a look!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.