小编Hem*_*har的帖子

如何使用Apollo Client执行批量突变

我尝试将ApolloClient 2.1与新的Mutation Component一起使用。

简单的用例正在工作,但是现在我有了一些更复杂的东西。

我要实现的是查询数据并将它们放在列表中,然后对该列表进行排序(在这里通过react-sortable-hoc),一旦排序,我想为列表中的所有元素更新新位置。

因此,基础是这样的,它适用于简单的查询:

const query = gql`
{
  items( order:{by:"position", direction:"desc"}) {
    id
    name
    position
  }
}`

const ItemView extends Component {
    onSortEnd = ({ oldIndex, newIndex }) => {
       console.log("Sort ended: ", oldIndex, newIndex);
    }

    render() {
     <Query query={query}>
        {({ loading, data, error }) => {
          if (loading) return <p>Loading...</p>;
          if (error) return <p>Error</p>;

          return (
            <ItemList items={data.items} onSortEnd={this.onSortEnd} />
          )
        }}
      </Query>
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我确实在很多方面都在努力进行突变。

我想我需要包装突变成分。但是,我该如何在其中提供GraphQL查询,因为我想通过多次触发类似的查询来进行批量修改,例如

mutation {
  updateItem1: updateItem(id: 457092155, input: {position: 1}) { …
Run Code Online (Sandbox Code Playgroud)

reactjs graphql apollo-client

12
推荐指数
1
解决办法
800
查看次数

标签 统计

apollo-client ×1

graphql ×1

reactjs ×1