自定义定义 Apollo React Mutation 组件中的变量



我的后端处理一个AddLike突变。这需要一个UserId和一个CommentId或PitchId。取决于你想喜欢哪个。我想在整个应用程序中重用我的 LikeButton 组件 这样,我需要在我的 Apollo React Mutation 中定义我的变量,以采用 CommentId 或 PitchID。现在我似乎无法使任何事情起作用。有什么建议吗?:-)

下面是组件的代码。API GrahQL 可以工作。所以那里没有什么可改变的。

import * as React from 'react';
import { Mutation } from 'react-apollo';
import { ADD_LIKE } from '../../GraphQLStatements/GraphQLStatements'; 
interface ILikeButtonProps {
currentUser : number;
type : any;
index: number;
}
const LikeButton: React.SFC<ILikeButtonProps> = (props) => {
let inputType : string = "pitchId"
const DefineType = () => {
if (props.type === "comment") {
inputType = "commentId"
}
else {
inputType = "pitchId"
}
}
return (
<Mutation mutation={ADD_LIKE}>
{(addLike) => (
<i className="far fa-thumbs-up icon like__button"
onClick={e => {
e.preventDefault();
e.stopPropagation();
DefineType()
addLike({ variables : { like : 
{ "userId": props.currentUser,
inputType : props.index}
} 
}).then ((res : any) => {
console.log(res.data.addLike.statusMessage)
});
}}>
</i>
)}
</Mutation>
);
};
export default LikeButton;

现在这是行不通的。提前感谢!

在inputType周围添加方括号有效:

addLike({ variables : { like : 
{ "userId": props.userId,
[inputType]: props.id}
} 

最新更新