2016-05-17 27 views
1

是否可以重用突变?在继电器中重复使用突变

假设存在突变UpdateItemMutation,我将道具传递给 但有时我只想更新特定属性。

即我希望能够做到:

UpdateItemMutation({prop1: 'Data', prop2: 'Data2'}) 
UpdateItemMutation({prop1: 'Data'}) 
UpdateItemMutation({prop2: 'Data2'}) 

不幸的是,我的乐观的配置抱怨,因为我传递一个未定义它。另外,由于我必须考虑突变更新所有事情,所以这会重新查询整个事情,理想情况下更新'prop1'只会为'prop1'做一个fatQuery。

使用@include如果prop定义在这里似乎不起作用。

+0

你可以发布你的突变代码,也是你得到的错误? – Chris

回答

1

目前没有直接的方法可用,尽管有解决方法,如果您不介意提取整个项目以响应突变,也是如此。

由于您想要更新一个项目的可变数量的属性,您可以编写一个突变(服务器端),其中包含属性列表和值列表。

// Using `graphql-relay` library's helper function `mutationWithClientMutationId`. 
const UpdateItemMutation = mutationWithClientMutationId({ 
    name: 'UpdateItem', 
    inputFields: { 
    itemId: { type: new GraphQLNonNull(GraphQLID) } 
    properties: { type: new GraphQLList(GraphQLString) }, 
    values: { type: new GraphQLList(GraphQLString) }, 
    }, 
    outputFields: { 
    item: { 
     type: ItemType, 
     resolve: (item) => item, 
    }, 
    }, 
    mutateAndGetPayload: ({itemId, properties, values, ...args}) => { 
    // Iterate over the properties and values. 
    // Update the item with itemId. 

    return item; 
    }, 
}); 

客户端突变调用象下面这样:

Relay.Store.commitUpdate(new UpdateItemMutation({ 
    item: this.props.item, 
    properties: ['prop1', 'prop2'], 
    values: ['value1', 'value2'], 
})); 

关于在客户端的突变乐观的更新,你可以从propertiesvalues即更新的属性和工艺的返回值他们的价值。

据我所知,包括有条件地在胖查询中的字段目前不被Relay支持。与中继容器不同,我们不能在胖查询中使用变量。看到current efforts by Relay contributors,我相信,在不久的将来,突变API将会变得更加强大和精简。