From b1531b9cec44ad2301f436b546321ece3ded4961 Mon Sep 17 00:00:00 2001 From: Paco Castro Date: Fri, 5 May 2023 19:11:36 +0200 Subject: [PATCH] Support for Event topic names Event topic names will allow period as a separator and these need to be translated to GraphQL valid ENUM values --- CHANGELOG.md | 1 + lib/shopify_api/webhooks/registry.rb | 2 +- test/webhooks/registry_test.rb | 15 +++++++++++++++ test/webhooks/webhook_registration_queries.rb | 12 ++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a1a5785a..1ed3d3cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api - [#1140](https://github.com/Shopify/shopify-api-ruby/pull/1140) ⚠️ [Breaking] Reformat Http error messages to be JSON parsable. - [#1142](https://github.com/Shopify/shopify-api-ruby/issues/1142) Restore API version 2022-04, in alignment with [this](https://shopify.dev/changelog/action-required-support-for-api-version-2022-04-extended-to-june-30-2023) changelog notice. +- [#1150](https://github.com/Shopify/shopify-api-ruby/pull/1150) [Patch] Add support for Event topic names. ## 12.5.0 diff --git a/lib/shopify_api/webhooks/registry.rb b/lib/shopify_api/webhooks/registry.rb index 98e3eb92b..e85520f2f 100644 --- a/lib/shopify_api/webhooks/registry.rb +++ b/lib/shopify_api/webhooks/registry.rb @@ -136,7 +136,7 @@ def unregister(topic:, session:) def get_webhook_id(topic:, client:) fetch_id_query = <<~QUERY { - webhookSubscriptions(first: 1, topics: #{topic.gsub("/", "_").upcase}) { + webhookSubscriptions(first: 1, topics: #{topic.gsub(%r{/|\.}, "_").upcase}) { edges { node { id diff --git a/test/webhooks/registry_test.rb b/test/webhooks/registry_test.rb index 8242d5623..b806ff362 100644 --- a/test/webhooks/registry_test.rb +++ b/test/webhooks/registry_test.rb @@ -196,6 +196,21 @@ def test_get_webhook_id_success ) end + def test_get_webhook_id_success_for_event + stub_request(:post, @url) + .with(body: JSON.dump({ query: queries[:fetch_id_event_query], variables: nil })) + .to_return({ status: 200, body: JSON.dump(queries[:fetch_id_response]) }) + + webhook_id_response = ShopifyAPI::Webhooks::Registry.get_webhook_id( + topic: "domain.sub_domain.something_happened", + client: ShopifyAPI::Clients::Graphql::Admin.new(session: @session), + ) + assert_equal( + queries[:fetch_id_response]["data"]["webhookSubscriptions"]["edges"][0]["node"]["id"], + webhook_id_response, + ) + end + def test_get_webhook_id_not_found stub_request(:post, @url) .with(body: JSON.dump({ query: queries[:fetch_id_query], variables: nil })) diff --git a/test/webhooks/webhook_registration_queries.rb b/test/webhooks/webhook_registration_queries.rb index ae3fbf302..1085fd13b 100644 --- a/test/webhooks/webhook_registration_queries.rb +++ b/test/webhooks/webhook_registration_queries.rb @@ -381,6 +381,18 @@ def queries "message" => "some error", ], }, + fetch_id_event_query: + <<~QUERY, + { + webhookSubscriptions(first: 1, topics: DOMAIN_SUB_DOMAIN_SOMETHING_HAPPENED) { + edges { + node { + id + } + } + } + } + QUERY delete_query: <<~QUERY, mutation webhookSubscription {