Skip to content

Commit aebda76

Browse files
committed
Add config option to disable the REST client
New ruby apps will have this config option turned on As of April 1st 2025 all new apps must use only GraphQL
1 parent 39eda8b commit aebda76

File tree

6 files changed

+109
-1
lines changed

6 files changed

+109
-1
lines changed

lib/shopify_api/clients/rest/admin.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class Admin < HttpClient
99

1010
sig { params(session: T.nilable(Auth::Session), api_version: T.nilable(String)).void }
1111
def initialize(session: nil, api_version: nil)
12+
if Context.rest_disabled
13+
raise Errors::DisabledResourceError,
14+
"The Admin REST API has been deprecated. Please use the GraphQL Admin API. For more information see https://www.shopify.com/ca/partners/blog/all-in-on-graphql"
15+
end
16+
1217
@api_version = T.let(api_version || Context.api_version, String)
1318
if api_version
1419
if api_version == Context.api_version

lib/shopify_api/context.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Context
2222
@user_agent_prefix = T.let(nil, T.nilable(String))
2323
@old_api_secret_key = T.let(nil, T.nilable(String))
2424
@response_as_struct = T.let(false, T.nilable(T::Boolean))
25+
@rest_disabled = T.let(false, T.nilable(T::Boolean))
2526

2627
@rest_resource_loader = T.let(nil, T.nilable(Zeitwerk::Loader))
2728

@@ -45,6 +46,7 @@ class << self
4546
old_api_secret_key: T.nilable(String),
4647
api_host: T.nilable(String),
4748
response_as_struct: T.nilable(T::Boolean),
49+
rest_disabled: T.nilable(T::Boolean),
4850
).void
4951
end
5052
def setup(
@@ -62,7 +64,8 @@ def setup(
6264
user_agent_prefix: nil,
6365
old_api_secret_key: nil,
6466
api_host: nil,
65-
response_as_struct: false
67+
response_as_struct: false,
68+
rest_disabled: false
6669
)
6770
unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
6871
raise Errors::UnsupportedVersionError,
@@ -82,6 +85,7 @@ def setup(
8285
@user_agent_prefix = user_agent_prefix
8386
@old_api_secret_key = old_api_secret_key
8487
@response_as_struct = response_as_struct
88+
@rest_disabled = rest_disabled
8589
@log_level = if valid_log_level?(log_level)
8690
log_level.to_sym
8791
else
@@ -93,6 +97,9 @@ def setup(
9397

9498
sig { params(api_version: String).void }
9599
def load_rest_resources(api_version:)
100+
# Skip loading REST resources if REST is disabled
101+
return if rest_disabled
102+
96103
# Unload any previous instances - mostly useful for tests where we need to reset the version
97104
@rest_resource_loader&.setup
98105
@rest_resource_loader&.unload
@@ -178,6 +185,11 @@ def host_name
178185
T.must(URI(T.must(host)).host)
179186
end
180187

188+
sig { returns(T::Boolean) }
189+
def rest_disabled
190+
T.must(@rest_disabled)
191+
end
192+
181193
private
182194

183195
sig { params(log_level: T.any(Symbol, String)).returns(T::Boolean) }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module ShopifyAPI
5+
module Errors
6+
class DisabledResourceError < StandardError; end
7+
end
8+
end

test/clients/base_rest_resource_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ def setup
1414
ShopifyAPI::Context.load_rest_resources(api_version: ShopifyAPI::Context.api_version)
1515
end
1616

17+
def test_rest_disabled
18+
ShopifyAPI::Context.setup(
19+
api_key: "test-key",
20+
api_secret_key: "test-secret-key",
21+
api_version: "2023-01",
22+
is_private: true,
23+
is_embedded: false,
24+
rest_disabled: true,
25+
)
26+
assert_raises(ShopifyAPI::Errors::DisabledResourceError) do
27+
TestHelpers::FakeResource.find(id: 1, session: @session)
28+
end
29+
30+
ShopifyAPI::Context.setup(
31+
api_key: "test-key",
32+
api_secret_key: "test-secret-key",
33+
api_version: "2023-01",
34+
is_private: true,
35+
is_embedded: false,
36+
rest_disabled: false,
37+
)
38+
end
39+
1740
def test_finds_resource_by_id
1841
body = { fake_resource: { id: 1, attribute: "attribute" } }.to_json
1942

test/clients/rest/admin_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ def test_api_version_can_be_overrriden
5050
run_test(:post)
5151
end
5252

53+
def test_raises_error_when_rest_is_disabled
54+
ShopifyAPI::Context.setup(
55+
api_key: "test-key",
56+
api_secret_key: "test-secret-key",
57+
api_version: "2023-01",
58+
is_private: true,
59+
is_embedded: false,
60+
rest_disabled: true,
61+
)
62+
session = ShopifyAPI::Auth::Session.new(
63+
shop: "test-shop.myshopify.com",
64+
access_token: SecureRandom.alphanumeric(10),
65+
)
66+
67+
assert_raises(ShopifyAPI::Errors::DisabledResourceError, "REST API access has been disabled via Context.rest_disabled") do
68+
ShopifyAPI::Clients::Rest::Admin.new(session: session)
69+
end
70+
end
71+
72+
def test_client_works_normally_when_rest_is_not_disabled
73+
ShopifyAPI::Context.setup(
74+
api_key: "test-key",
75+
api_secret_key: "test-secret-key",
76+
api_version: "2023-01",
77+
is_private: true,
78+
is_embedded: false,
79+
rest_disabled: false,
80+
)
81+
session = ShopifyAPI::Auth::Session.new(
82+
shop: "test-shop.myshopify.com",
83+
access_token: SecureRandom.alphanumeric(10),
84+
)
85+
86+
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)
87+
refute_nil(client)
88+
end
89+
5390
private
5491

5592
def run_test(http_method, path = "/some-path", expected_path = "some-path.json")

test/context_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ def test_scope_config_can_be_optional_and_defaults_to_empty
178178
assert_equal(ShopifyAPI::Auth::AuthScopes.new, ShopifyAPI::Context.scope)
179179
end
180180

181+
def test_rest_disabled_defaults_to_false
182+
ShopifyAPI::Context.setup(
183+
api_key: "test-key",
184+
api_secret_key: "test-secret-key",
185+
api_version: "2023-01",
186+
is_private: true,
187+
is_embedded: false,
188+
)
189+
refute(ShopifyAPI::Context.rest_disabled)
190+
end
191+
192+
def test_rest_disabled_can_be_set_in_setup
193+
ShopifyAPI::Context.setup(
194+
api_key: "test-key",
195+
api_secret_key: "test-secret-key",
196+
api_version: "2023-01",
197+
is_private: true,
198+
is_embedded: false,
199+
rest_disabled: true,
200+
)
201+
assert(ShopifyAPI::Context.rest_disabled)
202+
end
203+
181204
def teardown
182205
ShopifyAPI::Context.deactivate_session
183206
end

0 commit comments

Comments
 (0)