diff --git a/src/Services/ApiHelper.php b/src/Services/ApiHelper.php index 16a2db68..3341240d 100644 --- a/src/Services/ApiHelper.php +++ b/src/Services/ApiHelper.php @@ -366,34 +366,57 @@ public function getWebhooks(array $params = []): ResponseAccess */ public function createWebhook(array $payload): ResponseAccess { - $query = ' - mutation webhookSubscriptionCreate( - $topic: WebhookSubscriptionTopic!, - $webhookSubscription: WebhookSubscriptionInput! - ) { - webhookSubscriptionCreate( - topic: $topic - webhookSubscription: $webhookSubscription + $addressType = str_starts_with($payload['address'], 'arn:') ? 'arn' : 'callbackUrl'; + if ($addressType === 'arn') { + $query = ' + mutation eventBridgeWebhookSubscriptionCreate( + $topic: WebhookSubscriptionTopic!, + $webhookSubscription: EventBridgeWebhookSubscriptionInput! ) { - userErrors { - field - message + eventBridgeWebhookSubscriptionCreate( + topic: $topic, + webhookSubscription: $webhookSubscription + ) { + userErrors { + field + message + } + webhookSubscription { + id + topic + } } - webhookSubscription { - id - topic + } + '; + } else { + $query = ' + mutation webhookSubscriptionCreate( + $topic: WebhookSubscriptionTopic!, + $webhookSubscription: WebhookSubscriptionInput! + ) { + webhookSubscriptionCreate( + topic: $topic + webhookSubscription: $webhookSubscription + ) { + userErrors { + field + message + } + webhookSubscription { + id + topic + } } } + '; } - '; - // Change REST-format topics ("resource/event") // to GraphQL-format topics ("RESOURCE_EVENT"), for pre-v17 compatibility $topic = Util::getGraphQLWebhookTopic($payload['topic']); $variables = [ 'topic' => $topic, 'webhookSubscription' => [ - 'callbackUrl' => $payload['address'], + $addressType => $payload['address'], 'format' => 'JSON', ], ]; diff --git a/tests/Services/ApiHelperTest.php b/tests/Services/ApiHelperTest.php index 3191c054..04bafa22 100644 --- a/tests/Services/ApiHelperTest.php +++ b/tests/Services/ApiHelperTest.php @@ -203,8 +203,16 @@ public function testCreateWebhook(): void 'topic' => 'ORDERS_CREATE', 'address' => 'https://localhost/webhook/orders-create', ]); + + $dataArn = $shop->apiHelper()->createWebhook([ + 'topic' => 'ORDERS_CREATE', + 'address' => 'arn:aws:events:us-east-1::event-source/aws.partner/shopify.com/client_id_x/EventBridgeSource', + ]); + $this->assertInstanceOf(ResponseAccess::class, $data); + $this->assertInstanceOf(ResponseAccess::class, $dataArn); $this->assertSame('ORDERS_CREATE', $data['data']['webhookSubscriptionCreate']['topic']); + $this->assertSame('ORDERS_CREATE', $dataArn['data']['webhookSubscriptionCreate']['topic']); } public function testDeleteWebhook(): void