declare variables in a mutation using PHP and GraphQL

Bdelucca
Excursionist
16 2 3

Hi I was trying to change an image src from a product using PHP and GraphQL API, but I am having problems with the declaration of varibles and the access.

here is my code:

----------------------------------

<?php

$curl = curl_init();
//Tell cURL where our certificate bundle is located.
$certificate = "C:\cacert.pem";

curl_setopt_array($curl, [
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_CAINFO => $certificate,
  CURLOPT_CAPATH => $certificate,
  CURLOPT_POSTFIELDS => '{
    "query":"mutation productImageUpdate($productId: ID!, $image: ImageInput!) {
    productImageUpdate(productId: $productId, image: $image) {
      image {
        id
      }
      userErrors {
        field
        message
      }
    }}", 
    "variables": {
      "productId": "gid://shopify/Product/6101451735237",
      "image": { "src" : "https://picsum.photos/200/300"}
    }
  }',
  CURLOPT_HTTPHEADER => [
    "Content-Type: application/graphql",
    "X-Shopify-Access-Token: XXXXXX",
  ],
]);

function isJson($string) {
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
}

$response = curl_exec($curl);

curl_close($curl);

?>
 
-------------------------------------------------------------
 
Terminal:
{"errors":[{"message":"Parse error on \"query\" (STRING) at [3, 5]","locations":[{"line":3,"column":5}]}]}
 
 
 
Thanks for the Help
Replies 4 (4)

Gregarican
Shopify Partner
1033 86 285

Using GQL the process of assigning a new image to a product involves a few steps. For example, below is my Postman test of your API request that's giving you trouble. I pointed the productId to a valid one in my test shop. Note the API response. The image being assigned to a specific product likely needs to already be defined in Shopify. So you'd need to perform the initial step of uploading it in Shopify so that it's already defined by an ImageId. This is once you get past the PHP parsing issues that Shopify is complaining about...

Untitled.png

Bdelucca
Excursionist
16 2 3

 

Hi Greg! Thanks for the quick answer.

I thought I had a problem with the ID for the new Images. But my biggest issue is the GQL parsing complaining. I would like to know how to declare variables in the query.

I did found a way around it by declaring the variables in php and then implementing a function in the POSTFIELDS. But I would like to know how do it without it. 

regards!

Gregarican
Shopify Partner
1033 86 285

When I run test GQL API requests in Postman, I have the Content-Type defined as application/json. Here's how the raw data looks as captured with Fiddler. Maybe this helps a little more?

POST https://{my_shop}.myshopify.com/admin/api/2020-10/graphql.json HTTP/1.1
X-Shopify-Access-Token: {my_token}
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 47556ad1-c432-4bce-bc52-fa6ec9c6ee5b
Host: {my_shop}.myshopify.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 417
Cookie: xxxx

{"query":"  mutation productImageUpdate($productId: ID!, $image: ImageInput!) {\r\n    productImageUpdate(productId: $productId, image: $image) {\r\n      image {\r\n        id\r\n      }\r\n      userErrors {\r\n        field\r\n        message\r\n      }\r\n    }\r\n  }\r\n\r\n","variables":{
      "productId": "gid://shopify/Product/4423635632180",
      "image": { "src" : "https://picsum.photos/200/300"}
}}

 

Bdelucca
Excursionist
16 2 3

Thanks Greg! I will let you know how it goes. 😉