Skip to content

Commit

Permalink
Add bulk operation related methods (create, get, cancel)
Browse files Browse the repository at this point in the history
  • Loading branch information
xHeaven committed Dec 3, 2024
1 parent dcc6297 commit 0a9b16c
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/GraphQLClientMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,42 @@ public function mutate(string $query, array $variables, bool $withExtensions = f
);
}

public function getCurrentBulkOperation(): GraphQLClientTransformer
{
$response = $this->makeGetCurrentBulkOperationRequest();

/** @var array $response */
$response = data_get($response, 'data.currentBulkOperation');

return new GraphQLClientTransformer(
data: $response
);
}

public function createBulkOperation(string $query): GraphQLClientTransformer
{
$response = $this->makeCreateBulkOperationRequest($query);

/** @var array $response */
$response = data_get($response, 'data.bulkOperationRunQuery');

return new GraphQLClientTransformer(
data: $response
);
}

public function cancelBulkOperation(): GraphQLClientTransformer
{
$response = $this->makeCancelBulkOperationRequest();

/** @var array $response */
$response = data_get($response, 'data.bulkOperationCancel');

return new GraphQLClientTransformer(
data: $response
);
}

#[ArrayShape([
'requestedQueryCost' => 'float|int|null',
'actualQueryCost' => 'float|int|null',
Expand Down Expand Up @@ -157,6 +193,61 @@ private function makeMutationRequest(string $query, array $variables, bool $with
return $response;
}

/**
* @throws ClientNotInitializedException
* @throws ClientRequestFailedException
*/
private function makeGetCurrentBulkOperationRequest(): array
{
throw_if($this->connector === null, ClientNotInitializedException::class);

$response = $this->connector->create()->currentBulkOperation();

throw_if($response->failed(), ClientRequestFailedException::class, $response);

return Arr::wrap($response->json());
}

/**
* @throws ClientNotInitializedException
* @throws ClientRequestFailedException
*/
private function makeCreateBulkOperationRequest(string $query): array
{
throw_if($this->connector === null, ClientNotInitializedException::class);

$response = $this->connector->create()->createBulkOperation($query);

throw_if($response->failed(), ClientRequestFailedException::class, $response);

return Arr::wrap($response->json());
}

/**
* @throws ClientNotInitializedException
* @throws ClientRequestFailedException
*/
private function makeCancelBulkOperationRequest(): array
{
throw_if($this->connector === null, ClientNotInitializedException::class);

/** @var array $currentBulkOperation */
$currentBulkOperation = data_get($this->makeGetCurrentBulkOperationRequest(), 'data.currentBulkOperation');
/** @var ?string $currentBulkOperationId */
$currentBulkOperationId = data_get($currentBulkOperation, 'id');
/** @var ?string $currentBulkOperationStatus */
$currentBulkOperationStatus = data_get($currentBulkOperation, 'status');
$doesntHaveIdOrNotCancellable = $currentBulkOperationId === null || !in_array($currentBulkOperationStatus, ['CREATED', 'RUNNING']);

throw_if($doesntHaveIdOrNotCancellable, ClientRequestFailedException::class, 'There is no bulk operation to cancel.');

$response = $this->connector->create()->cancelBulkOperation($currentBulkOperationId);

throw_if($response->failed(), ClientRequestFailedException::class, $response);

return Arr::wrap($response->json());
}

private function ispectResponse(
array $response,
): void {
Expand Down
33 changes: 33 additions & 0 deletions src/Integrations/Requests/CancelBulkOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Luminarix\Shopify\GraphQLClient\Integrations\Requests;

class CancelBulkOperation extends BaseRequest
{
public function __construct(
public string $id,
) {}

protected function defaultBody(): array
{
$bulkOperation = <<<GRAPHQL
mutation {
bulkOperationCancel(id: "{$this->id}") {
bulkOperation {
status
}
userErrors {
field
message
}
}
}
GRAPHQL;

return [
'query' => $bulkOperation,
];
}
}
38 changes: 38 additions & 0 deletions src/Integrations/Requests/CreateBulkOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Luminarix\Shopify\GraphQLClient\Integrations\Requests;

class CreateBulkOperation extends BaseRequest
{
public function __construct(
public string $graphqlQuery,
) {}

protected function defaultBody(): array
{
$bulkOperation = <<<GRAPHQL
mutation {
bulkOperationRunQuery(
query: """
{$this->graphqlQuery}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
GRAPHQL;

return [
'query' => $bulkOperation,
];
}
}
31 changes: 31 additions & 0 deletions src/Integrations/Requests/CurrentBulkOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Luminarix\Shopify\GraphQLClient\Integrations\Requests;

class CurrentBulkOperation extends BaseRequest
{
protected function defaultBody(): array
{
$currentBulkOperationQuery = <<<'GRAPHQL'
{
currentBulkOperation {
id
status
errorCode
createdAt
completedAt
objectCount
fileSize
url
partialDataUrl
}
}
GRAPHQL;

return [
'query' => $currentBulkOperationQuery,
];
}
}
24 changes: 24 additions & 0 deletions src/Integrations/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Luminarix\Shopify\GraphQLClient\Integrations;

use Luminarix\Shopify\GraphQLClient\Integrations\Requests\CancelBulkOperation;
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\CreateBulkOperation;
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\CurrentBulkOperation;
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\Mutation;
use Luminarix\Shopify\GraphQLClient\Integrations\Requests\Query;
use Saloon\Http\Connector;
Expand Down Expand Up @@ -31,4 +34,25 @@ public function mutation(string $graphqlQuery, array $variables, bool $detailedC
new Mutation($graphqlQuery, $variables, $detailedCost)
);
}

public function currentBulkOperation(): Response
{
return $this->connector->send(
new CurrentBulkOperation
);
}

public function createBulkOperation(string $graphqlQuery): Response
{
return $this->connector->send(
new CreateBulkOperation($graphqlQuery)
);
}

public function cancelBulkOperation(string $id): Response
{
return $this->connector->send(
new CancelBulkOperation($id)
);
}
}

0 comments on commit 0a9b16c

Please sign in to comment.