Json and GraphQL Variables expected String to be a Hash

Viewed 1292

I am using GraphQL for the first time, an from what i could see the formatting is just the same as Json. But i am having a horrible time getting it working correctly. I have used PostMan to ensure my Querys are correct. But when taking it from Postman and trying to run it in PHP, I am getting endless problems.

My last attempt looking at another post on here, was passing the Query and Variables separately But the response i keep getting is:

{"errors":{"variables":"expected String to be a Hash"}}

My code:

 $query = <<<'JSON'
query ($first: Int, $sku: String)
{ productVariants(first: $first, query: $sku) {
    edges {
      node {
        title
        id
        sku
        product {
          title
        }
        inventoryItem {
          id
          inventoryLevels(first: 3) {
            edges {
              node {
                id
                available
                location {
                  id
                  name
                  address {
                    city
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
JSON;

            $variables = '{
  "first": 4,  
  "sku": "100560 OR 100559 OR 100558 OR 100557"
}';

curl_setopt_array($curl, array(
            CURLOPT_URL => "https://XXXXXXXXXXXXXXXXXXXXXXX.myshopify.com/admin/api/2020-04/graphql.json",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $json,
            CURLOPT_HTTPHEADER => array(
                "Content-Type: application/json",
                "Cookie: __cfduid=d10b3b9586e13d4b79ffd43e3cdec77f21592474922"
            ),
        ));

        $response = curl_exec($curl);

Is my formatting wrong?

Here is the raw copy from Postman:

query ($first: Int, $sku: String)
{ productVariants(first: $first, query: $sku) {
    edges {
      node {
        title
        id
        sku
        product {
          title
        }
        inventoryItem {
          id
          inventoryLevels(first: 3) {
            edges {
              node {
                id
                available
                location {
                  id
                  name
                  address {
                    city
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Variables:

{
  "first": 4,  
  "sku": "100560 OR 100559 OR 100558 OR 100557"
}
2 Answers

After much headache. It would appear it variables just needs to be formatted in an array.

$variables = array(
    "first" => 4,
    "sku" => "100560 OR 100559 OR 100558 OR 9995565"
);

Hope this helps someone else who experiances the same error. Not sure why it says Hash, such a missleading error.

Getting same error while adding metafield for a product. I have fixed using @dave's answer. Exactly what I have done is

$data['query'] = 'mutation($input: ProductInput!) {
                        productUpdate(input: $input) {
                            product {
                                id
                            }
                            userErrors {
                                field
                                message
                            }
                        }
                    }';
$data['variables'] = array('input'=>[
                    "id" => "gid://shopify/Product/xxxxxxxx",
                    "metafields" => array(
                        ['namespace'=> "awesome",
                        'key'=> "newKey",
                        'value'=> "someValue",
                        'valueType'=> "STRING"]
                    )
                ]
                );

Than curl request

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
    curl_setopt($ch, CURLOPT_POST, true);
Related