I am trying to figure out how to get an optimistic response when I add a task to my todo list.
ADD_TASK mutation
export const ADD_TASK = gql`
mutation addTask($task: AddTaskInput!) {
addTask(input: [$task]) {
task {
id
title
completed
}
}
}
`;
the add Function
const addTask = mutation(ADD_TASK);
........
........
async function add() {
try {
await addTask({
variables: {
task: {
title: text,
completed: false,
user: { username: user?.email },
},
},
optimisticResponse: {
addTask: {
__typename: "mutation",
task: {
__typename: "Task",
id: Math.round(Math.random() * -1000000),
title: text,
completed: false,
user: { username: user?.email },
},
},
},
});
} catch (e: any) {
console.log(e);
}
text = "";
}
I believe my problem lies in how I structured the optimisticResponse. If I get rid of it, everything works as expected, but I am trying to figure out how to get this particular feature of Apollo to work.
I happen to be using svelte with typescript from my repo here, but I imagine it would be the same in react etc.
Any Suggestions?
Thanks
J