目前没有直接的方法可用,尽管有解决方法,如果您不介意提取整个项目以响应突变,也是如此。
由于您想要更新一个项目的可变数量的属性,您可以编写一个突变(服务器端),其中包含属性列表和值列表。
// 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'],
}));
关于在客户端的突变乐观的更新,你可以从properties
和values
即更新的属性和工艺的返回值他们的价值。
据我所知,包括有条件地在胖查询中的字段目前不被Relay支持。与中继容器不同,我们不能在胖查询中使用变量。看到current efforts by Relay contributors,我相信,在不久的将来,突变API将会变得更加强大和精简。
你可以发布你的突变代码,也是你得到的错误? – Chris