首页 > 解决方案 > 如何为给定的 graphql 联合类型查询两个 mongoose 集合?

问题描述

我有两个系列:companiespeople. 我也有这个 graphql 架构

type Company {
  id: ID!
  ...
}

type Person {
  id: ID!
  ...
}

union Customer = Company | Person

type Query {
  allCustomers: [Customer!]
}

现在我的解析器看起来像这样:

allCustomers: async () => {
  const [customers, people] = await Promise.all([
    Company.find(),
    Person.find(),
  ])
  return [...customers, ...people]
}

有没有更好的方法来查询数据库?或者这样可以吗?

标签: node.jsmongoosegraphql

解决方案


推荐阅读