reactjs - Apollo GraphQL React - 如何点击查询?

标签 reactjs graphql apollostack

在 Apollo React 文档中 http://dev.apollodata.com/react/queries.html#basics有显示组件时自动获取的示例,但我想在单击按钮时运行查询。我看到一个在单击按钮时“重新”获取查询的示例,但我不希望它最初进行查询。我看到有一种方法可以调用突变,但是如何调用查询呢?

最佳答案

您可以通过使用 withApollo 高阶组件将引用传递给 Apollo Client 来实现此目的,如下所示:https://www.apollographql.com/docs/react/api/react-apollo.html#withApollo

然后,您可以对传入的对象调用client.query,如下所示:

class MyComponent extends React.Component {
  runQuery() {
    this.props.client.query({
      query: gql`...`,
      variables: { ... },
    });
  }

  render() { ... }
}

withApollo(MyComponent);

出于好奇,对点击事件运行查询的目标是什么?也许有更好的方法来实现根本目标。

关于reactjs - Apollo GraphQL React - 如何点击查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39814853/

相关文章:

reactjs - 将 apollo-client 与 NextJS getStaticPaths 和 getStaticProps (SSG) 结合使用 - 构建时出错 - ECONNREFUSED

graphql - Apollo GitHunt-React : updateCommentsQuery?

javascript - Next.js 不加载 jsx

javascript - ReactJS中的滑动动画

reactjs - 草稿 js 错误,在内容 block 末尾选择 if 装饰器

javascript - 等待另一个不断变化的 Action 的最佳方法

javascript - 解决Graphql的异步函数无法返回通过具有rdf数据的Stardog服务器查询的数据

graphql - 如何在GraphQL中为递归数据结构建模

postgresql - Apollo /GraphQL : Field Type to Use for Timestamp?

node.js - Apollo Stack 是否像 Relay 的 Node 接口(interface)一样支持全局对象识别?