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