Skip to content

Commit f13cd29

Browse files
committed
Implement ability to get a bulk operation by id
1 parent 88b7849 commit f13cd29

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/GraphQLClientMethods.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ public function mutate(string $query, array $variables, bool $withExtensions = f
8383
);
8484
}
8585

86+
public function getBulkOperation(int $id): GraphQLClientTransformer
87+
{
88+
$response = $this->makeGetBulkOperationRequest($id);
89+
90+
/** @var ?array $response */
91+
$response = data_get($response, 'data.node', []);
92+
93+
return new GraphQLClientTransformer(
94+
data: $response ?? []
95+
);
96+
}
97+
8698
public function getCurrentBulkOperation(): GraphQLClientTransformer
8799
{
88100
$response = $this->makeGetCurrentBulkOperationRequest();
@@ -203,6 +215,17 @@ private function makeMutationRequest(string $query, array $variables, bool $with
203215
return $response;
204216
}
205217

218+
private function makeGetBulkOperationRequest(int $id): array
219+
{
220+
throw_if($this->connector === null, ClientNotInitializedException::class);
221+
222+
$response = $this->connector->create()->getBulkOperation($id);
223+
224+
throw_if($response->failed(), ClientRequestFailedException::class, $response);
225+
226+
return Arr::wrap($response->json());
227+
}
228+
206229
/**
207230
* @throws ClientNotInitializedException
208231
* @throws ClientRequestFailedException
@@ -237,12 +260,17 @@ private function makeCreateBulkOperationRequest(string $query): array
237260
* @throws ClientNotInitializedException
238261
* @throws ClientRequestFailedException
239262
*/
240-
private function makeCancelBulkOperationRequest(): array
263+
private function makeCancelBulkOperationRequest(?int $id = null): array
241264
{
242265
throw_if($this->connector === null, ClientNotInitializedException::class);
243266

244267
/** @var array $currentBulkOperation */
245-
$currentBulkOperation = data_get($this->makeGetCurrentBulkOperationRequest(), 'data.currentBulkOperation');
268+
$currentBulkOperation = when(
269+
condition: $id !== null,
270+
// @phpstan-ignore argument.type
271+
value: data_get($this->makeGetBulkOperationRequest($id), 'data.node'),
272+
default: data_get($this->makeGetCurrentBulkOperationRequest(), 'data.currentBulkOperation'),
273+
);
246274
/** @var ?string $currentBulkOperationId */
247275
$currentBulkOperationId = data_get($currentBulkOperation, 'id');
248276
/** @var ?string $currentBulkOperationStatus */
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Luminarix\Shopify\GraphQLClient\Integrations\Requests;
6+
7+
class BulkOperation extends BaseRequest
8+
{
9+
public function __construct(
10+
public int $id,
11+
) {}
12+
13+
protected function defaultBody(): array
14+
{
15+
$currentBulkOperationQuery = <<<GRAPHQL
16+
query {
17+
node(id: "gid://shopify/BulkOperation/{$this->id}") {
18+
... on BulkOperation {
19+
completedAt
20+
createdAt
21+
errorCode
22+
fileSize
23+
objectCount
24+
partialDataUrl
25+
query
26+
rootObjectCount
27+
status
28+
type
29+
url
30+
}
31+
}
32+
}
33+
GRAPHQL;
34+
35+
return [
36+
'query' => $currentBulkOperationQuery,
37+
];
38+
}
39+
}

src/Integrations/ShopifyResource.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Luminarix\Shopify\GraphQLClient\Integrations;
66

7+
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\BulkOperation;
78
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\CancelBulkOperation;
89
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\CreateBulkOperation;
910
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\CurrentBulkOperation;
@@ -35,6 +36,13 @@ public function mutation(string $graphqlQuery, array $variables, bool $detailedC
3536
);
3637
}
3738

39+
public function getBulkOperation(int $id): Response
40+
{
41+
return $this->connector->send(
42+
new BulkOperation($id)
43+
);
44+
}
45+
3846
public function currentBulkOperation(): Response
3947
{
4048
return $this->connector->send(

0 commit comments

Comments
 (0)