Skip to content

Commit 87a3852

Browse files
Muhammad FaragMuhammadFarag
authored andcommitted
Update Graphql proxy.
Proxy passes data as is and sets the datatype as JSON.
1 parent 2f7e8f3 commit 87a3852

4 files changed

Lines changed: 78 additions & 7 deletions

File tree

src/Clients/Graphql.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(
3434
*
3535
* @param string|array $data Query to be posted to endpoint
3636
* @param array $query Parameters on a query to be added to the URL
37-
* @param array &$extraHeaders Any extra headers to send along with the request
37+
* @param array $extraHeaders Any extra headers to send along with the request
3838
* @param int|null $tries How many times to attempt the request
3939
*
4040
* @return HttpResponse
@@ -44,7 +44,7 @@ public function __construct(
4444
public function query(
4545
string | array $data,
4646
array $query = [],
47-
array &$extraHeaders = [],
47+
array $extraHeaders = [],
4848
?int $tries = null
4949
): HttpResponse {
5050
if (empty($data)) {
@@ -71,6 +71,40 @@ public function query(
7171
);
7272
}
7373

74+
/**
75+
* Proxy string query to this client's domain.
76+
*
77+
* @param string $data Query to be posted to endpoint
78+
* @param array $extraHeaders Any extra headers to send along with the request
79+
* @param int|null $tries How many times to attempt the request
80+
*
81+
* @return \Shopify\Clients\HttpResponse
82+
* @throws \Psr\Http\Client\ClientExceptionInterface
83+
* @throws \Shopify\Exception\MissingArgumentException
84+
* @throws \Shopify\Exception\UninitializedContextException
85+
*/
86+
public function proxy(
87+
string $data,
88+
array $extraHeaders = [],
89+
?int $tries = null
90+
): HttpResponse {
91+
if (empty($data)) {
92+
throw new MissingArgumentException('Query missing');
93+
}
94+
95+
list($accessTokenHeader, $accessToken) = $this->getAccessTokenHeader();
96+
$extraHeaders[$accessTokenHeader] = $accessToken;
97+
98+
return $this->client->post(
99+
path: $this->getApiPath(),
100+
body: $data,
101+
dataType: Http::DATA_TYPE_JSON,
102+
headers: $extraHeaders,
103+
tries: $tries,
104+
query: [],
105+
);
106+
}
107+
74108
/**
75109
* Fetches the URL path to be used for API requests.
76110
*

src/Utils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ public static function decodeSessionToken(string $jwt): array
169169
* @param string $rawBody The raw HTTP request payload
170170
*
171171
* @return HttpResponse
172+
* @throws \Psr\Http\Client\ClientExceptionInterface
172173
* @throws \Shopify\Exception\CookieNotFoundException
173174
* @throws \Shopify\Exception\MissingArgumentException
174175
* @throws \Shopify\Exception\SessionNotFoundException
176+
* @throws \Shopify\Exception\UninitializedContextException
175177
*/
176178
public static function graphqlProxy(array $rawHeaders, array $cookies, string $rawBody): HttpResponse
177179
{
@@ -182,8 +184,6 @@ public static function graphqlProxy(array $rawHeaders, array $cookies, string $r
182184

183185
$client = new Graphql($session->getShop(), $session->getAccessToken());
184186

185-
// If the body is not JSON, we forward it as a string
186-
$parsedBody = json_decode($rawBody, true) ?: $rawBody;
187-
return $client->query(data: $parsedBody);
187+
return $client->proxy(data: $rawBody);
188188
}
189189
}

tests/Clients/GraphqlTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,41 @@ public function testCanQueryWithExtraHeaders()
190190
new HttpResponseMatcher(decodedBody: json_decode($this->querySuccessResponse, true))
191191
);
192192
}
193+
194+
public function testProxyForwardsBodyAsJsonType()
195+
{
196+
$queryToProxy = <<<QUERY
197+
{
198+
"variables": {},
199+
"query": "{\nshop {\n name\n __typename\n }\n}"
200+
}
201+
QUERY;
202+
203+
$extraHeaders = ['Extra-Extra' => 'hear_all_about_it'];
204+
$client = new Graphql($this->domain, 'token');
205+
206+
$this->mockTransportRequests(
207+
[
208+
new MockRequest(
209+
response: $this->buildMockHttpResponse(200, json_decode($this->querySuccessResponse, true)),
210+
url: "https://$this->domain/admin/api/" . Context::$API_VERSION . '/graphql.json',
211+
method: 'POST',
212+
userAgent: "Shopify Admin API Library for PHP v$this->version",
213+
headers: [
214+
'Content-Type: application/json',
215+
'Content-Length: ' . strlen($queryToProxy),
216+
'Extra-Extra: hear_all_about_it',
217+
'X-Shopify-Access-Token: token'
218+
],
219+
body: $queryToProxy
220+
)
221+
]
222+
);
223+
224+
$response = $client->proxy(data: $queryToProxy, extraHeaders: $extraHeaders);
225+
$this->assertThat(
226+
$response,
227+
new HttpResponseMatcher(decodedBody: json_decode($this->querySuccessResponse, true))
228+
);
229+
}
193230
}

tests/UtilsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public function testGraphqlProxyFetchesDataWithJWT()
275275
url: "https://$this->domain/admin/api/" . Context::$API_VERSION . '/graphql.json',
276276
method: 'POST',
277277
headers: [
278-
'Content-Type: application/graphql',
278+
'Content-Type: application/json',
279279
'Content-Length: ' . strlen($this->testGraphqlQuery),
280280
'X-Shopify-Access-Token: token',
281281
],
@@ -312,7 +312,7 @@ public function testGraphqlProxyFetchesDataWithCookies()
312312
url: "https://$this->domain/admin/api/" . Context::$API_VERSION . '/graphql.json',
313313
method: 'POST',
314314
headers: [
315-
'Content-Type: application/graphql',
315+
'Content-Type: application/json',
316316
'Content-Length: ' . strlen($this->testGraphqlQuery),
317317
'X-Shopify-Access-Token: token',
318318
],

0 commit comments

Comments
 (0)