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

Update template to use token exchange #129

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ group :test do
gem "webdrivers"
end

gem "shopify_app", "~> 22.0"
gem "shopify_app", "~> 22.2"
16 changes: 4 additions & 12 deletions web/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ def count

# POST /api/products
def create
ProductCreator.call(count: 5, session: current_shopify_session)

success = true
error = nil
status_code = 200
ProductCreator.call(count: 5, session: current_shopify_session, id_token: shopify_id_token)
render(json: { success: true, error: nil })
rescue => e
success = false
error = e.message
status_code = e.is_a?(ShopifyAPI::Errors::HttpResponseError) ? e.code : 500

logger.info("Failed to create products: #{error}")
ensure
render(json: { success: success, error: error }, status: status_code)
logger.error("Failed to create products: #{e.message}")
render(json: { success: false, error: e.message }, status: e.try(:code) || :internal_server_error)
end
end
2 changes: 2 additions & 0 deletions web/app/services/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ def call(*args, **kwargs, &block)
new(*args, **kwargs, &block).call
end
end

def initialize(*args, **kwargs, &block); end
end
40 changes: 16 additions & 24 deletions web/app/services/product_creator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

class ProductCreator < ApplicationService
include ShopifyApp::AdminAPI::WithTokenRefetch

attr_reader :count

CREATE_PRODUCTS_MUTATION = <<~QUERY
mutation populateProduct($input: ProductInput!) {
productCreate(input: $input) {
mutation populateProduct($title: String!) {
productCreate(input: {title: $title}) {
product {
title
id
Expand All @@ -14,41 +16,31 @@ class ProductCreator < ApplicationService
}
QUERY

def initialize(count:, session:)
super()
def initialize(count:, session:, id_token:)
super
@count = count
@session = session
@id_token = id_token
end

def call
client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)
count.times do
response = with_token_refetch(@session, @id_token) do
client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)
client.query(query: CREATE_PRODUCTS_MUTATION, variables: { title: random_title })
end

(1..count).each do |_i|
response = client.query(
query: CREATE_PRODUCTS_MUTATION,
variables: {
input: {
title: random_title,
},
},
)
raise StandardError, response.body["errors"].to_s if response.body["errors"]

created_product = response.body["data"]["productCreate"]["product"]
ShopifyAPI::Logger.info("Created Product | Title: '#{created_product["title"]}' | Id: '#{created_product["id"]}'")
created_product = response.body.dig("data", "productCreate", "product")
Rails.logger.info("Created Product | Title: '#{created_product["title"]}' | Id: '#{created_product["id"]}'")
end
end

private

def random_title
adjective = ADJECTIVES[rand(ADJECTIVES.size)]
noun = NOUNS[rand(NOUNS.size)]

"#{adjective} #{noun}"
end

def random_price
(100.0 + rand(1000)) / 100
"#{ADJECTIVES.sample} #{NOUNS.sample}"
end

ADJECTIVES = [
Expand Down
1 change: 1 addition & 0 deletions web/config/initializers/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
config.shop_session_repository = "Shop"

config.reauth_on_access_scope_changes = true
config.new_embedded_auth_strategy = true

config.root_url = "/api"
config.login_url = "/api/auth"
Expand Down
Loading