Shopify / shopify-api-ruby

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

Adds api version configuration example for custom app #1330

Closed sle-c closed 1 month ago

sle-c commented 1 month ago

Description

Fixes https://github.com/Shopify/shopify-api-ruby/issues/1329

How has this been tested?

Script for graphql admin client with api version

require "shopify_api"

access_token = "shpat"
shop = "test.myshopify.com"

# Make the GraphQL query string
query = <<~QUERY
  {
    products(first: 10) {
      edges {
        cursor
        node {
          id
          title
          onlineStoreUrl
        }
      }
    }
  }
QUERY

session = ShopifyAPI::Auth::Session.new(
  shop: shop,
  access_token:
)

graphql_client = ShopifyAPI::Clients::Graphql::Admin.new(session: session, api_version: "unstable")
response = graphql_client.query(query: query)

puts response.body['data']

Script for REST

require "shopify_api"

access_token = "shpat"
shop = "test.myshopify.com"
session = ShopifyAPI::Auth::Session.new(
  shop: shop,
  access_token:
)
# use admin rest client to specify api version
rest_client = ShopifyAPI::Clients::Rest::Admin.new(session: session, api_version: "2024-01")
response = rest_client.get(path: "products")
p response

# use ShopifyApi::Context to specify api version
ShopifyAPI::Context.setup(
  api_key: "<api-key>",
  api_secret_key: "<api-secret-key>",
  scope: "read_orders,read_products,etc",
  is_embedded: true, # Set to true if you are building an embedded app
  api_version: "2024-01", # The version of the API you would like to use
  is_private: false, # Set to true if you have an existing private app
)
products = ShopifyAPI::Product.find_all
p products

Checklist: