Best way to construct a GraphQL query string in Python
You can use the """ multiline string method. For injecting variables, make sure to use the $ sign while defining the string and use the variables object in the JSON parameter of the requests.post method.
Here is an example. ContactInput
is one of the types I defined in my GraphQL schema.
query = """
mutation ($input:[ContactInput!]!) {
AddContacts(contacts: $input) {
user_id
}
}
"""
variables = {'input': my_arrofcontacts}
r = requests.post(url, json={'query': query , 'variables': variables})