Apollo 2.1 中的多个查询/变异 [英] Multiple Queries/Mutation in Apollo 2.1
问题描述
我需要一些帮助来使用 Apollo 2.1 中的新查询和变异组件,尤其是多个查询和变异.
I need some help using the new Query and Mutation component in Apollo 2.1, especially with multiple queries and mutations.
我有以下问题:
- 我有一个依赖于先前 graphql 结果的 graphql 请求,我该如何处理?
- 如何在已有查询的组件中添加两个不同的更改(在我的组件中我需要执行两个不同的操作)?
推荐答案
edit 2019/08/24来自 Apollo docs:
edit 2019/08/24 from the Apollo docs:
Apollo 客户端的新钩子 API 是一种更简单的方式来获取数据你的 React 应用程序没有渲染道具组件的样板和高阶组件 (HOC).我们建议对所有新的东西使用钩子未来的阿波罗代码.
The new hooks API for Apollo Client is a simpler way to fetch data in your React app without the boilerplate of render prop components and higher-order components (HOC). We recommend using hooks for all new Apollo code going forward.
原始答案:你应该嵌套它们.看这个例子:
original answer: You are supposed to nest them. See this example:
const NumbersWithData = () => (
<Query query={QueryOne}>
{({ loading: loadingOne, data: { one } }) => (
<Query query={QueryTwo}>
{({ loading: loadingTwo, data: { two }}) => {
if (loadingOne || loadingTwo) return <span>loading...</span>
return <h3>{one} is less than {two}</h3>
}}
</Query>
)}
</Query>
);
为了帮助保持嵌套可管理性,您可以查看 react-adopt.他们有一个 Apollo ToDo 应用示例,其中组合了一个查询和多个 Mutation.
To help with keeping the nesting manageable, you could check react-adopt. They have an Apollo ToDo App example, where they combine a Query and multiple Mutations.
这篇关于Apollo 2.1 中的多个查询/变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!