Creating Items Unexpected token

The error is because you’re manipulating strings, passing through json.dumps() multiple times and its adding extra \ characters that are causing the graphql query parser to error.

The problem I see is there is no reason to be building and manipulating strings. The column values is a JSON string of a regular object - build the object first, then turn it into a JSON string rather than trying to build the JSON from strings.

I am a javascript guy, so bear with me. I am also leaving out a lot that isn’t about creating the request body to send to the server

Build your columnValues as an object, I will leave that up to you as to how you do it (because I am not a python guy). If you’re using column values you retrieved with the API, the value property of a column_value is a JSON string. Use JSON.parse() or json.loads() to get an actual value object you can work with rather than strings that you need to manipulate. (Or use fragments in column_value v2 in the new API to bypass the JSON string all together.)

This is an example of the structure you’re creating, which you’re doing through string manipulation, but instead just make this object. Keys are the column ID and their values is the value object for the type of column it is. In this case all are text.

const columnValuesObject = {
   columnId: {text: "string"},
   columnId2: {text: "string2"},
   columnId3: {text: "string3"},
}

Look into is GraphQL variables. https://graphql.org/learn/queries/#variables so that you aren’t trying to do string manipulation on the query itself. Create a working query in the playground and copy it with the copy button and use it. :slight_smile: Using variables lets the server substitute values after parsing the query.

const query = `mutation ($boardId: ID!, $itemName: String!, $groupId: String, $columnValues: JSON) {
  create_item(
    board_id: $boardId
    group_id: $groupId
    item_name: $itemName
    column_values: $columnValues
  ) {
    id
  }
}`

// yes the above query is correct, you can steal it.

In the playground you can create the variables for testing, its below the query section. In the playground - you type JSON in. In your code though, its just an object for the variables:

const variables = {
     boardId: 1235423432,
     itemName: "item name is this",
     columnValues: JSON.stringify(columnValuesObject), //this is where we turn the columnValuesObject into a string.
     groupId: "groupid_string"
}

// in your code you will have variables for the boardId, itemName, groupId 
// that are used in this object, I'm skipping writing that out here.

now to put it together:

const requestBody = JSON.stringify({
    query,
    variables,
})

// use your favorite tool to post the requestBody to the API server. 
// the exact method may vary since some clients can take an object
// and will stringify it for you so you''d skip the stringify above.

1 Like