title: Passing variables with graphql using a http client
body: <p>I'm trying to interface with shopify storefront api in golang. But this is my first encounter with graphql and I'm a bit confused.</p>
  
  <p>I need to pass variables for the request and did some research where I came to the conclusion that I could pass variables like shown below. But shopify keeps returning this error:</p>
  
  <pre><code>{"errors":[{"message":"Parse error on \"variables\" (IDENTIFIER) at [13, 3]","locations":[{"line":13,"column":3}]}]}
  </code></pre>
  
  <p>Here is my current code:</p>
  
  <pre><code>body := strings.NewReader(fmt.Sprintf(`
      mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) {
          checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) {
            userErrors {
              message
              field
            }
            checkout {
              id
            }
          }
        }
      variables: { "lineItems": [ { "quantity": 1, "variantId": "%s" } ], "checkoutId": "%s" }
  `, productID, checkoutID))
  
  req, err := http.NewRequest("POST", "https://myshop.myshopify.com/api/graphql", body)
  if err != nil {
      // handle err
      fmt.Println(err)
  }
  req.Header.Set("Content-Type", "application/graphql")
  req.Header.Set("X-Shopify-Storefront-Access-Token", "mytoken")
  
  resp, err := http.DefaultClient.Do(req)
  if err != nil {
      // handle err
      fmt.Println(err)
  }
  defer resp.Body.Close()
  
  data, err := ioutil.ReadAll(resp.Body)
  if err != nil {
      // handle err
      fmt.Println(err)
  }
  </code></pre>
  
  <p>My question is, what is the correct way of passing variables when using <code>application/graphql</code>?</p>
  
  <hr>
  
  <p>After updating the code with the answer of @DanielRearden I'm now getting this error:</p>
  
  <pre><code>{"errors":[{"message":"Variable checkoutId of type ID! was provided invalid value","locations":[{"line":1,"column":11}],"extensions":{"value":null,"problems":[{"path":[],"explanation":"Expected value to not be null"}]}},{"message":"Variable lineItems of type [CheckoutLineItemInput!]! was provided invalid value","locations":[{"line":1,"column":29}],"extensions":{"value":null,"problems":[{"path":[],"explanation":"Expected value to not be null"}]}}]}
  </code></pre>
  
  <p>Updated code:</p>
  
  <pre><code>    body := strings.NewReader(fmt.Sprintf(`{
          "query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) { checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) { userErrors { message field } checkout { id } }}",
          "variables": { 
              "$lineItems": [ 
                  { "quantity": 1, "variantId": "%s" }
              ], 
              "$checkoutId": "%s"
          }
      }`, productID, checkoutID))
      ...
      ...
      req.Header.Set("Content-Type", "application/json")
  </code></pre>
  
  <hr>
  
  <p>Removing the <code>$</code> token on the variables as shown below returns another error:</p>
  
  <pre><code>{
          "query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) { checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) { userErrors { message field } checkout { id } }}",
          "variables": { 
              "checkoutId": "%s",
              "lineItems": [ 
                  { "quantity": 1, "variantId": "%s" }
              ]
          }
      }
  </code></pre>
  
  <p>And the error is: (status 500)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;meta name="referrer" content="never" /&gt;
    &lt;title&gt;Something went wrong&lt;/title&gt;
  </code></pre>
  
  <p>But I guess this is not a graphql issue anymore but more of a shopify api problem. </p>