Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dot notation accessing for graphql responses #1246

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/shopify_api/clients/http_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HttpResponse
sig { returns(T::Hash[String, T::Array[String]]) }
attr_reader :headers

sig { returns(T.any(T::Hash[String, T.untyped], String)) }
sig { returns(T.any(T::Hash[String, T.untyped], String, OpenStruct)) }
attr_reader :body

sig { returns(T.nilable(String)) }
Expand All @@ -28,7 +28,12 @@ class HttpResponse
def initialize(code:, headers:, body:)
@code = code
@headers = headers
@body = body
@body = T.let(body, T.any(OpenStruct, T::Hash[String, T.untyped], String))

if Context.graphql_response_object && body.is_a?(Hash)
json_body = body.to_json
+ @body = JSON.parse(json_body, object_class: OpenStruct)
end

@prev_page_info = T.let(nil, T.nilable(String))
@next_page_info = T.let(nil, T.nilable(String))
Expand Down
9 changes: 8 additions & 1 deletion lib/shopify_api/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Context
@active_session = T.let(Concurrent::ThreadLocalVar.new { nil }, T.nilable(Concurrent::ThreadLocalVar))
@user_agent_prefix = T.let(nil, T.nilable(String))
@old_api_secret_key = T.let(nil, T.nilable(String))
@graphql_response_object = T.let(false, T.nilable(T::Boolean))

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

Expand All @@ -43,6 +44,7 @@ class << self
user_agent_prefix: T.nilable(String),
old_api_secret_key: T.nilable(String),
api_host: T.nilable(String),
graphql_response_object: T.nilable(T::Boolean),
).void
end
def setup(
Expand All @@ -59,7 +61,8 @@ def setup(
private_shop: nil,
user_agent_prefix: nil,
old_api_secret_key: nil,
api_host: nil
api_host: nil,
graphql_response_object: false
)
unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
raise Errors::UnsupportedVersionError,
Expand All @@ -78,6 +81,7 @@ def setup(
@private_shop = private_shop
@user_agent_prefix = user_agent_prefix
@old_api_secret_key = old_api_secret_key
@graphql_response_object = graphql_response_object
@log_level = if valid_log_level?(log_level)
log_level.to_sym
else
Expand Down Expand Up @@ -128,6 +132,9 @@ def load_rest_resources(api_version:)
sig { returns(Symbol) }
attr_reader :log_level

sig { returns T.nilable(T::Boolean) }
attr_reader :graphql_response_object

sig { returns(T::Boolean) }
def private?
@is_private
Expand Down
9 changes: 9 additions & 0 deletions test/clients/http_response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def test_next_and_prev
assert_equal("page-info", response.prev_page_info)
assert_equal("other-page-info", response.next_page_info)
end

def test_object_response
modify_context(graphql_response_object: true)

# We need to verify that the response body is an OpenStruct object
response = ShopifyAPI::Clients::HttpResponse.new(code: 200, headers: {}, body: { "key" => { "nested_key" => "nested_value" }})
assert_kind_of(OpenStruct, response.body)
assert_equal("nested_value", response.body.key.nested_key)
end
end
end
end
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def setup
private_shop: T.nilable(String),
user_agent_prefix: T.nilable(String),
old_api_secret_key: T.nilable(String),
graphql_response_object: T.nilable(T::Boolean),
).void
end
def modify_context(
Expand All @@ -64,7 +65,8 @@ def modify_context(
logger: nil,
private_shop: "do-not-set",
user_agent_prefix: nil,
old_api_secret_key: nil
old_api_secret_key: nil,
graphql_response_object: nil
)
ShopifyAPI::Context.setup(
api_key: api_key ? api_key : ShopifyAPI::Context.api_key,
Expand All @@ -79,6 +81,7 @@ def modify_context(
user_agent_prefix: user_agent_prefix ? user_agent_prefix : ShopifyAPI::Context.user_agent_prefix,
old_api_secret_key: old_api_secret_key ? old_api_secret_key : ShopifyAPI::Context.old_api_secret_key,
log_level: :off,
graphql_response_object: graphql_response_object || ShopifyAPI::Context.graphql_response_object,
)
end
end
Expand Down