【问题标题】:How to reduce 3 graphQL Apollo requests in only 1 request如何仅在 1 个请求中减少 3 个 graphQL Apollo 请求
【发布时间】:2021-05-07 00:48:22
【问题描述】:

我有一个 JavaScript 博客,我正在使用 Apollo GraphQL 来保存我的数据。我打算得到三类六篇文章。因此,我请求获取一个类别的所有帖子,并使用类别的 id 重复此请求三遍。

所以,我打算只将我的三个请求转换为一个,但我不能。

我的代码:

{
  allPosts(where: {category: "id"}, first: 6) {
    edges {
      node {
        title
        image
      }
    }
  }
}

【问题讨论】:

  • 使用别名,阅读文档

标签: graphql apollo prismic.io


【解决方案1】:

您可以使用aliases 来满足您的要求(也许您需要调整fragment):

{
  firstCategory: allPosts(where: {category: "firstId"}, first: 6) {
    ...fields
  }

  secondCategory: allPosts(where: {category: "secondId"}, first: 6) {
    ...fields
  }
}

fragment fields on Post {
  edges {
    node {
      title
      image
    }
  }
}

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 2019-07-26
    • 2018-11-02
    • 2021-05-07
    • 1970-01-01
    • 2018-10-21
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多