Relay中没有变量生成的变异查询

时间:2016-05-10 19:35:36

标签: reactjs graphql relayjs relay

我对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是一个空对象

1 个答案:

答案 0 :(得分:1)

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

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