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

Use offline tokens #24

Merged
merged 3 commits into from
Jul 8, 2022
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ This template combines a number of third party open source tools:
- [Rails](https://rubyonrails.org/) builds and tests the backend.
- [Vite](https://vitejs.dev/) builds the [React](https://reactjs.org/) frontend.
- [React Router](https://reactrouter.com/) is used for routing. We wrap this with file-based routing.
- [React Query](https://react-query.tanstack.com/) queries the GraphQL Admin API.
- [React Query](https://react-query.tanstack.com/) queries the Admin API.

These third party tools are complemented by Shopify specific tools to ease app development:

- [Shopify API library](https://github.com/Shopify/shopify_api) adds OAuth to the Rails backend. This lets users install the app and grant scope permissions.
- [App Bridge React](https://shopify.dev/tools/app-bridge/react-components) adds authentication to API requests in the frontend and renders components outside of the App’s iFrame.
- [Polaris React](https://polaris.shopify.com/) is a powerful design system and component library that helps developers build high quality, consistent experiences for Shopify merchants.
- [Custom hooks](https://github.com/Shopify/shopify-frontend-template-react/tree/main/hooks) make authenticated requests to the GraphQL Admin API.
- [Custom hooks](https://github.com/Shopify/shopify-frontend-template-react/tree/main/hooks) make authenticated requests to the Admin API.
- [File-based routing](https://github.com/Shopify/shopify-frontend-template-react/blob/main/Routes.jsx) makes creating new pages easier.

## Getting started
Expand Down
19 changes: 0 additions & 19 deletions web/app/controllers/graphql_controller.rb

This file was deleted.

16 changes: 16 additions & 0 deletions web/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ class ProductsController < AuthenticatedController
def count
render(json: ShopifyAPI::Product.count.body)
end

def create
ProductCreator.call(count: 5)

success = true
error = nil
status_code = 200
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)
end
end
2 changes: 0 additions & 2 deletions web/app/jobs/app_uninstalled_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def perform(topic:, shop_domain:, webhook:)
end

logger.info("#{self.class} started for shop '#{shop_domain}'")
users = User.where(shopify_domain: shop_domain)
users.each(&:destroy)
shop.destroy
end
end
9 changes: 0 additions & 9 deletions web/app/models/user.rb

This file was deleted.

7 changes: 7 additions & 0 deletions web/app/services/application_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ApplicationService
def self.call(*args, **kwargs, &block)
new(*args, **kwargs, &block).call
end
end
118 changes: 118 additions & 0 deletions web/app/services/product_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# frozen_string_literal: true

class ProductCreator < ApplicationService
attr_reader :count

CREATE_PRODUCTS_MUTATION = <<~'QUERY'
mutation populateProduct($input: ProductInput!) {
productCreate(input: $input) {
product {
id
}
}
}
QUERY

def initialize(count:, session: ShopifyAPI::Context.active_session)
super()
@count = count
@session = session
end

def call
client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)

(1..count).each do |_i|
client.query(
query: CREATE_PRODUCTS_MUTATION,
variables: {
input: {
title: random_title,
variants: [{ price: random_price }],
},
}
)
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
end

ADJECTIVES = [
"autumn",
"hidden",
"bitter",
"misty",
"silent",
"empty",
"dry",
"dark",
"summer",
"icy",
"delicate",
"quiet",
"white",
"cool",
"spring",
"winter",
"patient",
"twilight",
"dawn",
"crimson",
"wispy",
"weathered",
"blue",
"billowing",
"broken",
"cold",
"damp",
"falling",
"frosty",
"green",
"long",
]

NOUNS = [
"waterfall",
"river",
"breeze",
"moon",
"rain",
"wind",
"sea",
"morning",
"snow",
"lake",
"sunset",
"pine",
"shadow",
"leaf",
"dawn",
"glitter",
"forest",
"hill",
"cloud",
"meadow",
"sun",
"glade",
"bird",
"brook",
"butterfly",
"bush",
"dew",
"dust",
"field",
"fire",
"flower",
]
end
1 change: 0 additions & 1 deletion web/config/initializers/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
config.after_authenticate_job = false
config.api_version = ShopifyAPI::AdminVersions::LATEST_SUPPORTED_ADMIN_VERSION
config.shop_session_repository = "Shop"
config.user_session_repository = "User"
Copy link
Contributor

Choose a reason for hiding this comment

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

Will shopify_app work without a configured user session repository?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, as per the docs, adding a user_session_repository is what makes the gem use online tokens.


config.reauth_on_access_scope_changes = true

Expand Down
5 changes: 2 additions & 3 deletions web/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

get "/api/products-count", to: "products#count"

post "/api/graphql", to: "graphql#proxy"
get "/api/products/count", to: "products#count"
get "/api/products/create", to: "products#create"

# Any other routes will just render the react app
match "*path" => "home#index", via: [:get, :post]
Expand Down
16 changes: 0 additions & 16 deletions web/db/migrate/20220609125828_create_users.rb

This file was deleted.

This file was deleted.

12 changes: 1 addition & 11 deletions web/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 0 additions & 113 deletions web/test/controllers/graphql_controller_test.rb

This file was deleted.

Loading