<?php
 declare(strict_types=1); namespace Shopify\Clients; use Shopify\Exception\HttpRequestException; use Psr\Http\Client\ClientExceptionInterface; use Shopify\Exception\UninitializedContextException; use Shopify\Context; use Shopify\Exception\MissingArgumentException; class Graphql { private readonly Http $client; protected ?string $token; public function __construct( string $domain, ?string $token = null ) { if (!Context::$IS_PRIVATE_APP && empty($token)) { throw new MissingArgumentException('Missing access token when creating GraphQL client'); } $this->client = new Http($domain); $this->token = $token; } public function query( $data, array $query = [], array $extraHeaders = [], ?int $tries = null ): HttpResponse { if (empty($data)) { throw new MissingArgumentException('Query missing'); } list($accessTokenHeader, $accessToken) = $this->getAccessTokenHeader(); $extraHeaders[$accessTokenHeader] = $accessToken; if (is_array($data)) { $data = json_encode($data); } else { $data = json_encode([ "query" => $data ]); } return $this->client->post( $this->getApiPath(), $data, $extraHeaders, $query, $tries, ); } public function proxy( string $data, array $extraHeaders = [], ?int $tries = null ): HttpResponse { if (empty($data)) { throw new MissingArgumentException('Query missing'); } list($accessTokenHeader, $accessToken) = $this->getAccessTokenHeader(); $extraHeaders[$accessTokenHeader] = $accessToken; return $this->client->post( $this->getApiPath(), $data, $extraHeaders, [], $tries, Http::DATA_TYPE_JSON, ); } protected function getApiPath(): string { return 'admin/api/' . Context::$API_VERSION . '/graphql.json'; } protected function getAccessTokenHeader(): array { $accessToken = Context::$IS_PRIVATE_APP ? Context::$API_SECRET_KEY : $this->token; return [HttpHeaders::X_SHOPIFY_ACCESS_TOKEN, $accessToken]; } } 