javascript - 如何在 Apollo GraphQL 解析器上的一个函数中解析 2 个不同的方法?

标签 javascript ecmascript-6 graphql apollo

我有这个解析器

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 中的这些方法/模型之一。

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

最佳答案

GraphQL 由需求(查询)驱动。

当您需要帐户时,只需查询即可。当您需要帐户和相关硬件时,只需查询两者即可 - 将自动调用其他解析器。这是 graphQL 背后的基本思想。查找任何涵盖 graphQL 中关系的教程。

关于javascript - 如何在 Apollo GraphQL 解析器上的一个函数中解析 2 个不同的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438035/

相关文章:

javascript - 如何通过 JavaScript 从主要世界时钟网站获取时区或日期?

javascript - 在我的例子中如何创建 setTimeout ?

javascript - JavaScript 中的基本/高效原型(prototype)继承?

Javascript:如果我不知道对象的键,如何访问嵌套对象中的值?

javascript - 为什么 ES6 类没有被提升?

javascript - ReactJS onBlur 不工作

javascript - 更喜欢解构——已经存在的变量

amazon-web-services - AWS AppSync 更新架构

node.js - Sequelize 和响应请求 GraphQL

node.js - Nodejs bcrypt 比较无法正常工作