How to request shopify graphql-admin-api from an api?

寵の児 提交于 2019-12-11 17:58:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!