Goutham
Goutham

Reputation: 362

How to pass array of objects through ajax post request for graphQl service

Here my array format is

coordinates: [
  {
    divId: "1234",
    divCor: {
      divleft: "1223",
      divtop: "455"
    },
    imgCor: {
      imgleft: "78895",
      imgtop: "452"
    }
  },
  {
    divId: "1234",
    divCor: {
      divleft: "1223",
      divtop: "455",
    },
    imgCor: {
      imgleft: "78895",
      imgtop: "452",
    }
  }
]

My ajax request is

  var data = "mutation M{publishProduct(coordinates: "+this.state.coordinates+" ){_id}}";

    console.log("publishProduct: "+JSON.stringify(data));

    $.ajax ({
      type: "POST",
      url: "/",
      contentType: "application/graphql",
      data: data,
      dataType : 'json',
      success:(data) => {

console I kept above displays as

 publishProduct: "mutation M{publishProduct(coordinates: [object Object],[object Object],[object Object] ){ _id}}  // Before passing data`

It is not passing as the format I needed.

Please help me how to pass array of objects for graphQl request.

Upvotes: 1

Views: 581

Answers (1)

WuDo
WuDo

Reputation: 195

You have to JSON.stringify the coordinates before you join them to mutation string:

var data = "mutation M{publishProduct(coordinates: "+JSON.stringify(this.state.coordinates)+" ){_id}}";

Upvotes: 2

Related Questions