0

I am trying to save data in db using graphql but i am getting below error.

Error

{
  "errors": [
    {
      "message": "Syntax Error: Expected Name, found String \"action\"",
      "locations": [
        {
          "line": 4,
          "column": 32
        }
      ],
      "extensions": {
        "code": "GRAPHQL_PARSE_FAILED"
      }
    }
  ]
}

filterStr Data:

{"action":"count","demography":{"email_domain":"","age":{"from":"11-13-1919","to":"11-13-2018"},"gender":["male","female"],"company":"","phone":"","source":"","forms_submitted":"","added_by":"","education":"","occupation":"","interest":"","location":[],"paying_customer":""},"email":{"sent":[],"delivered":[],"opened":[],"clicked":[]}}

Codes:

const USER_TOKEN = sessionStorage.getItem("user_token");
const audienceName = JSON.stringify(this.state.audienceName);
const userId = sessionStorage.getItem("id");
const filterStr = JSON.stringify(filter);//without stringify it is insering as [Object, Object]
let dataQuery = {
    query: `mutation{
    createAudience(input:{data:{
      audience_name:${audienceName},
      filters:${filterStr},
      created_by: ${userId},
    }}){
      audience{
        id
      }
    }
  }`
}


const add_new_audience = {
    method: methodType,
    url: constants.graphql,
    headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${USER_TOKEN}`
    },
    data: JSON.stringify(dataQuery)
};

const resp = await axios(add_new_audience);
console.log(resp);
if(resp) {
    this.props.history.push("/audiences");
}

Can anyone help me where i need to change the code?

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78
  • Is the request made (check network tab dev tools)? You can add chromeiQL extension to chrome and try the query that is sent (if it's sent and you can see what is sent in the network tab). Maybe that can help you a bit on how to debug it. – HMR Nov 13 '19 at 18:49
  • 1
    Using string interpolation to inject values into your document is error prone. Using `JSON.stringify` is insufficient since JSON syntax is not the same as GraphQL syntax. Use variables instead to pass in your dynamic values as shown [here](https://stackoverflow.com/questions/57924792/template-literal-getting-error-from-api/57926450). – Daniel Rearden Nov 13 '19 at 21:08

0 Answers0