PowerShell GraphQL Mutation Parsing Error

Viewed 609

I'm querying the Monday API v2 (https://monday.com/developers/v2) I'm getting stuck in getting the PowerShell query to work, the script is below:

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
    $url = "https://api.monday.com/v2/"
    $hdr = @{ }
    $hdr.Add("Authorization", "redacted")
    $hdr.Add("Content-Type", "application/json")

    $bodytxt = '{"query":"mutation{change_column_value (board_id: 528033866, item_id: 574335355, column_id: \"status_1\", value: "{\"index\": 1}") {id}}"}'
    $response = Invoke-WebRequest -Uri $url -Headers $hdr -Method Post -body $bodytxt
    Write-Host $response 

And it keeps returning

{"errors":[{"message":"Parse error on \": 1}\" (STRING) at [1, 110]","locations":[{"line":1,"column":110}]}],"account_id":5305133}

The mutation query in Postman works fine

change_column_value (board_id: 528033866, item_id: 574335355, column_id: "status_1", value: "{\"index\": 1}"){id}
}

I have tried working with what quotes are escaped, but I still haven't been able to get it to run succesfully. I was referencing the tips here https://www.reddit.com/r/PowerShell/comments/b9jasa/query_graphql_with_powershell/

Any thoughts on how to fix this error?

EDIT: Sorry forgot to add the current query (not mutation) that works fine through Powershell

$bodytxt = '{"query":"{  boards (ids: 528033866) {    groups (ids: \"group_title\"){        items () {            id            name       updates () {  body }     column_values () {        id        title        value    text  }        }        title    }    }  }"}'
1 Answers

The easiest way to create a GraphQL query is

  1. create a hashtable with HERE-STRINGS
  2. convert it to a JSON string with ConvertTo-Json.
$bodytxt = @{"query" = @'
  mutation {
    change_column_value(
      board_id: 123123
      item_id: 456456
      column_id: "status1"
      value: "{\"index\":1}"
    ) {
      id
    }
  }
'@
} | ConvertTo-Json

White spaces inside @'...'@ are optional.


reply to this comment

it should always work in the API call with the GraphQL?

I think it depends on services, but in most cases, yes. application/json is the GraphQL default content type for the request body.

and for monday.com

Be sure to use the application/json content type

Related