Solved

How to access GraphQL from separate microservice using NodeJS SDK

frank_meyer_ham
Visitor
2 1 0

Hey there,

we are currently building an Shopify app which includes the functionality to export a list of products. We want to implement this functionality into a separate microservice that will be running on a cronjob inside a separate docker container so that we can put the load off our main app server.

 

How can we authenticate and access the Shopify GraphQL API using this separate NodeJS service, running independent from the normal Shopify NodeJS app and that will not be accessed from the browser (i.e. can't use cookies eather)? We can't seem to find existing documentation on this topic.

 

We have implemented a custom Shopify.Session.CustomSessionStorage that is saving the session data into a redis DB so that our microservice can access them too.

Our microservice is now initializing Shopify with the same env variables 

 

Shopify.Context.initialize({
  API_KEY: process.env.SHOPIFY_API_KEY!,
  API_SECRET_KEY: process.env.SHOPIFY_API_SECRET!,
  SCOPES: process.env.SCOPES!.split(","),
  HOST_NAME: process.env.HOST!.replace(/https:\/\//, ""),
  API_VERSION: ApiVersion.October21,
  IS_EMBEDDED_APP: true,
  SESSION_STORAGE: new Shopify.Session.CustomSessionStorage(
    sessionStorage.store,
    sessionStorage.load,
    sessionStorage.destroy
  ),
});

 

 

Looking at the library we tried using "Shopify.Utils.loadOfflineSession" to load the session data from our redis:

 

const session = await Shopify.Utils.loadOfflineSession(myShopifyDomain);

 

 But `session` will always return `undefined`, even when the session is stored in Redis.

 

Unfortunately, the sessions in the Redis DB also all seem to expire after only a few hours to 1 day - is there a method that allows long-term authentication so our cronjob can run automatically?

Accepted Solution (1)

frank_meyer_ham
Visitor
2 1 0

This is an accepted solution.

Its always just after posting your question when you find the answer yourself.

 

For anyone having the same problem:
What you are searching for is an offline access token that allows you to access the Shop's API from any server and for longer amounts of time. Here's a good blog article explaining how you do it: https://rsirokov.medium.com/shopify-app-online-and-offline-access-modes-8a8c5ecd928b

View solution in original post

Reply 1 (1)

frank_meyer_ham
Visitor
2 1 0

This is an accepted solution.

Its always just after posting your question when you find the answer yourself.

 

For anyone having the same problem:
What you are searching for is an offline access token that allows you to access the Shop's API from any server and for longer amounts of time. Here's a good blog article explaining how you do it: https://rsirokov.medium.com/shopify-app-online-and-offline-access-modes-8a8c5ecd928b