Skip to content

Commit cdd07a9

Browse files
Removed OStructHashUtils Utility Class.
Added response_as_struct as param to client query to be able to override the default behavior and passed in false for all calls from the webhook registry.
1 parent 34c8511 commit cdd07a9

File tree

5 files changed

+14
-85
lines changed

5 files changed

+14
-85
lines changed

lib/shopify_api/clients/graphql/client.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ def initialize(session:, base_path:, api_version: nil)
2828
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
2929
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
3030
tries: Integer,
31+
response_as_struct: T.nilable(T::Boolean),
3132
).returns(HttpResponse)
3233
end
33-
def query(query:, variables: nil, headers: nil, tries: 1)
34+
def query(query:, variables: nil, headers: nil, tries: 1, response_as_struct: Context.response_as_struct)
3435
body = { query: query, variables: variables }
3536
@http_client.request(
3637
HttpRequest.new(
@@ -42,7 +43,7 @@ def query(query:, variables: nil, headers: nil, tries: 1)
4243
body_type: "application/json",
4344
tries: tries,
4445
),
45-
response_as_struct: Context.response_as_struct || false,
46+
response_as_struct: response_as_struct || false,
4647
)
4748
end
4849
end

lib/shopify_api/utils/ostruct_hash_utils.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/shopify_api/webhooks/registry.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ def unregister(topic:, session:)
138138
}
139139
MUTATION
140140

141-
delete_response = client.query(query: delete_mutation)
141+
delete_response = client.query(query: delete_mutation, response_as_struct: false)
142142
raise Errors::WebhookRegistrationError,
143143
"Failed to delete webhook from Shopify" unless delete_response.ok?
144-
result = T.cast(ShopifyAPI::Utils::OstructHashUtils.ensure_hash(delete_response.body),
145-
T::Hash[String, T.untyped])
144+
result = T.cast(delete_response.body, T::Hash[String, T.untyped])
146145
errors = result["errors"] || {}
147146
raise Errors::WebhookRegistrationError,
148147
"Failed to delete webhook from Shopify: #{errors[0]["message"]}" unless errors.empty?
@@ -171,11 +170,10 @@ def get_webhook_id(topic:, client:)
171170
}
172171
QUERY
173172

174-
fetch_id_response = client.query(query: fetch_id_query)
173+
fetch_id_response = client.query(query: fetch_id_query, response_as_struct: false)
175174
raise Errors::WebhookRegistrationError,
176175
"Failed to fetch webhook from Shopify" unless fetch_id_response.ok?
177-
body = T.cast(ShopifyAPI::Utils::OstructHashUtils.ensure_hash(fetch_id_response.body),
178-
T::Hash[String, T.untyped])
176+
body = T.cast(fetch_id_response.body, T::Hash[String, T.untyped])
179177
errors = body["errors"] || {}
180178
raise Errors::WebhookRegistrationError,
181179
"Failed to fetch webhook from Shopify: #{errors[0]["message"]}" unless errors.empty?
@@ -218,13 +216,10 @@ def process(request)
218216
).returns(T::Hash[Symbol, T.untyped])
219217
end
220218
def webhook_registration_needed?(client, registration)
221-
check_response = client.query(query: registration.build_check_query)
219+
check_response = client.query(query: registration.build_check_query, response_as_struct: false)
222220
raise Errors::WebhookRegistrationError,
223221
"Failed to check if webhook was already registered" unless check_response.ok?
224-
225-
response_body = ShopifyAPI::Utils::OstructHashUtils.ensure_hash(check_response.body)
226-
227-
parsed_check_result = registration.parse_check_result(T.cast(response_body, T::Hash[String, T.untyped]))
222+
parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
228223
must_register = parsed_check_result[:current_address] != registration.callback_address
229224

230225
{ webhook_id: parsed_check_result[:webhook_id], must_register: must_register }
@@ -238,12 +233,12 @@ def webhook_registration_needed?(client, registration)
238233
).returns(T::Hash[String, T.untyped])
239234
end
240235
def send_register_request(client, registration, webhook_id)
241-
register_response = client.query(query: registration.build_register_query(webhook_id: webhook_id))
236+
register_response = client.query(query: registration.build_register_query(webhook_id: webhook_id),
237+
response_as_struct: false)
242238

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

245-
response_body = ShopifyAPI::Utils::OstructHashUtils.ensure_hash(register_response.body)
246-
T.cast(response_body, T::Hash[String, T.untyped])
241+
T.cast(register_response.body, T::Hash[String, T.untyped])
247242
end
248243

249244
sig { params(body: T::Hash[String, T.untyped], mutation_name: String).returns(T::Boolean) }

test/utils/ostruct_hash_utils_test.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/webhooks/registry_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ def test_process_with_response_as_struct
7171
assert_equal({}, body)
7272
handler_called = true
7373
end,
74-
)
74+
)
7575

7676
ShopifyAPI::Webhooks::Registry.add_registration(
7777
topic: @topic, path: "path", delivery_method: :http, handler: handler,
78-
)
78+
)
7979

8080
ShopifyAPI::Webhooks::Registry.process(@webhook_request)
8181

0 commit comments

Comments
 (0)