@@ -159,12 +159,7 @@ def fetch_active_theme
159
159
# Check for errors in the response
160
160
if response . body [ "errors" ] . present?
161
161
error_message = response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
162
- raise "GraphQL error: #{ error_message } "
163
- end
164
-
165
- # Check if the response has the expected structure
166
- unless response . body [ "data" ] && response . body [ "data" ] [ "themes" ] && response . body [ "data" ] [ "themes" ] [ "nodes" ]
167
- raise "Invalid response structure"
162
+ raise ShopifyAPI ::Errors ::InvalidGraphqlRequestError , error_message
168
163
end
169
164
170
165
themes = response . body [ "data" ] [ "themes" ] [ "nodes" ]
@@ -211,7 +206,7 @@ def fetch_json_templates(client, theme_id, template_filenames)
211
206
# Check for errors in the response
212
207
if files_response . body [ "errors" ] . present?
213
208
error_message = files_response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
214
- raise "GraphQL error: #{ error_message } "
209
+ raise ShopifyAPI :: Errors :: InvalidGraphqlRequestError , error_message
215
210
end
216
211
217
212
files_response . body [ "data" ] [ "theme" ] [ "files" ] [ "nodes" ]
@@ -253,7 +248,7 @@ def all_sections_support_app_blocks?(client, theme_id, section_filenames)
253
248
# Check for errors in the section response
254
249
if section_response . body [ "errors" ] . present?
255
250
error_message = section_response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
256
- raise "GraphQL error: #{ error_message } "
251
+ raise ShopifyAPI :: Errors :: InvalidGraphqlRequestError , error_message
257
252
end
258
253
259
254
section_files = section_response . body [ "data" ] [ "theme" ] [ "files" ] [ "nodes" ]
@@ -275,89 +270,6 @@ def all_sections_support_app_blocks?(client, theme_id, section_filenames)
275
270
false
276
271
end
277
272
278
- # Keep these methods for backward compatibility
279
- def template_supports_app_blocks? ( theme_id , template_type )
280
- client = graphql_client
281
-
282
- # First, check if the JSON template exists and find the main section
283
- main_section = find_main_section ( client , theme_id , template_type )
284
- return false unless main_section
285
-
286
- # Now check if the main section supports app blocks
287
- schema_supports_app_blocks? ( client , theme_id , main_section )
288
- end
289
-
290
- def find_main_section ( client , theme_id , template_type )
291
- filename = "templates/#{ template_type } .json"
292
- files_variables = {
293
- themeId : theme_id ,
294
- filenames : [ filename ] ,
295
- }
296
-
297
- files_response = client . query ( query : FILES_QUERY , variables : files_variables )
298
-
299
- # Check for errors in the response
300
- if files_response . body [ "errors" ] . present?
301
- error_message = files_response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
302
- raise "GraphQL error: #{ error_message } "
303
- end
304
-
305
- template_files = files_response . body [ "data" ] [ "theme" ] [ "files" ] [ "nodes" ]
306
-
307
- # If the JSON template doesn't exist, return nil
308
- return nil if template_files . empty?
309
-
310
- # Parse the JSON template to find the main section
311
- template_content = template_files . first [ "body" ] [ "content" ]
312
- template_data = JSON . parse ( template_content )
313
-
314
- main_section = nil
315
- template_data [ "sections" ] . each do |id , section |
316
- if id == "main" || section [ "type" ] . to_s . start_with? ( "main-" )
317
- main_section = "sections/#{ section [ "type" ] } .liquid"
318
- break
319
- end
320
- end
321
-
322
- main_section
323
- rescue => e
324
- ShopifyApp ::Logger . error ( "Error finding main section: #{ e . message } " )
325
- nil
326
- end
327
-
328
- def schema_supports_app_blocks? ( client , theme_id , section_filename )
329
- section_variables = {
330
- themeId : theme_id ,
331
- filenames : [ section_filename ] ,
332
- }
333
-
334
- section_response = client . query ( query : FILES_QUERY , variables : section_variables )
335
-
336
- # Check for errors in the section response
337
- if section_response . body [ "errors" ] . present?
338
- error_message = section_response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
339
- raise "GraphQL error: #{ error_message } "
340
- end
341
-
342
- section_files = section_response . body [ "data" ] [ "theme" ] [ "files" ] [ "nodes" ]
343
-
344
- return false if section_files . empty?
345
-
346
- section_content = section_files . first [ "body" ] [ "content" ]
347
-
348
- # Extract schema from the section content
349
- schema_match = section_content . match ( /\{ \% \s +schema\s +\% \} ([\s \S ]*?)\{ \% \s +endschema\s +\% \} /m )
350
- return false unless schema_match
351
-
352
- schema = JSON . parse ( schema_match [ 1 ] )
353
-
354
- # Check if the schema has blocks that support app blocks
355
- schema [ "blocks" ] &.any? { |block | block [ "type" ] == "@app" } || false
356
- rescue => e
357
- ShopifyApp ::Logger . error ( "Error checking schema support: #{ e . message } " )
358
- false
359
- end
360
-
361
273
def expanded_script_tags
362
274
self . class . build_src ( required_script_tags , shop_domain )
363
275
end
@@ -379,25 +291,13 @@ def create_script_tag(attributes)
379
291
380
292
response = client . query ( query : SCRIPT_TAG_CREATE_MUTATION , variables : variables )
381
293
382
- # Add proper nil checks for the response structure
383
- if response . body [ "data" ] &&
384
- response . body [ "data" ] [ "scriptTagCreate" ] &&
385
- response . body [ "data" ] [ "scriptTagCreate" ] [ "userErrors" ] &&
386
- response . body [ "data" ] [ "scriptTagCreate" ] [ "userErrors" ] . any?
294
+ if response . body [ "data" ] [ "scriptTagCreate" ] [ "userErrors" ] . any?
387
295
errors = response . body [ "data" ] [ "scriptTagCreate" ] [ "userErrors" ]
388
296
error_messages = errors . map { |e | "#{ e [ "field" ] } : #{ e [ "message" ] } " } . join ( ", " )
389
297
raise ::ShopifyApp ::CreationFailed , "ScriptTag creation failed: #{ error_messages } "
390
298
end
391
299
392
- # Safely access the script tag data
393
- if response . body [ "data" ] &&
394
- response . body [ "data" ] [ "scriptTagCreate" ] &&
395
- response . body [ "data" ] [ "scriptTagCreate" ] [ "scriptTag" ]
396
- response . body [ "data" ] [ "scriptTagCreate" ] [ "scriptTag" ]
397
- else
398
- ShopifyApp ::Logger . warn ( "Script tag creation response missing expected data structure" )
399
- nil
400
- end
300
+ response . body [ "data" ] [ "scriptTagCreate" ] [ "scriptTag" ]
401
301
rescue ShopifyAPI ::Errors ::HttpResponseError => e
402
302
raise ::ShopifyApp ::CreationFailed , e . message
403
303
end
@@ -427,17 +327,11 @@ def fetch_all_script_tags
427
327
428
328
# Check for errors in the response
429
329
if response . body [ "errors" ] . present?
430
- ShopifyApp ::Logger . warn ( "GraphQL error fetching script tags: #{ response . body [ "errors" ] . map do |e |
431
- e [ "message" ]
432
- end . join ( ", " ) } ")
330
+ error_messages = response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
331
+ ShopifyApp ::Logger . warn ( "GraphQL error fetching script tags: #{ error_messages } " )
433
332
return [ ]
434
333
end
435
334
436
- # Handle nil data or missing structure
437
- return [ ] unless response . body [ "data" ] &&
438
- response . body [ "data" ] [ "scriptTags" ] &&
439
- response . body [ "data" ] [ "scriptTags" ] [ "edges" ]
440
-
441
335
response . body [ "data" ] [ "scriptTags" ] [ "edges" ] . map { |edge | edge [ "node" ] }
442
336
rescue => e
443
337
ShopifyApp ::Logger . warn ( "Error fetching script tags: #{ e . message } " )
0 commit comments