Join us today @ 1pm EDT for an AMA with 2H Media: Holiday Marketing for Your Shopify Store and have your marketing questions answered by marketing experts 2H Media | Plus watch the 2H Media AMA Livestream on Twitch!

cURL formatting for GraphQL bulk mutation

cURL formatting for GraphQL bulk mutation

CameronG
Visitor
1 0 0

Hello all, 

I'm trying to perform a BulkOperationRunQuery mutation query in cURL but I'm having trouble getting the formatting correct. The query runs correctly in the GraphiQL app but I keep getting errors when running in cURL. 

Here's the GraphiQL query:

 

mutation {
  bulkOperationRunQuery(
    query:"""
    {
  products {
    edges {
      node {
        id
        handle
        tags
        productType
        variants {
          edges {
            node {
              id
              sku
              price
              compareAtPrice
              inventoryItem {
                id
                unitCost {
                  currencyCode
                  amount
                }
                inventoryLevels {
                  edges {
                    node {
                      id
                      available
                      location {
                        id
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}""")
{
    bulkOperation {
      id
      status
    }
    userErrors {
      field
      message
    }
  }
}

 

 

This is the cURL command I've cobbled together:

 

curl -X POST \
  https://----------.myshopify.com/admin/api/2020-07/graphql.json \
  -H 'Content-Type: application/json' \
  -H 'X-Shopify-Access-Token:----------------'\
  -d ' {
  "query": "mutation{bulkOperationRunQuery(query:"""{products{edges{node{id handle tags productType variants{edges{node{id sku price compareAtPrice inventoryItem{id unitCost{currencyCode amount}inventoryLevels{edges{node{id available location{id name}}}}}}}}}}}}"""){bulkOperation{id status}userErrors{field message}}}"
  }'

 

But it results in this error:

 

{"error":"783: unexpected token at '{\n  \"mutation\": \"mutation{bulkOperationRunQuery(query:\"\"\"{products{edges{node{id handle tags productType variants{edges{node{id sku price compareAtPrice inventoryItem{id unitCost{currencyCode amount}inventoryLevels{edges{node{id available location{id name}}}}}}}}}}}}\"\"\"){bulkOperation{id status}userErrors{field message}}}\"\n  }'"}

 

 

I'm sure I'm not formatting the GraphQL query properly for cURL but not sure how to correct it. 

Any help with this or even documentation links would be appreciated!

Cheers!

-C

Reply 1 (1)

brdata
Shopify Partner
50 2 13

The triple quotation marks need to be escaped with a backslash. For example:

curl -X POST https://XXXXX.myshopify.com/admin/api/2020-10/graphql.json \
  -H 'Content-Type: application/json' \
  -H 'X-Shopify-Access-Token: shpat_XXXXXXXX' \
  -d '{"query" : "mutation { bulkOperationRunQuery( query:\"\"\" { products { edges { node { id vendor tags } } } } \"\"\" ){ bulkOperation { id status } userErrors { field message } } } " }'