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

How to use Session with Rest API? #1219

Closed CaioFML closed 1 year ago

CaioFML commented 1 year ago

Issue summary

I'm updating the shopify api and app gems to the latest from an older version where I was using this flow to activate a session and use the shopify_api interface to make requests to the rest api:

      shopify_session = ShopifyAPI::Session.new(
        domain: shop.shopify_domain,
        token: shop.shopify_token,
        api_version: ShopifyApp.configuration.api_version
      )

      ShopifyAPI::Base.activate_session(shopify_session)

But with the latest config, it does not work, or even recognize as a method what is being documented in the shopify docs to use:

test_session = ShopifyAPI::Utils::SessionUtils.load_current_session(
  auth_header: request.auth_header,
  cookies: request.cookies,
)

Basically I was able to use only the shopify token, domain and api_version to make the requests easily in the older version, is there something that I'm missing right now to initialize this session for each shop installed my app?

I could do it using shop.with_shopify_session do |session|, but seems not right

zzooeeyy commented 1 year ago

Hey @CaioFML -

Are you using the ShopifyApp Gem? If so, you can follow these instructions on how to use our controller concerns to automatically setup the session behind the scenes for you.

If you're just using the ShopifyAPI gem, you can construct and use the session from your Shopify token like this:

    session = ShopifyAPI::Auth::Session.new(
      shop: "#{your_shop_name}.myshopify.com"
      access_token: "token_for_the_shop"
    )

There are 2 ways you can use the session when making API calls:

  1. Passing session object into each client request

    def make_api_request(shop)
    # 1. create session object
    session = ShopifyAPI::Auth::Session.new(
      shop: "#{your_shop_name}.myshopify.com"
      access_token: "token"
    )
    
    # 2a. Create API client with the session information
    # session must be type `ShopifyAPI::Auth::Session`
    graphql_client = ShopifyAPI::Clients::Graphql::Admin.new(session: session)
    response = graphql_client.query(query: MY_API_QUERY)
    
    # 2b. REST example
    product_count = ShopifyAPI::Product.count(session: session)
    
    ...
    end
  2. Setting active_session in ShopifyAPI::Context

    
    #### Configuration
    def configure_app
    # This method is called before making authenticated API calls
    session = ShopifyAPI::Auth::Session.new(
      shop: "#{your_shop_name}.myshopify.com"
      access_token: "the_token_for_your_custom_app_found_in_admin"
    )
    
    # Activate session to be used in all API calls
    # session must be type `ShopifyAPI::Auth::Session`
    ShopifyAPI::Context.activate_session(session)
    end

Using clients to make authenticated API calls

def make_api_request

1. Create API client without session information

The graphql_client will use ShopifyAPI::Context.active_session when making API calls

graphql_client = ShopifyAPI::Clients::Graphql::Admin.new

2. Use API client to make queries

... end



`api_version` is now set as a part of [`ShopifyAPI::Context.setup`](https://github.com/Shopify/shopify-api-ruby/blob/main/README.md#setup-shopify-context). REST Resource will be loaded to the api version specified in the setup.  If you're using GraphQL admin or REST Admin clients, they can be specified explicitly as well.

We recently improved our documentation, please take a look to see if it contains more useful information to you:
- [Making GraphQL calls](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/graphql.md)
- [Making REST calls](https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/rest.md)

Let me know if you have more questions!
CaioFML commented 1 year ago

Hello, @zzooeeyy!

Thanks for the answer, the ShopifyAPI::Auth::Session worked correctly, maybe this need to be added to the docs, I could not find it anywhere

zzooeeyy commented 1 year ago

yay! We recently improved some of our docs, it did get added in parts to some of the docs! I'll close this ticket now that it has been resolved.