Custom Resolver function "expected function call to have a scalar result, not a stream"

I’ve created a custom resolver function to query & convert type of some DB collection attributes (e.g. timestamp stored originally as string type (“2021-08-11T10:29:52.000Z”) to GraphQL timestamp data type).

The function (below) has Resolver Field Name ‘TXN_AccountsWithDateTime’ and executes successfully return tXN_Accounts collection with the 4 date converted

  const cluster = context.services.get("db-cluster") ;
  const tXN_Accounts = cluster.db("DB").collection("TXN_Account") ;
  const tXN_AccountsWithDateTime = await tXN_Accounts.aggregate( [
    { $set : { 'transactionDate'                   : { $toDate : '$transactionDate'            } } },
    { $set : { 'transactionEffectiveDate'      : { $toDate : '$transactionEffectiveDate'   } } },
    { $set : { 'transactionObject.dateTime' : { $toDate : '$transactionObject.dateTime' } } },
    { $set : { 'balanceObject.dateTime'      : { $toDate : '$balanceObject.dateTime'     } } }
  ] );
  return tXN_AccountsWithDateTime;

As for Custom Resolver, I’ve defined the custom output type of ‘tXN_AccountsWithDatetime’ which is array of tXN_Account account collection, i.e.

{
  "title": "TXN_AccountsWithDateTime",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "_class": {
        "bsonType": "string"
      },
      "_id": {
        "bsonType": "string"
      },
      "accountId": {
        "bsonType": "object",
      ...

On execute the query in GraphQL Explorer

query getAccountsWithDateTime {
  TXN_AccountsWithDateTime {
      ....
  }
}

returns the following error (though I’ve already defined the return type to be array as mentioned above)

{
  "data": {
    "TXN_AccountsWithDateTime": null
  },
  "errors": [
    {
      "message": "expected function call to have a scalar result, not a stream",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "TXN_AccountsWithDateTime"
      ]
    }
  ]
}

Appreciate any advice.

Hi I think you need to add .toArray(); to your function

exports = async function totalSalesPerMonth(source) {
  
  const cluster = context.services.get("mongodb-atlas");
  const sales = cluster.db("bayanihan").collection("transactions");
  const totalSalesPerMonth = await sales.aggregate([
  { "$group": {
    "_id": { 
        "$dateToString": { "format": "%Y-%m-%d", "date": "$createdAt" } 
    },
    "average": { "$sum": "$fees.totalAmount" },
    "month": { "$first": { "$month": "$createdAt" } },
  } }
]).toArray();

return totalSalesPerMonth


}

This is snippet from my code using aggregate functions.