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

refactor where mandatory webhooks are skipped #1249

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions lib/shopify_api/webhooks/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class << self
metafield_namespaces: T.nilable(T::Array[String])).void
end
def add_registration(topic:, delivery_method:, path:, handler: nil, fields: nil, metafield_namespaces: nil)
return if mandatory_webhook_topic?(topic)

@registry[topic] = case delivery_method
when :pub_sub
Registrations::PubSub.new(topic: topic, path: path, fields: fields,
Expand Down Expand Up @@ -56,6 +54,8 @@ def clear
).returns(RegisterResult)
end
def register(topic:, session:)
return mandatory_registration_result(topic) if mandatory_webhook_topic?(topic)

registration = @registry[topic]

unless registration
Expand Down Expand Up @@ -83,6 +83,13 @@ def register(topic:, session:)
RegisterResult.new(topic: topic, success: registered, body: register_body)
end

sig do
params(topic: String).returns(RegisterResult)
end
def mandatory_registration_result(topic)
RegisterResult.new(topic: topic, success: true, body: nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be confusing to return that the mandatory webhooks are successfully registered, when they have they have not actually been registered? i.e. will this further confuse people into thinking they can register these webhook topics via the API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to an unfortunate coupling of this registry and the async job processor, we still need to add mandatory topics to the @registry. You and I had the same thought, and in my last PR that caused this bug used a similar approach to the one you are suggesting.

We'll still need to add mandatory hooks to the @registry even if we don't explicitly register to them with the API. This is a gross boundary and I'd love to explore streamlining webhook registrations so we don't have to have this silly flow :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that we need to add them to the registry currently. But do we need to return that we successfully registered them?

Could we skip pushing them to the array of topics we have registered? Or is there a way to return that they have not been successfully registered?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do we think removing them from the response of registered topics would break something else?

end

sig do
params(
session: Auth::Session,
Expand Down
11 changes: 3 additions & 8 deletions test/webhooks/registry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,12 @@ def test_get_webhook_id_with_graphql_errors
def test_registrations_to_mandatory_topics_are_ignored
ShopifyAPI::Webhooks::Registry.clear

ShopifyAPI::Webhooks::Registrations::Http.expects(:new).never
ShopifyAPI::Clients::Graphql::Admin.expects(:new).never

ShopifyAPI::Webhooks::Registry::MANDATORY_TOPICS.each do |topic|
ShopifyAPI::Webhooks::Registry.add_registration(
ShopifyAPI::Webhooks::Registry.register(
topic: topic,
delivery_method: :http,
path: "some_path_under_the_rainbow",
handler: TestHelpers::FakeWebhookHandler.new(
lambda do |topic, shop, body|
end,
),
session: @session,
)
end
end
Expand Down
Loading