可以通过使用options选项对象来传递变量给useMutation()。以下是示例代码:
import { useMutation } from '@apollo/client';
import gql from 'graphql-tag';
const DELETE_POST = gql`
mutation DeletePost($id: ID!) {
deletePost(id: $id) {
id
title
}
}
`;
const Post = ({ id }) => {
const [deletePost, { loading }] = useMutation(DELETE_POST);
const handleDeletePost = () => {
deletePost({ variables: { id } });
};
if (loading) return Loading...
;
return (
);
};
在这个例子中,我们使用了options选项对象来传递变量给useMutation()。在handleDeletePost()函数中,我们向deletePost()函数传递一个包含id变量的variables对象。这个变量将被传递给GraphQL mutation并在查询中使用。