I need to show Product Data (Name SKU
)in front end using graphQL
is this possible to show Product Data custom page in Magento 2 ?
1 Answer
Yes It's possible... Create new template file and link in xml file where you want... And Paste this codes in template file...
<?php
$query = '
query {
products(filter: {category_id: {eq: "2"}}) {
items {
name
sku
}
}
}
';
$json = json_encode(['query' => $query]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, '[YOUR END POINT]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
'User-Agent: PHP Script',
'Content-Type: application/json;charset=utf-8',
'Authorization: bearer [YOUR TOKEN]'
)
);
$response = curl_exec($curl);
echo "<pre>",var_dump($response),"</pre>";
?>