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

[minor] Scrip tags manager #1948

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

[minor] Scrip tags manager #1948

wants to merge 2 commits into from

Conversation

lizkenyon
Copy link
Contributor

@lizkenyon lizkenyon commented Mar 10, 2025

What this PR does

  • This PR adds the a script tag manager to the shopify_app gem.

  • This functionality was removed in version 22 of this package.

  • Script tags are still a valid way for modify the content of vintage themes.

  • This PR has several differences between the previous script tag manager

    • Not the manager adds script tags with GraphQL instead of REST
    • This adds the ability to specify whether or not the script tags should be cached
    • You can now specify a template to check for before adding the script tag
      • Script tags should not be used if the theme has a valid template
    • Change the name from scripttag_manager to script_tag_manager
script_tag-720.mov

Reviewer's guide to testing

Things to focus on

  1. Is the documentation clear?
  2. Has functionality been restored?

Checklist

Before submitting the PR, please consider if any of the following are needed:

  • Update CHANGELOG.md if the changes would impact users
  • Update README.md, if appropriate.
  • Update any relevant pages in /docs, if necessary
  • For security fixes, the Disclosure Policy must be followed.

@lizkenyon lizkenyon requested a review from a team as a code owner March 10, 2025 22:24
@lizkenyon lizkenyon force-pushed the liz/add-scriptags-manager branch 2 times, most recently from a9aefb3 to f09e915 Compare March 11, 2025 15:09
@lizkenyon lizkenyon changed the title [WIP] scriptags manager [minor] Scrip tags manager Mar 11, 2025
Add check for app blocks

Add docs

Update naming

rubo cop

rubo cop
@lizkenyon lizkenyon force-pushed the liz/add-scriptags-manager branch from 7ef47f6 to 7aafea2 Compare March 11, 2025 16:56
@@ -15,7 +15,7 @@ class Configuration
attr_accessor :embedded_app
alias_method :embedded_app?, :embedded_app
attr_accessor :webhooks
attr_accessor :scripttags
attr_accessor :script_tags
Copy link
Contributor

Choose a reason for hiding this comment

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

i like the rename 👍

Comment on lines +56 to +58
if template_types_to_check.all? do |template_type|
template_supports_app_blocks?(active_theme["id"], template_type)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

can we split this into a variable?

theme_support_app_blocks = template_types_to_check.all? do |template_type|
  template_supports_app_blocks?(active_theme["id"], template_type)
end

we also have this method theme_supports_app_blocks?, can we use it here?

Comment on lines +112 to +134
begin
response = client.query(query: query)

# Check for errors in the response
if response.body["errors"].present?
error_message = response.body["errors"].map { |e| e["message"] }.join(", ")
raise "GraphQL error: #{error_message}"
end

# Check if the response has the expected structure
unless response.body["data"] && response.body["data"]["themes"] && response.body["data"]["themes"]["nodes"]
raise "Invalid response structure"
end

themes = response.body["data"]["themes"]["nodes"]
return nil if themes.empty?

themes.first
rescue => e
ShopifyApp::Logger.warn("Failed to fetch active theme: #{e.message}")
nil
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think we need the begin..rescue..end block here, can we rescue on the method instead?

def fetch_active_theme
rescue
end

Comment on lines +112 to +134
begin
response = client.query(query: query)

# Check for errors in the response
if response.body["errors"].present?
error_message = response.body["errors"].map { |e| e["message"] }.join(", ")
raise "GraphQL error: #{error_message}"
end

# Check if the response has the expected structure
unless response.body["data"] && response.body["data"]["themes"] && response.body["data"]["themes"]["nodes"]
raise "Invalid response structure"
end

themes = response.body["data"]["themes"]["nodes"]
return nil if themes.empty?

themes.first
rescue => e
ShopifyApp::Logger.warn("Failed to fetch active theme: #{e.message}")
nil
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

can we remove the nested begin...rescue...end and use the def...rescue...end pattern?

def fetch_active_theme
rescue => e
end

Comment on lines +122 to +124
unless response.body["data"] && response.body["data"]["themes"] && response.body["data"]["themes"]["nodes"]
raise "Invalid response structure"
end
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need this check? It should be guaranteed by the graphql query we send right?

Copy link
Contributor

Choose a reason for hiding this comment

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

i think we should handle userErrors response in addition to the previous errors response

end
end

def template_supports_app_blocks?(theme_id, template_type)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we break down this method to smaller ones and handle exceptions in each method instead of multiple calls here?

if response.body["data"]["scriptTagCreate"]["userErrors"].any?
errors = response.body["data"]["scriptTagCreate"]["userErrors"]
error_messages = errors.map { |e| "#{e["field"]}: #{e["message"]}" }.join(", ")
raise ::ShopifyApp::CreationFailed, "ScriptTag creation failed: #{error_messages}"
Copy link
Contributor

Choose a reason for hiding this comment

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

some of the exception we raise above are not wrapped around a class like this one. Should we wrap them in error classes like this so it's more explicit?

Comment on lines +348 to +350
ShopifyApp::Logger.warn("GraphQL error fetching script tags: #{response.body["errors"].map do |e|
e["message"]
end.join(", ")}")
Copy link
Contributor

Choose a reason for hiding this comment

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

this formatting is kinda strange 🤔

Comment on lines +355 to +357
return [] unless response.body["data"] &&
response.body["data"]["scriptTags"] &&
response.body["data"]["scriptTags"]["edges"]
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't the structure be guaranteed here as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

also we should handle userErrors response

@@ -13,7 +13,7 @@ def self.call
config.myshopify_domain = "myshopify.com"
config.api_version = ShopifyAPI::LATEST_SUPPORTED_ADMIN_VERSION
config.billing = nil
config.scripttags = nil
config.script_tags = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

wondering if this is gonna make this incompatible with previous versions 🤔

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