2

I would like to modify this string:

query = """
    mutation { 
     productUpdate(input: {id: "gid://shopify/Product/463...03", title: "test", descriptionHtml: "Amazing product"}) {
       product {
         id
       }
       userErrors {
         field
         message
        }
      }
     }
  """

I'm trying to put a variable in the place of "test" and "Amazing product". Normally i would do this with f-strings, but because of the GraphQL brackets i cannot do this.

Does anyone have an idea on how to solve this problem?

Many thanks!

3 Answers 3

3

you can double up the curly braces to escape them:

my_title = "The Title"
my_description = "The description"

query = f"""
    mutation {{ 
     productUpdate(input: {{id: "gid://shopify/Product/463...03", title: "{my_title}", descriptionHtml: "{my_description}"}}) {{
       product {{
         id
       }}
       userErrors {{
         field
         message
        }}
      }}
     }}
  """

https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals

Sign up to request clarification or add additional context in comments.

Comments

2

You can use the graphql-python/gql package. It supports variables.

Comments

0

While fstrings are great, in case of graphql queries you can just use below format.

query = """
    mutation { 
     productUpdate(input: {id: "gid://shopify/Product/463...03", title: "%s", descriptionHtml: "%s"}) {
       product {
         id
       }
       userErrors {
         field
         message
        }
      }
     }
  """ % ('test', 'Amazing Product')

print(query)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.