目前没有直接的方法可用,尽管有一种解决方法,如果您不介意获取整个项目以响应突变。
当你想更新一个项目的可变数量的属性时,你可以编写一个突变(服务器端),它接受一个属性列表和一个值列表。
// 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 目前不支持有条件地在胖查询中包含字段。与 Relay 容器不同,我们不能在 fat 查询中使用变量。看到current efforts by Relay contributors,我相信,在不久的将来,变异 API 会变得更加强大,而且会更加精简。