Skip to content

Instantly share code, notes, and snippets.

@remy727
Created January 27, 2025 19:54
Show Gist options
  • Save remy727/269fabf10aa6b4bbb0677d421d2824c9 to your computer and use it in GitHub Desktop.
Save remy727/269fabf10aa6b4bbb0677d421d2824c9 to your computer and use it in GitHub Desktop.
Remove Shop-specific webhooks
# Shop-specific webhooks need to be deleted before migration into App-specific webhooks
# Unless they will be triggered twice
require "mongo"
require "shopify_api"
def remove_webhook(shopify_domain, access_token)
# Get webhook
query = <<~GRAPHQL
query {
webhookSubscriptions(first: 1, topics: [ORDERS_UPDATED]) {
edges {
node {
id
}
}
}
}
GRAPHQL
session = ShopifyAPI::Auth::Session.new(shop: shopify_domain, access_token: access_token)
client = ShopifyAPI::Clients::Graphql::Admin.new(session: session)
response = client.query(query: query)
webhook_id = response.body.dig("data", "webhookSubscriptions", "edges", 0, "node", "id")
mutation = <<~GRAPHQL
mutation webhookSubscriptionDelete($id: ID!) {
webhookSubscriptionDelete(id: $id) {
deletedWebhookSubscriptionId
userErrors {
field
message
}
}
}
GRAPHQL
response = client.query(query: mutation, variables: {
id: webhook_id
})
puts "shopify_domain: #{shopify_domain}, success"
rescue => e
puts "shopify_domain: #{shopify_domain}, error: #{e.message}"
end
def check
client = Mongo::Client.new("YOUR_DATABASE")
collection = client[:shopify_sessions]
collection.find.each do |document|
remove_webhook(document["shop"], document["accessToken"])
end
end
check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment