【问题标题】:Custom define variables inside Apollo React Mutation Component在 Apollo React Mutation 组件中自定义变量
【发布时间】:2019-03-17 12:32:30
【问题描述】:

我的后端处理 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;

现在这不起作用。提前致谢!

【问题讨论】:

    标签: reactjs graphql apollo react-apollo graphql-js


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-18
      • 2020-06-21
      • 2023-03-15
      • 2017-08-26
      • 1970-01-01
      • 2018-09-02
      • 2019-04-02
      • 2019-01-26
      • 1970-01-01
      相关资源
      最近更新 更多