我有这个解析器
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/