Home > OS >  parsing HashMap failed, expected Object, but encountered Array
parsing HashMap failed, expected Object, but encountered Array

Time:11-13

For creating an action at hasura I'm using the following node.js code (still at an experimental stage) in glitch.com -

const execute = async (gql_query, variables) => {
  const fetchResponse = await fetch(
    "https://example.com/v1/graphql",
    {
      method: "POST",
      body: JSON.stringify({
        query: gql_query,
        variables: variables
      })
    }
  );
  // console.log('DEBUG: ', fetchResponse);
  const data = await fetchResponse.json();
  console.log("DEBUG: ", data);
  return data;
};


// paste the code from codegen here

const ACTION_INSERT_PAYSLIP_GET_DRIVER_PAYMENT_DATA = `
query getDriverPaymentData ($orders: [Int!]!) {
  company_order (where: {company_order_id: {_in: $orders}}) {
    company_order_details (distinct_on: stage_cost_driver_id) {
      stage_cost_driver_id
      company_user {
        delivery_salary
      }
    }
  }
}`

// Request Handler
app.post('/action_insert_payslip', async (req, res) => {
  // get request input
  const { order_list } = req.body.input
  console.log('Input', order_list)
  const orders = order_list.order_id
  console.log('Item: ', orders)
  
  const { data:driverPaymentData, errors:driverPaymentError} = await execute(ACTION_INSERT_PAYSLIP_GET_DRIVER_PAYMENT_DATA, orders)
  console.log('Driver Payment Data: ', driverPaymentData)

  // run some business logic

  // success
  return res.json({
    // payslip_list: "<value>"
    payslip_list: order_list
  })

});

The query getDriverPaymentData produces an output like the following in hasura api explorer:

{
  "data": {
    "company_order": [
      {
        "company_order_details": [
          {
            "stage_cost_driver_id": 1,
            "company_user": {
              "delivery_salary": 20
            }
          },
          {
            "stage_cost_driver_id": 6,
            "company_user": {
              "delivery_salary": 10
            }
          }
        ]
      },
      {
        "company_order_details": [
          {
            "stage_cost_driver_id": 6,
            "company_user": {
              "delivery_salary": 10
            }
          }
        ]
      }
    ]
  }
}

But in the log, I'm getting the following output:

Input { order_id: [ 247, 260, 253 ] }
Item:  [ 247, 260, 253 ]
DEBUG:  { errors:
   [ { extensions: [Object],
       message:
        'parsing HashMap failed, expected Object, but encountered Array' } ] }
Driver Payment Data:  undefined

It says that it expects object but encountered array. But from what I see, I'm already getting an object "data": {[....]} with array inside it from the output at hasura's API console.

What am I missing here? How can I get the data of stage_cost_driver_id and delivery_salary?

CodePudding user response:

Shouldn't variables be an object?

     body: JSON.stringify({
        query: gql_query,
        variables: {orders: variables}
      })
  • Related