如何查询给定graphql联合类型的两个猫鼬集合?

时间:2019-11-17 22:37:02

标签: node.js mongoose graphql

我有两个集合: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]
}

是否有更好的方法来查询数据库?还是可以吗?

0 个答案:

没有答案