From 8c979071339a054c81171e59938a159db7f47c5b Mon Sep 17 00:00:00 2001 From: Mathew Hartley Date: Thu, 9 Nov 2023 10:04:15 +1300 Subject: [PATCH 1/4] Add support for metafield_namespaces in webhook registration This is an extension on https://github.com/Shopify/shopify-api-ruby/pull/1186, allowing users of the shopify_app ruby/rails library to register webhooks and expect them to return with metafields back. --- lib/shopify_app/managers/webhooks_manager.rb | 1 + test/shopify_app/managers/webhooks_manager_test.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/shopify_app/managers/webhooks_manager.rb b/lib/shopify_app/managers/webhooks_manager.rb index d02c71a2e..4ee17df67 100644 --- a/lib/shopify_app/managers/webhooks_manager.rb +++ b/lib/shopify_app/managers/webhooks_manager.rb @@ -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 diff --git a/test/shopify_app/managers/webhooks_manager_test.rb b/test/shopify_app/managers/webhooks_manager_test.rb index 43195c61f..99fa0d577 100644 --- a/test/shopify_app/managers/webhooks_manager_test.rb +++ b/test/shopify_app/managers/webhooks_manager_test.rb @@ -22,6 +22,7 @@ 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 @@ -40,6 +41,7 @@ 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 From 5e514186376de64957c7810c9f1416de698c1805 Mon Sep 17 00:00:00 2001 From: Mathew Hartley Date: Thu, 9 Nov 2023 11:17:04 +1300 Subject: [PATCH 2/4] Update docs and changelog --- CHANGELOG.md | 1 + docs/shopify_app/webhooks.md | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd301922f..3bac23b5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Unreleased * Bump `shopify_api` to include bugfix with mandatory webhooks + fixes for CI failures that prevented earlier release * Fixes bug with `WebhooksManager#recreate_webhooks!` where we failed to register topics in the registry[#1743](https://github.com/Shopify/shopify_app/pull/1704) * Allow embedded apps to provide a full URL to get redirected to, rather than defaulting to Shopify Admin [#1746](https://github.com/Shopify/shopify_app/pull/1746) +* Add support for metafield_namespaces in webhook registration [#1745](https://github.com/Shopify/shopify_app/pull/1745) 21.7.0 (Oct 12, 2023) ---------- diff --git a/docs/shopify_app/webhooks.md b/docs/shopify_app/webhooks.md index eed665c27..8d455461f 100644 --- a/docs/shopify_app/webhooks.md +++ b/docs/shopify_app/webhooks.md @@ -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. + +```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 From 557a1336123c258d545e0750c5d491ec3ba426a3 Mon Sep 17 00:00:00 2001 From: Mathew Hartley Date: Thu, 9 Nov 2023 11:18:38 +1300 Subject: [PATCH 3/4] Update tests to splat to keyword arguments to fix Mocha warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes `expected positional hash (…), but received keyword arguments (…)` for those method calls by splatting them into keyword arguments. --- test/shopify_app/managers/webhooks_manager_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/shopify_app/managers/webhooks_manager_test.rb b/test/shopify_app/managers/webhooks_manager_test.rb index 99fa0d577..50e5eca4d 100644 --- a/test/shopify_app/managers/webhooks_manager_test.rb +++ b/test/shopify_app/managers/webhooks_manager_test.rb @@ -25,7 +25,7 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase 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" }, @@ -44,7 +44,7 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase 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 = [ { From 532cbeccc32020a54ac5d1ce93d03e9c6bbb17fb Mon Sep 17 00:00:00 2001 From: Mathew Hartley Date: Fri, 15 Dec 2023 12:00:34 +1300 Subject: [PATCH 4/4] Update changelog and add shopify.dev reference --- CHANGELOG.md | 2 +- docs/shopify_app/webhooks.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bac23b5c..e5d96c4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ---------- @@ -11,7 +12,6 @@ Unreleased * Bump `shopify_api` to include bugfix with mandatory webhooks + fixes for CI failures that prevented earlier release * Fixes bug with `WebhooksManager#recreate_webhooks!` where we failed to register topics in the registry[#1743](https://github.com/Shopify/shopify_app/pull/1704) * Allow embedded apps to provide a full URL to get redirected to, rather than defaulting to Shopify Admin [#1746](https://github.com/Shopify/shopify_app/pull/1746) -* Add support for metafield_namespaces in webhook registration [#1745](https://github.com/Shopify/shopify_app/pull/1745) 21.7.0 (Oct 12, 2023) ---------- diff --git a/docs/shopify_app/webhooks.md b/docs/shopify_app/webhooks.md index 8d455461f..b8da43fa8 100644 --- a/docs/shopify_app/webhooks.md +++ b/docs/shopify_app/webhooks.md @@ -40,7 +40,7 @@ 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. +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|