Class: ShopifyGraphql::Webhook

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/shopify_graphql/resources/webhook.rb

Constant Summary collapse

ALL_WEBHOOKS_QUERY =
<<~GRAPHQL
  query {
    webhookSubscriptions(first: 250) {
      edges {
        node {
          id
          topic
          endpoint {
            ... on WebhookHttpEndpoint {
              callbackUrl
            }
          }
        }
      }
    }
  }
GRAPHQL
CREATE_WEBHOOK_MUTATION =
<<~GRAPHQL
  mutation($topic: WebhookSubscriptionTopic!, $webhookSubscription: WebhookSubscriptionInput!) {
    webhookSubscriptionCreate(topic: $topic, webhookSubscription: $webhookSubscription) {
      webhookSubscription {
        id
      }
      userErrors {
        field
        message
      }
    }
  }
GRAPHQL
DELETE_WEBHOOK_MUTATION =
<<~GRAPHQL
  mutation($id: ID!) {
    webhookSubscriptionDelete(id: $id) {
      deletedWebhookSubscriptionId
      userErrors {
        field
        message
      }
    }
  }
GRAPHQL

Class Method Summary collapse

Class Method Details

.allObject



50
51
52
53
54
55
# File 'lib/shopify_graphql/resources/webhook.rb', line 50

def all
  response = execute(ALL_WEBHOOKS_QUERY)
  response.data.webhookSubscriptions.edges.map do |edge|
    edge.node
  end
end

.create(topic:, address:, include_fields:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shopify_graphql/resources/webhook.rb', line 57

def create(topic:, address:, include_fields:)
  response = execute(CREATE_WEBHOOK_MUTATION,
    topic: topic,
    webhookSubscription: {
      callbackUrl: address,
      format: 'JSON',
      includeFields: include_fields,
    },
  )
  response = response.data.webhookSubscriptionCreate
  handle_user_errors(response)
end

.delete(id) ⇒ Object



70
71
72
73
74
# File 'lib/shopify_graphql/resources/webhook.rb', line 70

def delete(id)
  response = execute(DELETE_WEBHOOK_MUTATION, id: id)
  response = response.data.webhookSubscriptionDelete
  handle_user_errors(response)
end