Python 3.9 Mutation for linking items

Hello!
I have two connected boards trying to connect one item in board1 with multiple items from board2. I’m using apiVersion = “2023-10”.

First of all, I tested this mutation in playground, and everything worked:

mutation{
	change_multiple_column_values(item_id:123456, board_id: 123456, column_values: "{\"link_to_another_board\" : {\"item_ids\" : [123457, 123458, 123459]}}") {
    id
  }
}

But, when I try to implement this code into my Python script, there are some issues with strings, backslashes, and variables.

I only have 2 variables:

# The int, contains items id from board1:
new_item_id = r2.json()['data']['create_item']['id']

print(f'New item id: {new_item_id}')   --> New item id: 123456
#The list of items from board2, which must to be linked with item above:

print(f"linked_items: {linked_items}") --> linked_items: [123457, 123458, 123459]

Now, I need to make ‘mutation query’.

First, lets declare a variables for GraphQL schema:

graph_var_id = {"item_id": int(new_item_id), "linked_items": json.dumps(linked_items)}

print(f'graph_var_id: {graph_var_id}') --> graph_var_id: {'item_id': 1330819451, 'linked_items': '[123457, 123458, 123459]'}

It looks good, but I already don’t like the ‘’ around the [123457, 123458, 123459]

Ok, make query:

query_link_creation = 'mutation ($linked_items: JSON!, $item_id: Int!) {change_multiple_column_values(item_id: $item_id, board_id: 123456, column_values: "{\" link_to_another_board \" : {\"item_ids\" : $linked_items}}") {id}}'

Make data and request:

data3 = {'query': query_link_creation, 'variables': graph_var_id}

print(f'data3: {data3}') --> 
data3: {'query': 'mutation ($linked_items: JSON!, $item_id: Int!) {change_multiple_column_values(item_id: $item_id, board_id: 123456, column_values: "{" link_to_another_board " : {"item_ids" : $linked_items}}") {id}}', 'variables': {'item_id': 123456, 'linked_items': '[123457, 123458, 123459]'}}

And, of course, the lack of backslashes\ it causes an error:

r3 = requests.post(url=apiUrl, json=data3, headers=headers)

print(f'r3: {r3.json()}') -->

r3: {'errors': [{'message': 'Parse error on " : {" (STRING) at [1, 151]', 'locations': [{'line': 1, 'column': 151}]}], 'account_id': 1111111111111}

But I really don’t know how to make this query. I already trying to use f-string (and become an errors, because f-string didn’t support backslashes), trying to use placeholders, and even this: my_string = f"This is a backslash: {chr(92)}".

I already read this topic:

But it didn’t help me.

Please, help me :slight_smile:

I can’t do that because of Python syntaxis, this is the main problem :slight_smile:
The {var} in python is a variable declaration inside “f-string”.
When i try to make “column_values” variable i need to use {{}} for GraphQL syntaxis, and then i also need to add inside {{}} another {} with my variable, which contain a list of ids.

This is exactly why im trying to make the whole query as a string, and put as variables only IDs

I did it the other way, but it still doesn’t work:

UA_id: [1328478802, 1330082698, 1330278661]
New_item_id: 1332245910

column_values = '{\"link_to_t_am\" : {\"item_ids\" : ' + str(UA_id) + '}}'

graph_var_id = {"item_id": int(new_item_id), "$values": json.dumps(column_values)}

query_link_creation = 'mutation ($values: JSON!, $item_id: ID!) {change_multiple_column_values(item_id:$item_id, board_id:1315023730, column_values: $values) {id}}'

data3 = {"query": query_link_creation, "variables": graph_var_id}

print(f'data3: {data3}')

r3 = requests.post(url=apiUrl, json=data3, headers=headers)

print(f'r3: {r3.json()}')

And result:


UA_id: [1328478802, 1330082698, 1330278661]

graph_var_id: {'item_id': 1332245910, '$values': '"{\\"link_to_t_am\\" : {\\"item_ids\\" : [1328478802, 1330082698, 1330278661]}}"'}

data3: {'query': 'mutation ($values: JSON!, $item_id: ID!) {change_multiple_column_values(item_id:$item_id, board_id:1315023730, column_values: $values) {id}}', 'variables': {'item_id': 1332245910, '$values': '"{\\"link_to_t_am\\" : {\\"item_ids\\" : [1328478802, 1330082698, 1330278661]}}"'}}

r3: {'errors': [{'message': 'Variable $values of type JSON! was provided invalid value', 'locations': [{'line': 1, 'column': 11}], 'extensions': {'value': None, 'problems': [{'path': [], 'explanation': 'Expected value to not be null'}]}}], 'account_id': xxxxxx}

Okay, i solve the problem. My solution looks stupid, but it works.

I have some variables in my Python script:

New item id: 1333012078

UA_id: [1328478802, 1330082698, 1330278661, 1333009859]

Make query by hands:

part1 = 'mutation {change_multiple_column_values(item_id: ' + str(new_item_id) + ', board_id: 1315023730, column_values: "{\\\"link_to_t_am\\\" : {\\\"item_ids\\\" : '
part3 = '}}") {id}}'
query_link_creation = part1 + str(UA_id) + part3

data3 = {"query": query_link_creation}

r3 = requests.post(url=apiUrl, json=data3, headers=headers)

And everything works, without query $variables

1 Like