【问题标题】:How to resolve 2 different methods in one function on Apollo GraphQL resolver?如何在 Apollo GraphQL 解析器的一个函数中解析 2 种不同的方法?
【发布时间】:2019-04-25 13:19:46
【问题描述】:

我有这个解析器

SLAccount: (_, { imsUserId, imsToken }, context) =>
  new Promise((resolve, reject) => {
    addAuth(context, imsUserId, imsToken);
    context.model
      .getUserAccount(getEndpoint(imsUserId, imsToken))
      .then(resolve)
      .catch(reject);
  }),

但我需要在其中包含另一种方法/模型:

  getHardware(endpoint) {
    return this.connector
      .get(
        `${endpoint}/...`,
      )
      .catch(res => this.handleError(res));
  }

所以我需要这样的东西:

SLAccount: (_, { imsUserId, imsToken }, context) =>
  new Promise((resolve, reject) => {
    addAuth(context, imsUserId, imsToken);
    context.model
      .getUserAccount(getEndpoint(imsUserId, imsToken))
      .then(resolve)
      .catch(reject);

    context.model
      .getHardware(getEndpoint(imsUserId, imsToken))
      .then(resolve)
      .catch(reject);
  }),

问题是我不需要同时调用它们,有时我只需要 SLAccount 中的这些方法/模型之一。

那么处理这个问题的最佳方法是什么?

【问题讨论】:

    标签: javascript ecmascript-6 graphql apollo


    【解决方案1】:

    GraphQL 由需求(查询)驱动。

    当您需要 account 时,只需查询即可。当您需要 account 和相关的 hardware 时,只需查询两者 - 将自动调用额外的解析器。这是 graphQL 背后的基本思想。寻找任何涉及 graphQL 关系的教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-07
      • 2021-01-25
      • 2016-12-12
      • 2020-09-11
      • 2020-06-27
      • 2020-07-22
      • 2019-04-14
      • 2019-07-15
      相关资源
      最近更新 更多