@@ -38,21 +38,15 @@ def create_script_tags(session:)
38
38
@session = session
39
39
return unless required_script_tags . present?
40
40
41
- # Check if any scripttag has template_types defined
42
41
template_types_to_check = required_script_tags . flat_map { |tag | tag [ :template_types ] } . compact . uniq
43
42
44
- # If template types are specified, check if the theme supports app blocks for those templates
45
43
if template_types_to_check . any?
46
- # First fetch the active theme
47
44
active_theme = fetch_active_theme
48
-
49
- # If we failed to fetch the active theme, don't proceed with script tag creation
50
45
unless active_theme
51
46
ShopifyApp ::Logger . debug ( "Failed to fetch active theme. Skipping script tag creation." )
52
47
return
53
48
end
54
49
55
- # Check if all templates support app blocks
56
50
if all_templates_support_app_blocks? ( active_theme [ "id" ] , template_types_to_check )
57
51
ShopifyApp ::Logger . info (
58
52
"Theme supports app blocks for templates: #{ template_types_to_check . join ( ", " ) } . " \
@@ -156,44 +150,32 @@ def fetch_active_theme
156
150
157
151
response = client . query ( query : ACTIVE_THEME_QUERY )
158
152
159
- # Check for errors in the response
160
153
if response . body [ "errors" ] . present?
161
154
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"
155
+ raise ShopifyAPI ::Errors ::InvalidGraphqlRequestError , error_message
168
156
end
169
157
170
158
themes = response . body [ "data" ] [ "themes" ] [ "nodes" ]
171
- return nil if themes . empty?
159
+ return if themes . empty?
172
160
173
161
themes . first
174
162
rescue => e
175
163
ShopifyApp ::Logger . warn ( "Failed to fetch active theme: #{ e . message } " )
176
164
nil
177
165
end
178
166
179
- # New method to check all templates at once
180
167
def all_templates_support_app_blocks? ( theme_id , template_types )
181
168
client = graphql_client
182
169
183
- # First, fetch all JSON templates at once
184
170
template_filenames = template_types . map { |type | "templates/#{ type } .json" }
185
171
json_templates = fetch_json_templates ( client , theme_id , template_filenames )
186
172
187
- # If any template is missing, return false
188
173
return false if json_templates . length != template_types . length
189
174
190
- # Extract main sections from all templates
191
175
main_sections = extract_main_sections ( json_templates )
192
176
193
- # If any template doesn't have a main section, return false
194
177
return false if main_sections . length != template_types . length
195
178
196
- # Check if all main sections support app blocks
197
179
all_sections_support_app_blocks? ( client , theme_id , main_sections )
198
180
rescue => e
199
181
ShopifyApp ::Logger . error ( "Error checking template support: #{ e . message } " )
@@ -208,10 +190,9 @@ def fetch_json_templates(client, theme_id, template_filenames)
208
190
209
191
files_response = client . query ( query : FILES_QUERY , variables : files_variables )
210
192
211
- # Check for errors in the response
212
193
if files_response . body [ "errors" ] . present?
213
194
error_message = files_response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
214
- raise "GraphQL error: #{ error_message } "
195
+ raise ShopifyAPI :: Errors :: InvalidGraphqlRequestError , error_message
215
196
end
216
197
217
198
files_response . body [ "data" ] [ "theme" ] [ "files" ] [ "nodes" ]
@@ -250,15 +231,13 @@ def all_sections_support_app_blocks?(client, theme_id, section_filenames)
250
231
251
232
section_response = client . query ( query : FILES_QUERY , variables : section_variables )
252
233
253
- # Check for errors in the section response
254
234
if section_response . body [ "errors" ] . present?
255
235
error_message = section_response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
256
- raise "GraphQL error: #{ error_message } "
236
+ raise ShopifyAPI :: Errors :: InvalidGraphqlRequestError , error_message
257
237
end
258
238
259
239
section_files = section_response . body [ "data" ] [ "theme" ] [ "files" ] [ "nodes" ]
260
240
261
- # If we didn't get all the sections we asked for, return false
262
241
return false if section_files . length != section_filenames . length
263
242
264
243
# Check if all sections support app blocks
@@ -275,89 +254,6 @@ def all_sections_support_app_blocks?(client, theme_id, section_filenames)
275
254
false
276
255
end
277
256
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
257
def expanded_script_tags
362
258
self . class . build_src ( required_script_tags , shop_domain )
363
259
end
@@ -379,25 +275,13 @@ def create_script_tag(attributes)
379
275
380
276
response = client . query ( query : SCRIPT_TAG_CREATE_MUTATION , variables : variables )
381
277
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?
278
+ if response . body [ "data" ] [ "scriptTagCreate" ] [ "userErrors" ] . any?
387
279
errors = response . body [ "data" ] [ "scriptTagCreate" ] [ "userErrors" ]
388
280
error_messages = errors . map { |e | "#{ e [ "field" ] } : #{ e [ "message" ] } " } . join ( ", " )
389
281
raise ::ShopifyApp ::CreationFailed , "ScriptTag creation failed: #{ error_messages } "
390
282
end
391
283
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
284
+ response . body [ "data" ] [ "scriptTagCreate" ] [ "scriptTag" ]
401
285
rescue ShopifyAPI ::Errors ::HttpResponseError => e
402
286
raise ::ShopifyApp ::CreationFailed , e . message
403
287
end
@@ -425,19 +309,12 @@ def fetch_all_script_tags
425
309
426
310
response = client . query ( query : SCRIPT_TAGS_QUERY )
427
311
428
- # Check for errors in the response
429
312
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 ( ", " ) } ")
313
+ error_messages = response . body [ "errors" ] . map { |e | e [ "message" ] } . join ( ", " )
314
+ ShopifyApp ::Logger . warn ( "GraphQL error fetching script tags: #{ error_messages } " )
433
315
return [ ]
434
316
end
435
317
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
318
response . body [ "data" ] [ "scriptTags" ] [ "edges" ] . map { |edge | edge [ "node" ] }
442
319
rescue => e
443
320
ShopifyApp ::Logger . warn ( "Error fetching script tags: #{ e . message } " )
0 commit comments