How to update Shopify API 2022-10 to 2023-04

Hello!

*old_version ==> new_version*
*Rails 6.1.7*
*shopify_app 21.2.0 ==> 21.7.0*
*shopify_api 12.2.1 ==> 13.3.0*

I’m trying to update Shopify API 2022-10 to 2023-04 but I’m running into errors related to session storage. I understand that session storage is no longer the responsibility of shopify_api gem since 12.3.0

https://github.com/Shopify/shopify-api-ruby/blob/main/CHANGELOG.md#version-1230

but removing this line:

# config/initializers/shopify_app.rb

#...
      session_storage: ShopifyApp::SessionRepository,
#...

results in other errors such as:

Failure/Error:
        allow(ShopifyAPI::Utils::SessionUtils).to(
          receive(:load_current_session).and_return(
            ShopifyAPI::Auth::Session.new(
              shop: account.shopify_domain,
              scope: ShopifyApp.configuration.scope
            )
          )
        )
      
        ShopifyAPI::Utils::SessionUtils does not implement: load_current_session

Any help would be greatly appreciated.

Thanks!

Hi Craftonix,

The error is indicating that the SessionUtils does not have the method load_current_session.

As of version 12.3.0, the shopify_api gem has deprecated the SessionRepository and moved session storage responsibilities to the shopify_app gem. This means you now need to manage the sessions yourself.

You should update your test setup to create a session explicitly. Here’s an example of how you might do this:

session = ShopifyAPI::Session.new(
  domain: account.shopify_domain,
  token: account.shopify_token,
  api_version: '2023-04'
)
ShopifyAPI::Base.activate_session(session)

In the above code, account.shopify_domain should be your shop’s domain, and account.shopify_token should be your access token.

If you’re looking to stub the session for tests, you can do something like:

session = instance_double('ShopifyAPI::Session', valid?: true, site: "https://#{account.shopify_domain}")

allow(ShopifyAPI::Session).to receive(:new).and_return(session)

This will return the stubbed session whenever new is called on ShopifyAPI::Session.

Hope this helps!

1 Like

Thanks Liam for the quick and helpful reply!

I will test your suggestion and let you know soon.

Just curious, where can I find the documentatin for API 2022-10 ? (I know it’s one year old and no longer supported, but I’d like to know)

Hi,

I have posted a reply twice here and for some reasone this interface deletes my post!

Any chance we can email?

Hi you should be able to DM me on here - regarding the documentation for 2022-10, this isn’t stored on shopify.dev, we only display the active versions docs.

Hi Liam,

Sorry I’m new here, so please be patient with my noob questions: I don’t
know what you mean by “DM me on here” - I cannot find a Direct Message
button anywhere on the Shopify Community forums. So, I’m going to try
replying to this notification by clicking Reply.

Perhaps you’re not using the versions that I provided because it seems
you’re still referring to deprecated methods.

Also, you provided two code snippets but I don’t understand which one you
propose to use in production code and which one to use in my tests. You
refer to the first snippet as “test setup” but I think what you mean by
“create a session explicitly” is not to rely on the shopify_api gem to do
it. Is this correct?

Please clarify and point to the Shopify.dev docs as to where I can find
these code samples.

I do see in
shopify_app/lib/shopify_app/controller_concerns/login_protection.rb the
methods

activate_shopify_session
current_shopify_session

There’s also a private method load_current_session but I don’t think I
should use this.

I tried your suggestion, so I got:

Failures:

  1. account collections creating first account collection
    Failure/Error:
    session = ShopifyAPI::Session.new(
    domain: account.shopify_domain,
    token: account.shopify_token,
    api_version: ‘2023-04’
    )

NameError:
uninitialized constant ShopifyAPI::Session

So, I’m guessing you mean Shopify::Auth::Session

trying again:

Failures:

  1. account collections creating first account collection
    Failure/Error:
    session = ShopifyAPI::*Auth::*Session.new(
    domain: account.shopify_domain,
    token: account.shopify_token,
    api_version: ‘2023-04’
    )

ArgumentError:
missing keyword: :shop

trying again:

Failures:

  1. account collections creating first account collection
    Failure/Error:
    session = ShopifyAPI::Auth::Session.new(
    shop: account.shopify_domain,
    domain: account.shopify_domain,
    token: account.shopify_token,
    api_version: ‘2023-04’
    )

ArgumentError:
unknown keywords: :domain, :token, :api_version

removing the unknown keywords:

Failures:

  1. account collections creating first account collection
    Failure/Error: ShopifyAPI::Base.activate_session(session)

NameError:
uninitialized constant ShopifyAPI::Base
Did you mean? Base64

Removing the call to ShopifyAPI::Base.activate_session(session)

I get:

Failures:

  1. account collections creating first account collection
    Failure/Error: expect(current_path).to eq(‘/settings’)

expected: “/settings”
got: “/login”

(compared using ==)

So, I’m. guessing I’m still not authenticated and I’m being redirected to
the /login page.

Please help.


I also tried the following:

session = ShopifyAPI::Auth::Session.new(
shop: account.shopify_domain,
scope: ShopifyApp.configuration.scope
)

allow_any_instance_of(ShopifyApp::LoginProtection).to(
receive(:current_shopify_session).and_return(session)
)

but I also get redirected to the /login page.

Thanks!

Any news?