React-Apollo的动态突变文件 [英] Dynamic mutation document for react-apollo
问题描述
我需要动态更改我的变异文档,以便能够在一个变异中创建多个项目.因此,我有此函数createOrderName
,该函数采用整数,并能够创建正确的突变文档.例如. createOrderName(2)
获取
I need to change my mutation document dynamically to be able to create multiple items in a single mutation. So I have this function createOrderName
that takes an integer and be able to create the correct mutation document. Eg. createOrderName(2)
gets
mutation createOrderMut($input0: AddToOrderMenuItemConnectionInput!, $input1: AddToOrderMenuItemConnectionInput!) {
input0: addToOrderMenuItemConnection (input:$input0) {
changedOrderMenuItem {
id
}
}
input1: addToOrderMenuItemConnection (input:$input1) {
changedOrderMenuItem {
id
}
}
}
我的容器如下.
const CartContainer = compose(
graphql(createOrderName(2), {
props: ({ mutate }) => ({
addToOrderMenuItem: (menus, orderId) => mutate({
variables: createOrdersInput(menus, orderId)
})
})
})
)(CartView)
现在如何将整数值传递给此突变,以使其创建正确的突变文档?目前已修复为2,但我需要使其更具灵活性,以便可以创建任意数量的项目...
Now how can I pass an integer value to this mutation in order for it to create the correct mutation document? Currently it's fix to 2, but I need it to be more flexible so I can create any number of items...
推荐答案
这听起来像是对您正在使用的后端的不幸限制.进行批处理突变的正确方法是在服务器上具有一个突变字段,该字段接受带有要插入的所有项目的list参数.因此,Apollo并非设计为使用标准react-apollo
API支持这种动态查询生成.那是因为我们坚信使用静态查询比在运行时生成字段要好得多:
This sounds like an unfortunate limitation of the backend you're working with. The right way to do a batch mutation would be to have a single mutation field on the server that accepts a list argument with all of the items you want to insert. Thus, Apollo isn't designed to support such dynamic query generation with the standard react-apollo
API. That's because we strongly believe that working with static queries is much better than generating fields at runtime: https://dev-blog.apollodata.com/5-benefits-of-static-graphql-queries-b7fa90b0b69a#.hp710vxe7
尽管如此,听起来像动态生成突变字符串是一个不错的选择.您可以通过直接使用Apollo而不是通过graphql
HoC来做到这一点.您可以使用withApollo
HoC执行此操作: http://dev .apollodata.com/react/higher-order-components.html#withApollo
Given the situation, though, sounds like generation the mutation string dynamically is a good option. You can do this by using Apollo directly instead of through the graphql
HoC. You can use the withApollo
HoC to do this: http://dev.apollodata.com/react/higher-order-components.html#withApollo
import { withApollo } from 'react-apollo';
const MyComponent = ({ client }) => {
function mutate() {
const dynamicMutationString = // generate mutation string here
client.mutate({
mutation: gql`${dynamicMutationString}`,
variables: { ... },
}).then(...);
}
return <button onClick={mutate}>Click here</button>;
}
const MyComponentWithApollo = withApollo(MyComponent);
我们仅出于标准目的而构建了这个额外的API.
We built this extra API for just this purpose - when the standard stuff is not enough.
以下是mutate
btw的文档: http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient.mutate
Here are the docs for mutate
btw: http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient.mutate
这篇关于React-Apollo的动态突变文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!