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 declarative webhooks processing #138

Merged
merged 4 commits into from
Aug 1, 2024
Merged

Conversation

lizkenyon
Copy link
Contributor

WHY are these changes introduced?

Fixes ShopifyAPI::Errors::NoWebhookHandler for mandatory webhooks

Adds the ability to process webhooks that have been declared in the shopify.app.toml

The shopify_app gem couples the creating of the webhook subscription with the processing of the webhook events.
To support declarative webhooks, the template needs to be able to handle webhook events that were not subscribed to via the gem.

WHAT is this pull request doing?

  • Creates new controllers to handle each of the webhook subscriptions
  • These controllers now handle what the registry.process function does
  1. Validate the webhook request is from Shopify
  2. Queues the correct job to process the webhook

####Questions

  • Should we create a new generator for shopify_app that generates a new controller + job?
  • We want to keep the ability to create and subscribe to webhooks via the API?

Checklist

Note: once this PR is merged, it becomes a new release for this template.

  • I have added/updated tests for this change
  • I have made changes to the README.md file and other related documentation, if applicable

The webhook registry in the package assumes that all webhooks to be processed will be defined in the config

It also couples subscribing to processing

This PR removes the registration loggic from the config.

It uses the provided webhook verification concern with new controllers
@lizkenyon lizkenyon requested a review from a team as a code owner July 18, 2024 15:16
shopify.app.toml Outdated
@@ -6,17 +6,17 @@ scopes = "write_products"
api_version = "2024-07"

[[webhooks.subscriptions]]
uri = "/api/webhooks/app_uninstalled"
uri = "/api/shopify_webhooks/app_uninstalled"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The shopify_app gem uses the /webhook route, so I have defined these routes as /shopify_webhooks.

We do have the ability to change the gem webhook route in the app config. So we could default that to something else and define these under /webhook

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd prefer to use /webhooks for the declarative hooks - this is the easiest and preferred way to set up webhooks, so I'd like to make it the easiest to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I misunderstood what config.webhook_jobs_namespace does. It only changes the namespace of the jobs, not the route.

But I figured out if we change the order of how we define the routes, it allows us to send both types of webhooks to /webhooks.

Copy link
Contributor

@sle-c sle-c left a comment

Choose a reason for hiding this comment

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

This works well with declarative webhooks.

This overwrites the existing webhook registration routes with explicit routes and controllers that handle the appropriate webhook registrations in the shopify.app.toml file.

The caveat is one needs to know that if they use config.webhooks for registering webhooks, they don't need to make a new controller and only need to add an active job corresponding to that webhook topic.

@@ -18,6 +18,12 @@
get :count
end
end
namespace :webhooks do
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we call out that routes will need to be added? Or can we make the topic generic so each one doesn't have to be explicitly added?

Copy link
Contributor Author

@lizkenyon lizkenyon Aug 1, 2024

Choose a reason for hiding this comment

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

I didn't see this comment, before merging, But I do have docs in the shopify_app gem. But I would also be good to add comments explaining this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each webhook needs a route, but the generators I created add the route for you.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh nice, then that's probably fine then, right?

@lizkenyon lizkenyon merged commit b73bd9e into main Aug 1, 2024
7 checks passed
@lizkenyon lizkenyon deleted the liz/declarative-webhooks branch August 1, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ShopifyAPI::Errors::NoWebhookHandler for mandatory webhooks
3 participants