skip to Main Content

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.

2

Answers


  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 GuzzleHttpClient([
            '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,
    ];
    
    Login or Signup to reply.
  2. Go to Apps -> Manage Apps at the bottom and then :
    Create a private app in Shopify, which will connect to your application. Make sure you manage permission for what you want to query

    After creating the private app you will get the password which you can use as the token for your HTTP requests with header ‘X-Shopify-Access-Token’ value: password

      curl -X POST 
      https://{shop}.myshopify.com/admin/api/2021-04/graphql.json 
      -H 'Content-Type: application/graphql' 
      -H 'X-Shopify-Access-Token: {password}' 
      -d '
      {
        products(first: 5) {
          edges {
            node {
              id
              handle
            }
          }
          pageInfo {
            hasNextPage
          }
        }
      }
      '
    

    For more visit: https://shopify.dev/docs/admin-api/getting-started#authentication

    The way I use in NodeJS is by using package "graphql-request" to make requests and

    const mutation = gql`
          mutation createProduct(
            $input: ProductInput!
            $media: [CreateMediaInput!]
          ) {
            productCreate(input: $input, media: $media) {
              userErrors {
                field
                message
              }
              product {
                id
                metafields(first: 1) {
                  edges {
                    node {
                      id
                    }
                  }
                }
              }
            }
          }
        `;
         //const input = form your own input 
         const res = await graphQLClient.rawRequest(mutation, input);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search