Parse Error With Graph QL when updating column values for sub item

Hello,
I’m trying to create a subitem for a parent item, and I have difficulties with a mutation who work on playground but not in my code.
(TypeScript)

const client: GraphQLClient = new GraphQLClient(
      'https://api.monday.com/v2/',
      {
        headers: {
          'Content-Type': 'application/json',
          Authorization: 'my token',
        },
      },
    );

const item_id: number = XXX;
const metric_item = 'Name';
const metric_value = JSON.stringify({ numbers: '22' });

const add_metric_sub_item_mutation = `
  mutation {
    create_subitem (parent_item_id: ${item_id}, item_name: ${metric_item}, column_values: ${metric_value} {
      id
     }
}
`;

await client.request(add_metric_sub_item_mutation);

I also try with variables but always the same error. I also try every solution found in previous community posts but still here…

Error: Parse error on "column_values" (IDENTIFIER) at [3, 83]: {"response":{"errors":[{"message":"Parse error on \"column_values\" (IDENTIFIER) at [3, 83]","locations":[{"line":3,"column":83}]}],"account_id":14676321,"status":200,"headers":{}},"request":{"query":"\n      mutation {\n        create_subitem (parent_item_id: XXX, item_name: Name, column_values: {\"numbers\":\"22\"} {\n          id\n        }\n      }\n    "}}

Any tips?

I suggest looking into using GraphQL Variables rather than string substitution. The issue you’re running into is the metric_value JSON being substituted isn’t properly escaped for quotes when the string gets serialized by the client for the request.

These let you define placeholders the graphql server will use values from a “variables” object thats included with the request when it processes, rather than string substitution.

Your names can be whatever you want them to be, but i prefer being specific to the query

const variables = {
  itemId: item_id,
  itemName: metric_item,
  values: metric_value
}

const query = `
  mutation ($itemId: ID!, $itemName: String!, $values: JSON!) {
    create_subitem (parent_item_id: $itemId, item_name: $itemName, column_values: $values {
      id
     }
}
`

await client.request(query, variables)

GraphQLClient supports a variables object being supplied alongside the query. Another advantage of variables is you don’t have to allocate a string every time its called, you can create the query string outside the query function.

Also add the "“API-Version”: “2023-10"” header. 2023-07 is going to be removed from service on January 15th, so you want to make sure you’re testing against the new API - which does have a lot of breaking changes.

One change in 2023-10 is item IDs are now ID type instead of Int type. ID can be a string or int. This means you no longer have to coerce your query responses where the ID is a string to a number to use them in a mutation.

I made the updates but it still not working :confused:
Here is sample code I am trying

const item_id: string = item.create_item.id;
const metric_item = 'Total connections';
const total_connections = { numbers: '22' };

const variables = {
  itemId: item_id,
  itemName: metric_item,
  values: total_connections,
};

const query = `
  mutation ($itemId: ID!, $itemName: String!, $values: JSON!) {
      create_subitem (parent_item_id: $itemId, item_name: $itemName, column_values: $values {
          id
         }
      }
    `;

await client.request(query, variables);

Here is the error:
Error: GraphQL Error (Code: 500): {“response”:{“error_message”:“Internal server error”,“status_code”:500,“status”:500,“headers”:{}},“request”:{“query”:"\n mutation ($itemId: ID!, $itemName: String!, $values: JSON!) {\n create_subitem (parent_item_id: $itemId, item_name: $itemName, column_values: $values) {\n id\n }\n }\n ",“variables”:{“itemId”:“5679169037”,“itemName”:“Total connections”,“values”:{“numbers”:“22”}}}}

I also added:
‘Content-Type’: ‘application/json’,
‘API-Version’: ‘2023-10’,

inside the header

Update, its working because I forgot to wrap ma values by JSON.stringify() after your updates. Thanks!

1 Like

Glad it works! I don’t think variables add much complexity to your code, and save a lot of headaches around escaping strings.

const total_connections = JSON.stringify({ [column_id]: number_of_connections})

Just sharing the above because you may want to abstract out the column_id because those can get changed over time on accident, or you may want to reuse code in some way on another board. So that makes it a bit easier to edit since you might be able to make the column_id a constant someplace if you use it in many places. Or as you advance and create recipes and custom triggers you can abstract further. (Maybe you want to store this data to several boards one day, having a reusable recipe lets you reuse the code, the recipe just passes the column for that board to your code). I don’t know what you’re working on, just pointing out some more “advanced” techniques to think about before you box yourself in inadvertently.

1 Like

Thank you @codyfrisch for the help!

1 Like