Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Support for AWS EventBridge for Shopify Webhooks #224

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
57 changes: 40 additions & 17 deletions src/Services/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
Expand Down
8 changes: 8 additions & 0 deletions tests/Services/ApiHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading