Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

cURL formatting for GraphQL bulk mutation

cURL formatting for GraphQL bulk mutation

CameronG
Shopify Partner
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 14

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 } } } " }'