class ShopifyAPI::Webhooks::Registry

Constants

MANDATORY_TOPICS

Public Class Methods

add_registration(topic:, delivery_method:, path:, handler: nil, fields: nil, metafield_namespaces: nil) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 24
def add_registration(topic:, delivery_method:, path:, handler: nil, fields: nil, metafield_namespaces: nil)
  @registry[topic] = case delivery_method
  when :pub_sub
    Registrations::PubSub.new(topic: topic, path: path, fields: fields,
      metafield_namespaces: metafield_namespaces)
  when :event_bridge
    Registrations::EventBridge.new(topic: topic, path: path, fields: fields,
      metafield_namespaces: metafield_namespaces)
  when :http
    unless handler
      raise Errors::InvalidWebhookRegistrationError, "Cannot create an Http registration without a handler."
    end

    Registrations::Http.new(topic: topic, path: path, handler: handler,
      fields: fields, metafield_namespaces: metafield_namespaces)
  else
    raise Errors::InvalidWebhookRegistrationError,
      "Unsupported delivery method #{delivery_method}. Allowed values: {:http, :pub_sub, :event_bridge}."
  end
end
clear() click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 46
def clear
  @registry.clear
end
get_webhook_id(topic:, client:) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 160
        def get_webhook_id(topic:, client:)
          fetch_id_query = <<~QUERY
            {
              webhookSubscriptions(first: 1, topics: #{topic.gsub(%r{/|\.}, "_").upcase}) {
                edges {
                  node {
                    id
                  }
                }
              }
            }
          QUERY

          fetch_id_response = client.query(query: fetch_id_query, response_as_struct: false)
          raise Errors::WebhookRegistrationError,
            "Failed to fetch webhook from Shopify" unless fetch_id_response.ok?
          body = T.cast(fetch_id_response.body, T::Hash[String, T.untyped])
          errors = body["errors"] || {}
          raise Errors::WebhookRegistrationError,
            "Failed to fetch webhook from Shopify: #{errors[0]["message"]}" unless errors.empty?

          edges = body.dig("data", "webhookSubscriptions", "edges") || {}
          return nil if edges.empty?

          edges[0]["node"]["id"].to_s
        end
mandatory_registration_result(topic) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 89
def mandatory_registration_result(topic)
  RegisterResult.new(
    topic: topic,
    success: false,
    body: "Mandatory webhooks are to be registered in the partners dashboard",
  )
end
process(request) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 187
        def process(request)
          raise Errors::InvalidWebhookError, "Invalid webhook HMAC." unless Utils::HmacValidator.validate(request)

          handler = @registry[request.topic]&.handler

          unless handler
            raise Errors::NoWebhookHandler, "No webhook handler found for topic: #{request.topic}."
          end

          if handler.is_a?(WebhookHandler)
            handler.handle(data: WebhookMetadata.new(topic: request.topic, shop: request.shop,
              body: request.parsed_body, api_version: request.api_version, webhook_id: request.webhook_id))
          else
            handler.handle(topic: request.topic, shop: request.shop, body: request.parsed_body)
            warning = <<~WARNING
              DEPRECATED: Use ShopifyAPI::Webhooks::WebhookHandler#handle instead of
              ShopifyAPI::Webhooks::Handler#handle.
              https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/webhooks.md#create-a-webhook-handler
            WARNING
            ShopifyAPI::Logger.deprecated(warning, "15.0.0")
          end
        end
register(topic:, session:) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 56
def register(topic:, session:)
  return mandatory_registration_result(topic) if mandatory_webhook_topic?(topic)

  registration = @registry[topic]

  unless registration
    raise Errors::InvalidWebhookRegistrationError, "Webhook topic #{topic} has not been added to the registry."
  end

  client = Clients::Graphql::Admin.new(session: session)
  register_check_result = webhook_registration_needed?(client, registration)

  registered = true
  register_body = nil

  if register_check_result[:must_register]
    register_body = send_register_request(
      client,
      registration,
      register_check_result[:webhook_id],
    )
    registered = registration_sucessful?(
      register_body,
      registration.mutation_name(register_check_result[:webhook_id]),
    )
  end

  RegisterResult.new(topic: topic, success: registered, body: register_body)
end
register_all(session:) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 102
def register_all(session:)
  topics = @registry.keys
  result = T.let([], T::Array[RegisterResult])
  topics.each do |topic|
    register_response = register(
      topic: topic,
      session: session,
    )
    result.push(register_response)
  end
  result
end
unregister(topic:, session:) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 121
        def unregister(topic:, session:)
          return { "response": nil } if mandatory_webhook_topic?(topic)

          client = Clients::Graphql::Admin.new(session: session)

          webhook_id = get_webhook_id(topic: topic, client: client)
          return {} if webhook_id.nil?

          delete_mutation = <<~MUTATION
            mutation webhookSubscription {
              webhookSubscriptionDelete(id: "#{webhook_id}") {
                userErrors {
                  field
                  message
                }
                deletedWebhookSubscriptionId
              }
            }
          MUTATION

          delete_response = client.query(query: delete_mutation, response_as_struct: false)
          raise Errors::WebhookRegistrationError,
            "Failed to delete webhook from Shopify" unless delete_response.ok?
          result = T.cast(delete_response.body, T::Hash[String, T.untyped])
          errors = result["errors"] || {}
          raise Errors::WebhookRegistrationError,
            "Failed to delete webhook from Shopify: #{errors[0]["message"]}" unless errors.empty?
          user_errors = result.dig("data", "webhookSubscriptionDelete", "userErrors") || {}
          raise Errors::WebhookRegistrationError,
            "Failed to delete webhook from Shopify: #{user_errors[0]["message"]}" unless user_errors.empty?
          result
        end

Private Class Methods

mandatory_webhook_topic?(topic) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 252
def mandatory_webhook_topic?(topic)
  MANDATORY_TOPICS.include?(topic)
end
registration_sucessful?(body, mutation_name) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 245
def registration_sucessful?(body, mutation_name)
  !body.dig("data", mutation_name, "webhookSubscription").nil?
end
send_register_request(client, registration, webhook_id) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 235
def send_register_request(client, registration, webhook_id)
  register_response = client.query(query: registration.build_register_query(webhook_id: webhook_id),
    response_as_struct: false)

  raise Errors::WebhookRegistrationError, "Failed to register webhook with Shopify" unless register_response.ok?

  T.cast(register_response.body, T::Hash[String, T.untyped])
end
webhook_registration_needed?(client, registration) click to toggle source
# File lib/shopify_api/webhooks/registry.rb, line 218
def webhook_registration_needed?(client, registration)
  check_response = client.query(query: registration.build_check_query, response_as_struct: false)
  raise Errors::WebhookRegistrationError,
    "Failed to check if webhook was already registered" unless check_response.ok?
  parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
  must_register = parsed_check_result[:current_address] != registration.callback_address

  { webhook_id: parsed_check_result[:webhook_id], must_register: must_register }
end