我正在尝试创建一个自定义的 Apollo 突变挂钩,我需要在其中访问挂钩外部的变量:
const useCustomMutation = () => {
return useMutation(
GRAPHQL_QUERY,
{
update: (store, response) => {
store.writeQuery({
query: A_DIFFERENT_QUERY,
data: externalVariable // <= This is were I need access (simplified code).
});
},
}
);
}
在另一个文件中,我会像这样调用钩子:
const someVariable = 1234;
const [mutationFunction, {data, loading, error}] = useCustomMutation();
mutationFunction({ variables: { queryVariable: someVariable }}); // this is the variable I need to access above
如何在无需将参数传递给钩子本身的情况下访问自定义钩子内的 queryVariable
/someVariable
(externalVariable
所在的位置)?
// I'm trying to avoid this:
// I want to access mutationFunction variables, not pass a variable to the hook.
const someVariable = 1234;
const [mutationFunction] = useCustomMutation(someVariable);