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

Add support for metafield_namespaces in webhook registration #1745

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Unreleased
----------
* Fix `add_webhook` generator to create the webhook jobs under the correct directory[#1748](https://github.com/Shopify/shopify_app/pull/1748)
* Add support for metafield_namespaces in webhook registration [#1745](https://github.com/Shopify/shopify_app/pull/1745)

21.8.1 (December 6, 2023)
----------
Expand Down
14 changes: 14 additions & 0 deletions docs/shopify_app/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ ShopifyApp.configure do |config|
end
```

If you need to read metafields, you can pass in the `metafield_namespaces` parameter in `config/webhooks`. Note if you are also using the `fields` parameter you will need to add `metafields` into that as well. Shopify documentation on metafields in webhooks can be found [here](https://shopify.dev/docs/api/admin-rest/2023-10/resources/webhook#resource-object).

```ruby
ShopifyApp.configure do |config|
config.webhooks = [
{
topic: 'orders/create',
path: 'webhooks/order_create',
metafield_namespaces: ['app-namespace'],
},
]
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],
metafield_namespaces: attributes[:metafield_namespaces],
)
end
end
Expand Down
6 changes: 4 additions & 2 deletions test/shopify_app/managers/webhooks_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase
path: "webhooks/orders_updated",
handler: OrdersUpdatedJob,
fields: nil,
metafield_namespaces: nil,
}

ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(expected_hash).once
ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once
ShopifyApp.configure do |config|
config.webhooks = [
{ topic: "orders/updated", path: "webhooks/orders_updated" },
Expand All @@ -40,9 +41,10 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase
path: "/webhooks/orders_updated",
handler: OrdersUpdatedJob,
fields: nil,
metafield_namespaces: nil,
}

ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(expected_hash).once
ShopifyAPI::Webhooks::Registry.expects(:add_registration).with(**expected_hash).once
ShopifyApp.configure do |config|
config.webhooks = [
{
Expand Down