【问题标题】:No Variables in Relay generated mutation query中继生成的变异查询中没有变量
【发布时间】:2016-09-05 23:00:18
【问题描述】:

我对 Relay 很陌生,所以也许这是一个非常愚蠢的错误。

我正在尝试做一个简单的突变,将defect 添加到photo

这是我的 Relay.Mutation 对象:

AddDefectMutation.js

export default class AddDefectMutation extends Relay.Mutation {

    getMutation() {
        return Relay.QL`mutation { addDefect }`;
    }

    getVariables() {
        return{
            photoId: this.props.photoId,
            title: this.props.title
        }
    }

    getFatQuery() {
        return Relay.QL`
            fragment on AddDefectMutationPayload {
                updatedPhoto {
                    issues
                }
            }
        `
    }

    getConfigs() {
        return [{
            type : 'FIELDS_CHANGE',
            fieldIDs : {
                updatedPhoto : this.props.photoId
            }
        }]
    }
}

这是 GraphQl 架构的一部分

const AddDefectMutation = mutationWithClientMutationId({
    name: 'AddDefectMutation',
    description: 'Add a new defect and return all the defects.',
    inputFields: {
        photoId: {
            description: 'Photo of this defect',
            type: new GraphQLNonNull(GraphQLString)
        },
        title: {
            description: 'A short description of the defect',
            type: GraphQLString
        }
    },
    outputFields: {
        updatedPhoto: {
            type: PhotoGraphQLType,
            resolve: ({localIdIssue}) => driver.getIssuePhoto(localIdIssue)
        }
    },
    mutateAndGetPayload: ({photoId, title}) =>
        driver.addIssue(photoId, title).then(localIdIssue => ({localIdIssue}))
})

const MutationGraphQLType = new GraphQLObjectType({
    name: 'Mutation',
    fields: () => ({
        addDefect: AddDefectMutation
    })
})

我的问题是当我拨打这个电话时:

Relay.Store.commitUpdate(new AddDefectMutation(
        {photoId: this.props.pictureId, title: this.props.title}), {
        onSuccess: ()=> console.log("Mutation Success !"),
        onFailure: transaction => console.error(transaction.getError() || new Error('Mutation failed.'))
    })

Relay 生成好的突变查询没有问题,但它没有将给定的变量放在构造函数中。


编辑:这里是中继生成的突变片段

mutation AddDefect($input_0:AddDefectMutationInput!) {
    addDefect(input:$input_0) {
        ...F4,
        clientMutationId
    }
}

问题是$input_0 是一个空对象

【问题讨论】:

    标签: reactjs graphql relayjs relay


    【解决方案1】:

    变量title 未正确传递给突变构造函数。在您的 Relay.Store.commitUpdate 函数调用中,将 {photoId: this.props.pictureId, this.props.title}) 更改为

    {photoId: this.props.pictureId, title: this.props.title})
    

    【讨论】:

    • 是的,抱歉,这是复制错误。
    • 不,它不在原始代码中。我已经简化了这个以避免不相关的代码
    • 无法测试自己。您可以尝试将照片本身作为道具传递给突变构造函数而不是照片ID吗?您需要对 getConfigsgetVariables 函数和 commitUpdate 函数调用进行细微更改。
    • 好的,我会发布结果
    • 代码对我来说看起来不错。您可能希望在调用 commitUpdate 之前放置一个调试日志,以检查是否将有效的 this.props.pictureIdthis.props.title 值传递给突变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2018-08-05
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2016-06-12
    • 2012-10-25
    相关资源
    最近更新 更多