Skip to content

Commit

Permalink
Move api_version to client
Browse files Browse the repository at this point in the history
This matches the approach taken the js client and will serve more
use cases than on the query.
  • Loading branch information
ewalk153 committed Jan 23, 2023
1 parent 6f0cd85 commit 042a69a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/shopify_api/clients/graphql/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module ShopifyAPI
module Clients
module Graphql
class Admin < Client
sig { params(session: T.nilable(Auth::Session)).void }
def initialize(session:)
super(session: session, base_path: "/admin/api")
sig { params(session: T.nilable(Auth::Session), api_version: T.nilable(String)).void }
def initialize(session:, api_version: nil)
super(session: session, base_path: "/admin/api", api_version: api_version)
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/shopify_api/clients/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module Graphql
class Client
extend T::Sig

sig { params(session: T.nilable(Auth::Session), base_path: String).void }
def initialize(session:, base_path:)
sig { params(session: T.nilable(Auth::Session), base_path: String, api_version: T.nilable(String)).void }
def initialize(session:, base_path:, api_version: nil)
@http_client = T.let(HttpClient.new(session: session, base_path: base_path), HttpClient)
@api_version = T.let(api_version || Context.api_version, String)
end

sig do
Expand All @@ -18,16 +19,14 @@ def initialize(session:, base_path:)
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: Integer,
api_version: T.nilable(String),
).returns(HttpResponse)
end
def query(query:, variables: nil, headers: nil, tries: 1, api_version: nil)
def query(query:, variables: nil, headers: nil, tries: 1)
body = { query: query, variables: variables }
api_version ||= Context.api_version
@http_client.request(
HttpRequest.new(
http_method: :post,
path: "#{api_version}/graphql.json",
path: "#{@api_version}/graphql.json",
body: body,
query: nil,
extra_headers: headers,
Expand Down
11 changes: 5 additions & 6 deletions lib/shopify_api/clients/graphql/storefront.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ module ShopifyAPI
module Clients
module Graphql
class Storefront < Client
sig { params(shop: String, storefront_access_token: String).void }
def initialize(shop, storefront_access_token)
sig { params(shop: String, storefront_access_token: String, api_version: T.nilable(String)).void }
def initialize(shop, storefront_access_token, api_version: nil)
session = Auth::Session.new(
id: shop,
shop: shop,
access_token: "",
is_online: false,
)
super(session: session, base_path: "/api")
super(session: session, base_path: "/api", api_version: api_version)
@storefront_access_token = storefront_access_token
end

Expand All @@ -23,12 +23,11 @@ def initialize(shop, storefront_access_token)
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: Integer,
api_version: T.nilable(String),
).returns(HttpResponse)
end
def query(query:, variables: nil, headers: {}, tries: 1, api_version: nil)
def query(query:, variables: nil, headers: {}, tries: 1)
T.must(headers).merge!({ "X-Shopify-Storefront-Access-Token": @storefront_access_token })
super(query: query, variables: variables, headers: headers, tries: tries, api_version: api_version)
super(query: query, variables: variables, headers: headers, tries: tries)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/shopify_api/clients/rest/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module Rest
class Admin < HttpClient
extend T::Sig

sig { params(session: T.nilable(Auth::Session)).void }
def initialize(session: nil)
super(session: session, base_path: "/admin/api/#{Context.api_version}")
sig { params(session: T.nilable(Auth::Session), api_version: T.nilable(String)).void }
def initialize(session: nil, api_version: nil)
@api_version = T.let(api_version || Context.api_version, String)
super(session: session, base_path: "/admin/api/#{@api_version}")
end

sig do
Expand Down
10 changes: 9 additions & 1 deletion test/clients/graphql/admin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ def setup
super
@session = ShopifyAPI::Auth::Session.new(shop: "test-shop.myshopify.com",
access_token: SecureRandom.alphanumeric(10))
@client = ShopifyAPI::Clients::Graphql::Admin.new(session: @session)
@client = build_client
@path = "admin/api"
@expected_headers = TestHelpers::Constants::DEFAULT_CLIENT_HEADERS.merge({
"X-Shopify-Access-Token": @session.access_token,
})
end

def build_client
if !defined?("@api_version")
ShopifyAPI::Clients::Graphql::Admin.new(session: @session)
else
ShopifyAPI::Clients::Graphql::Admin.new(session: @session, api_version: @api_version)
end
end
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion test/clients/graphql/storefront_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ def setup
super
@shop = "test-shop.myshopify.com"
@storefront_access_token = SecureRandom.alphanumeric(10)
@client = ShopifyAPI::Clients::Graphql::Storefront.new(@shop, @storefront_access_token)
@client = build_client
@path = "api"
@expected_headers = TestHelpers::Constants::DEFAULT_CLIENT_HEADERS.merge({
"X-Shopify-Storefront-Access-Token": @storefront_access_token,
})
end

def build_client
if !defined?("@api_version")
ShopifyAPI::Clients::Graphql::Storefront.new(@shop, @storefront_access_token)
else
ShopifyAPI::Clients::Graphql::Storefront.new(@shop, @storefront_access_token, api_version: @api_version)
end
end
end
end
end
Expand Down
15 changes: 13 additions & 2 deletions test/clients/rest/admin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ def test_path_starting_at_admin_overrides_default
assert_equal(response_headers.to_h { |k, v| [k, [v]] }, response.headers)
end

def test_api_version_can_be_overrriden
@api_version = "2022-07"
run_test(:post)
end

private

def run_test(http_method, path = "/some-path", expected_path = "some-path.json")
session = ShopifyAPI::Auth::Session.new(shop: "test-shop.myshopify.com",
access_token: SecureRandom.alphanumeric(10))
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)
client = if defined?(@api_version)
ShopifyAPI::Clients::Rest::Admin.new(session: session, api_version: @api_version)
else
ShopifyAPI::Clients::Rest::Admin.new(session: session)
end

request = {
path: path,
Expand All @@ -66,9 +75,11 @@ def run_test(http_method, path = "/some-path", expected_path = "some-path.json")
success_body = { "success" => true }
response_headers = { "content-type" => "application/json" }

api_version = defined?(@api_version) ? @api_version : ShopifyAPI::Context.api_version

stub_request(
http_method,
"https://#{session.shop}/admin/api/#{ShopifyAPI::Context.api_version}/#{expected_path}",
"https://#{session.shop}/admin/api/#{api_version}/#{expected_path}",
)
.with(body: request[:body].to_json, query: request[:query], headers: expected_headers)
.to_return(body: success_body.to_json, headers: response_headers)
Expand Down
6 changes: 3 additions & 3 deletions test/test_helpers/graphql_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_can_make_query_with_variables
end

def test_can_override_api_version
@api_version = "2022-01"
query = <<~QUERY
query myTestQuery($first: Int) {
products (first: $first) {
Expand All @@ -72,16 +73,15 @@ def test_can_override_api_version
first: 10,
}
setup
api_version = "2022-01"
body = { query: query, variables: variables }
success_body = { "success" => true }
response_headers = { "content-type" => "application/json" }

stub_request(:post, "https://test-shop.myshopify.com/#{@path}/#{api_version}/graphql.json")
stub_request(:post, "https://test-shop.myshopify.com/#{@path}/#{@api_version}/graphql.json")
.with(body: body, headers: @expected_headers)
.to_return(body: success_body.to_json, headers: response_headers)

@client.query(query: query, variables: variables, api_version: api_version)
@client.query(query: query, variables: variables)
end
end
end

0 comments on commit 042a69a

Please sign in to comment.