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

Cannot subscribe to a webhook after unregistering to it #1222

Closed
BaggioGiacomo opened this issue Sep 27, 2023 · 10 comments
Closed

Cannot subscribe to a webhook after unregistering to it #1222

BaggioGiacomo opened this issue Sep 27, 2023 · 10 comments
Assignees

Comments

@BaggioGiacomo
Copy link

BaggioGiacomo commented Sep 27, 2023

Issue summary

I'm trying to register to a webhook (customers/create) after unregistering to it.

These are my webhooks on the ShopifyApp.configuration:

config.webhooks = [
    { topic: "app_subscriptions/update", path: "api/webhooks/app_subscriptions_update" },
    { topic: "shop/update", path: "api/webhooks/shop_update" },
    {
      topic: "customers/update",
      path: "api/webhooks/customers_update",
    },
    {
      topic: "bulk_operations/finish",
      path: "api/webhooks/bulk_operations_finish",
    },
    { topic: "customers/create", path: "api/webhooks/customers_create" },
    # After a store owner uninstalls your app, Shopify invokes the APP_UNINSTALLED webhook
    # to let your app know.
    { topic: "app/uninstalled", path: "api/webhooks/app_uninstalled" },
  ]

Here's the code to give you more context:

# Unregistering some webhooks

WEBHOOK_TOPICS_TO_DELETE = [
    "customers/update",
    "customers/create",
    "bulk_operations/finish",
  ]

def unregister_webhooks
    WEBHOOK_TOPICS_TO_DELETE.each do |topic|
      ShopifyAPI::Webhooks::Registry.unregister(
        topic: topic,
        session: ShopifyAPI::Context.active_session,
      )
    end
  end

If I call the recreate_webhooks! method from the ShopifyApp, I don't get any errors but If I query the webhooksSubscription I get an empty array (the method just deletes my webhooks subscriptions without re-creating them)

If I call the create_webhooks method from the ShopifyApp, I get this Sorbet error: /Users/jack/.rvm/gems/ruby-3.1.2/gems/sorbet-runtime-0.5.11001/lib/types/_types.rb:222:in must': Passed nil into T.must (TypeError)`

I get the same error if I call:

ShopifyAPI::Webhooks::Registry.add_registration(topic: "customers/create", delivery_method: :http, handler: CustomersCreateJob, path: 'api/webhooks/customers_create')

ShopifyAPI::Webhooks::Registry.register_all(session: shop_session)

as the example here

I think the problem is here since I've tried to manually instantiate a Registrations::Http object (since my webhooks have httpas delivery_method) and it works. The problem might be the registry instance variable.

Obviously I pass a session that is not nil!

  • shopify_api gem version: 13.1.0
  • shopify_app gem version: 21.6.0
  • Rails version: 7.0.7.1
  • Ruby version: 3.1.2
  • Operating system: macOS Ventura 13.4 (22F66)

Expected behavior

  • I expect to get all the my webhooks when running the webhooksSubscription query
  • I expect to not get errors while trying to registrate/create my webhooks

Actual behavior

  • I get an empty array when running the webhooksSubscription query
  • I get errors while trying to registrate/create my webhooks
@lizkenyon
Copy link
Contributor

Hi there 👋

Thank you for flagging this!
I believe this will have been resolved with this PR.

@BaggioGiacomo
Copy link
Author

No, the error persists.
Calling ShopifyApp::WebhooksManager.recreate_webhooks!(session:) will empty your webhook subscriptions (fetched from API).

I now have a question for you. Which is the best way to re-subscribe to a webhook?
(Let's re-open this issue)

@lizkenyon
Copy link
Contributor

Hi there thanks for flagging👋

Could you confirm what version of the shopify_app gem you are using?

@lizkenyon lizkenyon reopened this Jan 22, 2024
@BaggioGiacomo
Copy link
Author

Hi there, I get these errors using the shopify_app -> 21.6.0 and the shopify_api -> 13.3.1
If i use the latest versions, the recreate_webhooks! method give me the same error as create_webhooks: /Users/jack/.rvm/gems/ruby-3.1.2/gems/sorbet-runtime-0.5.11001/lib/types/_types.rb:222:in must': Passed nil into T.must (TypeError)

I'm testing this using the rails console ❗

@sle-c sle-c self-assigned this Feb 22, 2024
@sle-c sle-c removed their assignment Mar 4, 2024
@lizkenyon lizkenyon self-assigned this Mar 8, 2024
@lizkenyon
Copy link
Contributor

Hi there 👋

I am unable to replicate this issue.

I am able to destroy my webhook subscriptions, call recreate_webhooks and it successfully recreates my webhook subscriptions.

If this is still an issue for you could you please provide an example minimal repository that I am able to clone and run that is able to replicate the issue

@BaggioGiacomo
Copy link
Author

BaggioGiacomo commented Mar 11, 2024

Hi @lizkenyon !

Calling ShopifyApp::WebhooksManager.recreate_webhooks!(session: ShopifyAPI::Context.active_session) works if it is called from the app. It deletes all the webhook subscription and throws an error if called from the rails console

It deletes all the webhook subscription and throws an error if called from the rails console
I think this happens because the @registry is not defined when using the rails console

To reproduce this issue:

  1. npm init @shopify/app@latest -- --template ruby
  2. cd web && bundle
  3. rails db:create && rails db:migrate
  4. npm run dev
  5. Do the basic config
  6. Install the app in a dev store
  7. Open the rails console e run this:
Shop.first.with_shopify_session do
  ShopifyApp::WebhooksManager.recreate_webhooks!(session: ShopifyAPI::Context.active_session)
end
  1. It will throw this error: Passed nil into T.must (TypeError) and the APP_UNINSTALLED webhook subscription is not present anymore

@artyrcheek
Copy link

Same issue here

@lizkenyon
Copy link
Contributor

Hi there 👋

You are getting this error because the HOST env var is not set when running to the rails console.

You will need to set the HOST, SHOPIFY_API_KEY and SHOPIFY_API_SECRET when starting the console for the app to be set up correctly.

SHOPIFY_API_KEY="fake" SHOPIFY_API_SECRET="otherfake" HOST="fa-dual-ict-our.example.com" rails console

If you are using the rails console a lot you may want to look into setting up an .env file to hold the variables.

@BaggioGiacomo
Copy link
Author

I confirm that it doesn't throw the error anymore. I still have the other error: when I call ShopifyApp::WebhooksManager.recreate_webhooks!(session: ShopifyAPI::Context.active_session) (from the app or from the rails console) my webhook subscriptions are empty.

This is what I get after calling that method:

image

Is this the correct way to unsubscribe to webhooks?

  def unregister_webhooks
    WEBHOOK_TOPICS_TO_DELETE.each do |topic|
      ShopifyAPI::Webhooks::Registry.unregister(
        topic: topic,
        session: ShopifyAPI::Context.active_session,
      )
    end
  end

@BaggioGiacomo
Copy link
Author

This method recreates all the webhook subscriptions correctly:

def subscribe_to_webhooks
  ShopifyApp::WebhooksManager.add_registrations
  ShopifyAPI::Webhooks::Registry.register_all(session: ShopifyAPI::Context.active_session)
end

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

No branches or pull requests

4 participants