0

I have two collections: companies and people. I also have this graphql schema

type Company {
  id: ID!
  ...
}

type Person {
  id: ID!
  ...
}

union Customer = Company | Person

type Query {
  allCustomers: [Customer!]
}

Right now my resolver looks like this:

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

Is there a beter way to query the database? Or is this ok?

brielov
  • 1,887
  • 20
  • 28
  • 1
    You don't want to load the whole Company & Person collections into memory at once & you're defeating the purpose of graphql's 'over fetching' moto by sending all this data over the wire. Not to mention the stress you're putting on your DB with these 2 queries. It might be a good idea to implement some pagination. – Dan Starns Nov 17 '19 at 22:43
  • The example is simplified, skip and limit are included in the real one. I just wanted to know if it is okay to do it like this or if there is a better way. – brielov Nov 17 '19 at 22:45
  • ok cool, i would do the same here then. Looks good to me – Dan Starns Nov 17 '19 at 22:45

0 Answers0