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

Extend webhook registration to support filters #1347

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

kennyadsl
Copy link
Contributor

@kennyadsl kennyadsl commented Nov 20, 2024

Description

With this change, we can register webhooks specifying the filter parameter that allows to define some conditions on the topic fields to reduce the number of webhooks received.

Other than adding this feature for already possible registrations, this extends the number of webhooks that can be registered with this library.

In fact, there are some webhook registrations that require the filter parameter to be passed to allow the registrationi (this is partly an assumption because I couldn't find proper documentation around this topic). An example is METAOBJECTS_CREATE, which, without the filter param set,

mutation webhookSubscriptionCreate($topic: WebhookSubscriptionTopic!, $webhookSubscription: WebhookSubscriptionInput!) {
  webhookSubscriptionCreate(topic: $topic, webhookSubscription: $webhookSubscription) {
    webhookSubscription {
    	topic
      filter
    }
    userErrors {
      field
      message
    }
  }
}

{
  "topic": "METAOBJECTS_CREATE",
  "webhookSubscription": {
    "callbackUrl": "https://test5.com"
  }
}

gives the following error:

{
  "data": {
    "webhookSubscriptionCreate": {
      "webhookSubscription": null,
      "userErrors": [
        {
          "field": [
            "webhookSubscription"
          ],
          "message": "The specified filter is invalid, please ensure you specify the field(s) you wish to filter on."
        }
      ]
    }
  }
}

While with the filter on, works as expected.


This change has been made available to 2024-07 and 2024-10, which are the versions with the filter parameter available, according to the GraphQL documentation.


Refs:

How has this been tested?

I prepared a follow-up change for shopify_app, which can be found here. With both changes, I can register to the METAOBJECTS_CREATE topic in my app with:

ShopifyApp.configure do |config|
  config.webhooks = [
    {
      topic: "metaobjects/update",
      path: "api/webhooks/metaobjects_update",
      filter: "type:my_type OR type:my_other_type",
    },
  ]
end

Checklist:

  • My commit message follow the pattern described in here
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the project documentation.
  • I have added a changelog line.

With this change, we can register webhooks specifying the filter
parameter that allows to define some conditions on the topic fields
to reduce the number of webhooks received.

Other than adding this feature for already possible registrations, this
extends the number of webhooks that can be registered with this library.

In fact, there are some webhook registrations that require the filter
parameter to be passed to allow the registrationi (this is partly an
assumption because I couldn't find proper documentation around this
topic). An example is METAOBJECTS_CREATE, which, without the filter
param set,

```
mutation webhookSubscriptionCreate($topic: WebhookSubscriptionTopic!, $webhookSubscription: WebhookSubscriptionInput!) {
  webhookSubscriptionCreate(topic: $topic, webhookSubscription: $webhookSubscription) {
    webhookSubscription {
    	topic
      filter
    }
    userErrors {
      field
      message
    }
  }
}

{
  "topic": "METAOBJECTS_CREATE",
  "webhookSubscription": {
    "callbackUrl": "https://test5.com"
  }
}

```

gives the following error:

```
{
  "data": {
    "webhookSubscriptionCreate": {
      "webhookSubscription": null,
      "userErrors": [
        {
          "field": [
            "webhookSubscription"
          ],
          "message": "The specified filter is invalid, please ensure you specify the field(s) you wish to filter on."
        }
      ]
    }
  }
}
```

While with the filter on, works as expected.

---

This change has been made available to 2024-07 and 2024-10, which are
the versions with the filter parameter available, according to the
GraphQL documentation.

---

Refs:
- https://shopify.dev/docs/apps/build/webhooks/customize/filters
- https://shopify.dev/docs/api/admin-graphql/2024-07/input-objects/WebhookSubscriptionInput
- https://shopify.dev/docs/api/admin-graphql/2024-10/input-objects/WebhookSubscriptionInput
Copy link
Contributor

@matteodepalo matteodepalo left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you for contributing with both PRs 🙂

@matteodepalo matteodepalo merged commit b10f372 into Shopify:main Nov 28, 2024
5 checks passed
@kennyadsl kennyadsl deleted the kennyadsl/add-filter-support branch November 28, 2024 11:32
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.

2 participants