我需要动态更改我的突变文档,以便能够在单个突变中创建多个项目。所以我有这个功能createOrderName
它需要一个整数并能够创建正确的突变文档。例如。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,但我需要它更加灵活,以便我可以创建任意数量的项目...
这听起来像是您正在使用的后端的一个不幸的限制。进行批量突变的正确方法是在服务器上有一个突变字段,该字段接受包含要插入的所有项目的列表参数。因此,Apollo 的设计目的并不是支持这种标准的动态查询生成react-apollo
API。这是因为我们坚信使用静态查询比在运行时生成字段要好得多:https://dev-blog.apollodata.com/5-benefits-of-static-graphql-queries-b7fa90b0b69a#.hp710vxe7 https://dev-blog.apollodata.com/5-benefits-of-static-graphql-queries-b7fa90b0b69a#.hp710vxe7
不过,考虑到这种情况,听起来动态生成突变字符串是一个不错的选择。您可以直接使用 Apollo 来完成此操作,而不是通过graphql
霍克。您可以使用withApollo
执行此操作的 HoC:http://dev.apollodata.com/react/higher-order-components.html#withApollo 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 就是为了这个目的——当标准的东西还不够的时候。
这是文档mutate
btw: http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient.mutate http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient.mutate
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)