Skip to content

Commit

Permalink
Add support for filters in webhook registration
Browse files Browse the repository at this point in the history
To support passing a filter to webhook registrations, the filter
parameter is needed.
  • Loading branch information
kennyadsl committed Nov 20, 2024
1 parent db9dcd9 commit 2abf998
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Unreleased
----------

- Add support for filters in webhook registration [1923](https://github.com/Shopify/shopify_app/pull/1923)
- Make `ShopifyApp.configuration.scope` default to empty list `[]` [1913](https://github.com/Shopify/shopify_app/pull/1913)

22.4.0 (August 22, 2024)
Expand Down
16 changes: 15 additions & 1 deletion docs/shopify_app/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ ShopifyApp.configure do |config|
config.webhooks = [
{
topic: 'orders/create',
path: 'api/webhooks/order_create',
path: 'api/webhooks/orders_create',
metafield_namespaces: ['app-namespace'],
},
]
end
```

If you need to filter by webhook fields, you can register a webhook with a `filter` parameter. The documentation for Webhook filters can be found [here](https://shopify.dev/docs/apps/build/webhooks/customize/filters).

```ruby
ShopifyApp.configure do |config|
config.webhooks = [
{
topic: 'products/update',
path: 'api/webhooks/products_update',
filter: "variants.price:>=10.00",
},
]
end
```

If you'd rather implement your own controller then you'll want to use the [`ShopifyApp::WebhookVerification`](/lib/shopify_app/controller_concerns/webhook_verification.rb) module to verify your webhooks, example:

```ruby
Expand Down
1 change: 1 addition & 0 deletions lib/shopify_app/managers/webhooks_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def add_registrations
path: webhook_path,
handler: delivery_method == :http ? webhook_job_klass(webhook_path) : nil,
fields: attributes[:fields],
filter: attributes[:filter],
metafield_namespaces: attributes[:metafield_namespaces],
)
end
Expand Down
27 changes: 27 additions & 0 deletions test/shopify_app/managers/webhooks_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase
handler: OrdersUpdatedJob,
fields: nil,
metafield_namespaces: nil,
filter: nil,
}

ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once
Expand All @@ -42,6 +43,7 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase
handler: OrdersUpdatedJob,
fields: nil,
metafield_namespaces: nil,
filter: nil,
}

ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once
Expand All @@ -57,6 +59,31 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase
ShopifyApp::WebhooksManager.add_registrations
end

test "#add_registrations includes filters" do
expected_hash = {
topic: "orders/updated",
delivery_method: :http,
path: "/webhooks/orders_updated",
handler: OrdersUpdatedJob,
fields: nil,
metafield_namespaces: nil,
filter: "id:*",
}

ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once
ShopifyApp.configure do |config|
config.webhooks = [
{
topic: "orders/updated",
address: "https://some.domain.over.the.rainbow.com/webhooks/orders_updated",
filter: "id:*",
},
]
end

ShopifyApp::WebhooksManager.add_registrations
end

test "#add_registrations raises an error when missing path and address" do
ShopifyApp.configure do |config|
config.webhooks = [
Expand Down

0 comments on commit 2abf998

Please sign in to comment.