【问题标题】:Variable ID! remains undefined in graphql mutation可变标识!在 graphql 突变中仍未定义
【发布时间】:2017-10-12 15:50:43
【问题描述】:

我有突变 -

`export const setTimeLineOnUser = gql 
    mutation ($tID: ID!, $uID: ID! ){
        setTimeLineOnUser(userTimeLineId: $tID, timeLineUserId: $uID){
            userTimeLine {
                id
                updatedAt
                posts{
                    id
                    postType
                    updatedAt
                }
            }
            timeLineUser {
                id
                name
                email
            }
        }
    };`

还有一个看起来像这样的函数 -

`this.props.createTimeLine().then((response) => {
            console.log(response.data.createTimeLine.id);
            this.addTimeLineToUser(response.data.createTimeLine.id, this.props.userQuery.user.id);
        });`

用户有这样的一对一关系 -

`setTimeLineOnUser(userTimeLineId: ID!, timeLineUserId: ID!): SetTimeLineOnUserPayload`

现在运行时出现此错误 -

错误:GraphQL 错误:“ID!”类型的变量“$tID”预期值但价值未定义。原因:预期的非空值,发现为空。 (第 1 行,第 11 列): 突变($tID:ID!,$uID:ID!){ ^ GraphQL 错误:“ID!”类型的变量“$uID”预期值但价值未定义。原因:预期的非空值,发现为空。 (第 1 行,第 22 列): 突变($tID:ID!,$uID:ID!){ ^ 在新的 ApolloError (apollo.umd.js:1959) 在 apollo.umd.js:2670 在 tryCallOne (core.js:37) 在 core.js:123 在 JSTimers.js:117 在 Object.callTimer (JSTimersExecution.js:95) 在 Object.callImmediatesPass (JSTimersExecution.js:199) 在 Object.callImmediates (JSTimersExecution.js:214) 在 MessageQueue.js:228 在 MessageQueue.__guard (MessageQueue.js:218)

虽然console.log(this.props.userQuery.user.id); 正在返回有效的ID! 我什至试图使突变将 id 分配给状态,以便它们可能不是 null 但仍然没有用!!!我做错了什么?

如果您想查看文件,请点击此处 - Gist

【问题讨论】:

    标签: graphql graphql-js apollo react-apollo


    【解决方案1】:

    问题是当您调用addTimelineToUser 时,您提供了一个对象,但您需要两个参数。

        componentDidMount(){
    
            this.props.createTimeLine().then((response) => {
                // this.addTimeLineToUser({ tID: response.data.createTimeLine.id, uID: this.props.userQuery.user.id }) wrong
                this.addTimeLineToUser(response.data.createTimeLine.id, this.props.userQuery.user.id)
            });
        }
    
    
        addTimeLineToUser = (tId, uId) => {
            this.props.setTimeLineOnUser({variables: {tId, uId}})
                .then((response) => {
                    console.log(response);
                }).catch((err) => {
                    console.log(err);
            });
        };

    这可能不起作用,因为我不确定是否在组件内部加载了查询并执行了挂载功能。

    【讨论】:

    • console.log(this.props.userQuery.user.id);在 componentDidMount 实际上不返回 null/undefined 它返回 uid。甚至在函数被触发之前
    • 这个确实有效!已经用不同的组合尝试了 3 天,这确实有效。谢谢
    • 太棒了!很高兴能提供帮助
    猜你喜欢
    • 2019-09-09
    • 2021-04-17
    • 2018-07-27
    • 2015-12-19
    • 2017-09-18
    • 2018-06-07
    • 2018-11-21
    • 2021-11-18
    • 2018-08-04
    相关资源
    最近更新 更多