Ad

Shopify GraphQL Mutation Returns "Parse Error On \"gid\" (IDENTIFIER)"

- 1 answer

I am trying write some PHP code to run a mutation on the shopify graphql api. When I run the mutation from command line curl, it works well. But, for some reason PHP curl for the same returns an error/

Here is the command line curl and its output.

    curl -X  POST \
    > "https://myshopname.myshopify.com/admin/api/2019-04/graphql.json" \
    > -H "Content-Type: application/graphql" \
    > -H "X-Shopify-Access-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
    > -d '
    > mutation{
    > productVariantUpdate(input: {
    >     id: "gid://shopify/ProductVariant/126493786xxxx",
    >     price: 2998.5
    >   }){
    >     productVariant{ id, price }
    >   }
    > 
    > }
    > '
    {"data":{"productVariantUpdate":{"productVariant":{"id":"gid:\/\/shopify\/ProductVariant\/126493786xxxx","price":"2998.50"}}},"extensions":{"cost":{"requestedQueryCost":10,"actualQueryCost":10,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":990,"restoreRate":50.0}}}}

I cannot post the exact php code since its from multiple files, but here is the payload of the php curl call

    mutation{productVariantUpdate(input: {    id: "gid://shopify/ProductVariant/126493786xxxx",    price: 2998.5  }){    productVariant{ id, price }  }}

and the curl_info output

    array(
        'url' => 'https://myshopname.myshopify.com/admin/api/2019-04/graphql.json',
        'content_type' => 'application/json; charset=utf-8',
        'http_code' => (int) 200,
        'header_size' => (int) 2431,
        'request_size' => (int) 385,
        'filetime' => (int) -1,
        'ssl_verify_result' => (int) 0,
        'redirect_count' => (int) 0,
        'total_time' => (float) 0.719179,
        'namelookup_time' => (float) 0.060711,
        'connect_time' => (float) 0.123953,
        'pretransfer_time' => (float) 0.273472,
        'size_upload' => (float) 151,
        'size_download' => (float) 110,
        'speed_download' => (float) 152,
        'speed_upload' => (float) 210,
        'download_content_length' => (float) -1,
        'upload_content_length' => (float) 151,
        'starttransfer_time' => (float) 0.718514,
        'redirect_time' => (float) 0,
        'redirect_url' => '',
        'primary_ip' => '23.227.63.64',
        'certinfo' => array(),
        'primary_port' => (int) 443,
        'local_ip' => '192.168.0.101',
        'local_port' => (int) 40882,
        'request_header' => 'POST /admin/api/2019-04/graphql.json HTTP/1.1
    Host: alberto-torresi-2.myshopify.com
    User-Agent: wcurl
    Accept: */*
    Content-Type: application/graphql
    X-Shopify-Access-Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    Content-Length: 151

    '
    )

The error returned is:

    {"errors":[{"message":"Parse error on \"gid\" (IDENTIFIER) at [1, 49]","locations":[{"line":1,"column":49}]}]}
Ad

Answer

It would be good to see a bit more of the php code to see what the error might be, but the issue could be related to you not escaping the quotes in the mutation.

Try changing:

mutation{productVariantUpdate(input: {    id: "gid://shopify/ProductVariant/126493786xxxx",    price: 2998.5  }){    productVariant{ id, price }  }}

to:

mutation{productVariantUpdate(input: {    id: \"gid://shopify/ProductVariant/126493786xxxx\",    price: 2998.5  }){    productVariant{ id, price }  }}

To escape the quotes in the string

Ad
source: stackoverflow.com
Ad