Complete the code to specify the optimistic response for a mutation.
mutation UpdateUserName($id: ID!, $name: String!) {
updateUser(id: $id, name: $name) {
id
name
}
}
const optimisticResponse = {
updateUser: {
__typename: 'User',
id: [1],
name: 'New Name'
}
};The optimistic response needs a temporary id as a string to match the expected type.
Complete the code to update the cache after a mutation with optimistic UI.
cache.modify({
id: cache.identify({ __typename: 'User', id: [1] }),
fields: {
name() {
return 'Updated Name';
}
}
});The cache.modify needs the id used in the optimistic response, which is 'temp-id'.
Fix the error in the optimistic response by completing the missing __typename.
const optimisticResponse = {
updateUser: {
[1]: 'User',
id: 'temp-id',
name: 'Optimistic Name'
}
};The __typename field is required for Apollo Client to identify the object type in the cache.
Fill both blanks to write an optimistic response with correct __typename and id.
const optimisticResponse = {
updateUser: {
[1]: 'User',
id: [2],
name: 'Optimistic Update'
}
};The optimistic response must include __typename and a temporary id string.
Fill all three blanks to write a cache update function using optimistic UI with correct id, field, and value.
cache.modify({
id: cache.identify({ __typename: [1], id: [2] }),
fields: {
[3]() {
return 'Optimistic Value';
}
}
});The cache.modify needs the __typename 'User', the temporary id 'temp-id', and the field name 'name' to update.