0

How can I create a resolver that adds data from another DocumentDB collection to each item within the query response list?

Given the grossly overly simplified following schema:

type Asset {
  id: ID!
  ...
}

type DeviceData {
  id: ID!
  assetID: string
  asset: Asset
  ...
}

Query {
  findDevicesQuery: [DeviceData]
}

For example, findDevicesQuery() => currently returns all devices

I now need to get the associated asset for each device by assetID and then append it to the findDevicesQuery response for each device

ie:

findDevicesQuery => returns

[
  {deviceData1, associatedAssetData1}, 
  {deviceData2, associatedAssetData2}, 
  {deviceData3, associatedAssetData3}
]

I would've done this with a connection in Amplify using DynamoBD, however this project is not using Amplify, it is using DocumentDB.

halfer
  • 19,824
  • 17
  • 99
  • 186
studiobrain
  • 1,135
  • 2
  • 13
  • 35

1 Answers1

0

I took a different approach, so adding it here for anyone else who might stumble here...

Since mongoDB (AWS DocumentDB) is used, an aggregation api is exposed, so I used it:

MongoModel.aggregate([
  {
    $lookup: {
      from: 'assets',
      localField: 'assets_id',
      foreignField: '_id',
      as: 'asset'
    }
  }
])
  .limit(10)
  .then(data => {
    console.log('findDevicesQuery() data', JSON.stringify(data))
    resolve(data)
  })
  .catch(error => reject(new Error(`${ERROR_FINDING_DEVICES}: ${error}`)))```
studiobrain
  • 1,135
  • 2
  • 13
  • 35