问题
I am trying to request shopify graphql-admin-api from my api. I am doing it according to the documentation given by graphql-admin-api, but it still gives me authorization errors.
回答1:
PHP users can follow this function to make request to Shopify Admin API using GraphQL
I am using GuzzleHttp ( PHP HTTP client ) to create request
public function graph($query , $variables = []){
$domain = 'xxx.myshopify.com';
$url = 'https://'.$domain.'/admin/api/2019-10/graphql.json';
$request = ['query' => $query];
if(count($variables) > 0) { $request['variables'] = $variables; }
$req = json_encode($request);
$parameters['body'] = $req;
$stack = HandlerStack::create();
$client = new \GuzzleHttp\Client([
'handler' => $stack,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Shopify-Access-Token'=>$this->token // shopify app accessToken
],
]);
$response = $client->request('post',$url,$parameters);
return $body = json_decode($response->getBody(),true);
}
$query = "{ shop { name email } }"; // this is example graphQL query
$response = graph($query) // call this function
Below code can help you to check how much cost this graphQL query
$calls = $response->extensions->cost;
$apiCallLimitGraph = [
'left' => (int) $calls->throttleStatus->currentlyAvailable,
'made' => (int) ($calls->throttleStatus->maximumAvailable - $calls->throttleStatus->currentlyAvailable),
'limit' => (int) $calls->throttleStatus->maximumAvailable,
'restoreRate' => (int) $calls->throttleStatus->restoreRate,
'requestedCost' => (int) $calls->requestedQueryCost,
'actualCost' => (int) $calls->actualQueryCost,
];
来源:https://stackoverflow.com/questions/57405914/how-to-request-shopify-graphql-admin-api-from-an-api