Skip to content

Commit 2c54020

Browse files
authored
Merge branch 'main' into liz/config-to-disable-rest
2 parents 5815f02 + 451f983 commit 2c54020

File tree

8 files changed

+488
-294
lines changed

8 files changed

+488
-294
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44
## Unreleased
55

6+
- [#1344](https://github.com/Shopify/shopify-api-ruby/pull/1344) Allow ShopifyAPI::Webhooks::Registry to update a webhook when fields or metafield_namespaces are changed.
67
- [#1343](https://github.com/Shopify/shopify-api-ruby/pull/1343) Make ShopifyAPI::Context::scope parameter optional. `scope` defaults to empty list `[]`.
78
- [#1348](https://github.com/Shopify/shopify-api-ruby/pull/1348) Add config option that will disable the REST API client and REST resources. New apps should use the GraphQL Admin API
89

lib/shopify_api/webhooks/registration.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ def mutation_name(webhook_id); end
4848
sig { abstract.returns(String) }
4949
def build_check_query; end
5050

51-
sig { abstract.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
51+
sig do
52+
abstract.params(body: T::Hash[String, T.untyped]).returns({
53+
webhook_id: T.nilable(String),
54+
current_address: T.nilable(String),
55+
fields: T::Array[String],
56+
metafield_namespaces: T::Array[String],
57+
})
58+
end
5259
def parse_check_result(body); end
5360

5461
sig { params(webhook_id: T.nilable(String)).returns(String) }

lib/shopify_api/webhooks/registrations/event_bridge.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def build_check_query
3030
edges {
3131
node {
3232
id
33+
includeFields
34+
metafieldNamespaces
3335
endpoint {
3436
__typename
3537
... on WebhookEventBridgeEndpoint {
@@ -43,17 +45,29 @@ def build_check_query
4345
QUERY
4446
end
4547

46-
sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
48+
sig do
49+
override.params(body: T::Hash[String, T.untyped]).returns({
50+
webhook_id: T.nilable(String),
51+
current_address: T.nilable(String),
52+
fields: T::Array[String],
53+
metafield_namespaces: T::Array[String],
54+
})
55+
end
4756
def parse_check_result(body)
4857
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
4958
webhook_id = nil
59+
fields = []
60+
metafield_namespaces = []
5061
current_address = nil
5162
unless edges.empty?
5263
node = edges[0]["node"]
5364
webhook_id = node["id"].to_s
5465
current_address = node["endpoint"]["arn"].to_s
66+
fields = node["includeFields"] || []
67+
metafield_namespaces = node["metafieldNamespaces"] || []
5568
end
56-
{ webhook_id: webhook_id, current_address: current_address }
69+
{ webhook_id: webhook_id, current_address: current_address, fields: fields,
70+
metafield_namespaces: metafield_namespaces, }
5771
end
5872
end
5973
end

lib/shopify_api/webhooks/registrations/http.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def build_check_query
3636
edges {
3737
node {
3838
id
39+
includeFields
40+
metafieldNamespaces
3941
endpoint {
4042
__typename
4143
... on WebhookHttpEndpoint {
@@ -49,10 +51,19 @@ def build_check_query
4951
QUERY
5052
end
5153

52-
sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
54+
sig do
55+
override.params(body: T::Hash[String, T.untyped]).returns({
56+
webhook_id: T.nilable(String),
57+
current_address: T.nilable(String),
58+
fields: T::Array[String],
59+
metafield_namespaces: T::Array[String],
60+
})
61+
end
5362
def parse_check_result(body)
5463
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
5564
webhook_id = nil
65+
fields = []
66+
metafield_namespaces = []
5667
current_address = nil
5768
unless edges.empty?
5869
node = edges[0]["node"]
@@ -63,8 +74,11 @@ def parse_check_result(body)
6374
else
6475
node["callbackUrl"].to_s
6576
end
77+
fields = node["includeFields"] || []
78+
metafield_namespaces = node["metafieldNamespaces"] || []
6679
end
67-
{ webhook_id: webhook_id, current_address: current_address }
80+
{ webhook_id: webhook_id, current_address: current_address, fields: fields,
81+
metafield_namespaces: metafield_namespaces, }
6882
end
6983
end
7084
end

lib/shopify_api/webhooks/registrations/pub_sub.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def build_check_query
3434
edges {
3535
node {
3636
id
37+
includeFields
38+
metafieldNamespaces
3739
endpoint {
3840
__typename
3941
... on WebhookPubSubEndpoint {
@@ -48,17 +50,30 @@ def build_check_query
4850
QUERY
4951
end
5052

51-
sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
53+
sig do
54+
override.params(body: T::Hash[String, T.untyped]).returns({
55+
webhook_id: T.nilable(String),
56+
current_address: T.nilable(String),
57+
fields: T::Array[String],
58+
metafield_namespaces: T::Array[String],
59+
})
60+
end
5261
def parse_check_result(body)
5362
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
5463
webhook_id = nil
64+
fields = []
65+
metafield_namespaces = []
5566
current_address = nil
5667
unless edges.empty?
5768
node = edges[0]["node"]
5869
webhook_id = node["id"].to_s
59-
current_address = "pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
70+
current_address =
71+
"pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
72+
fields = node["includeFields"] || []
73+
metafield_namespaces = node["metafieldNamespaces"] || []
6074
end
61-
{ webhook_id: webhook_id, current_address: current_address }
75+
{ webhook_id: webhook_id, current_address: current_address, fields: fields,
76+
metafield_namespaces: metafield_namespaces, }
6277
end
6378
end
6479
end

lib/shopify_api/webhooks/registry.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,14 @@ def webhook_registration_needed?(client, registration)
219219
check_response = client.query(query: registration.build_check_query, response_as_struct: false)
220220
raise Errors::WebhookRegistrationError,
221221
"Failed to check if webhook was already registered" unless check_response.ok?
222+
222223
parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
223-
must_register = parsed_check_result[:current_address] != registration.callback_address
224+
registration_fields = registration.fields || []
225+
registration_metafield_namespaces = registration.metafield_namespaces || []
226+
227+
must_register = parsed_check_result[:current_address] != registration.callback_address ||
228+
parsed_check_result[:fields].sort != registration_fields.sort ||
229+
parsed_check_result[:metafield_namespaces].sort != registration_metafield_namespaces.sort
224230

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

0 commit comments

Comments
 (0)