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 method.

Here is an example. ContactInput is one of the types I defined in my GraphQL schema.

query = """
  mutation ($input:[ContactInput!]!) {
    AddContacts(contacts: $input) {
variables = {'input': my_arrofcontacts}
r =, json={'query': query , 'variables': variables})