Shopify / shopify-api-ruby

ShopifyAPI is a lightweight gem for accessing the Shopify admin REST and GraphQL web services.
MIT License
1.06k stars 473 forks source link

updated session docs #1172

Closed nelsonwittwer closed 1 year ago

nelsonwittwer commented 1 year ago

Description

As noted in https://github.com/Shopify/shopify-api-ruby/issues/1163, our docs for sessions are out of date. These changes better outline how sessions work within the ruby libraries.

vfonic commented 1 year ago

Where in the docs I can read more about auth_header variable that appears in several places in the docs out of nowhere?

nelsonwittwer commented 1 year ago

Where in the docs I can read more about auth_header variable that appears in several places in the docs out of nowhere?

This is the HTTP_AUTHORIZATION header. You can see it used in the shopify_app.

vfonic commented 1 year ago

@nelsonwittwer do you think this would be something worth including somewhere in the docs? Thanks!

This might be better line of code to link to: https://github.com/Shopify/shopify_app/blob/main/lib/shopify_app/controller_concerns/login_protection.rb#L53

As someone who has been actively building Shopify apps for the past several years, it is still not clear how I can make Shopify API calls with the latest version of shopify_api gem outside of the request-response scope (in bg jobs for example).

EDIT: I have the code written somewhere that works. If you need it, I can dig it up and share it here so that you can add it to documentation?

kyreeves commented 1 year ago

@vfonic I'm having trouble making requests in background jobs as well. Have you had any luck with this since your last comment?

vfonic commented 1 year ago

Yes, I have it fully working. You can do:

shop.with_shopify_session do
  # any code that makes requests to Shopify REST or GraphQL API
end

or

session = ShopifyAPI::Auth::Session.new(shop: shop.shopify_domain, access_token: shop.shopify_token, scope: shop.access_scopes)
# and then
client = ShopifyAPI::Clients::Graphql::Admin.new(session: session)
client.query(query:, variables:).body
# or
ShopifyAPI::Context.activate_session(session)
ShopifyAPI::Webhook.all.as_json
# or whatever you need

Something along these lines should work. In both cases shop is an ActiveRecord instance of Shop model as per ShopifyApp gem.