From 2b7c1f53e54c0d2c7ee112a63f5fb3e36d8161bd Mon Sep 17 00:00:00 2001 From: Paulo Margarido <64600052+paulomarg@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:22:04 -0400 Subject: [PATCH] Add debug option to GraphQL requests --- CHANGELOG.md | 2 ++ lib/shopify_api/clients/graphql/client.rb | 14 +++++++++++-- lib/shopify_api/clients/graphql/storefront.rb | 12 +++++++++-- test/test_helpers/graphql_client.rb | 20 +++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce0cab193..10c8a398f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api ## Unreleased +- [#1327](https://github.com/Shopify/shopify-api-ruby/pull/1327) Support `?debug=true` parameter in GraphQL client requests + ## 14.4.0 - [#1325](https://github.com/Shopify/shopify-api-ruby/pull/1325) Add support for 2024-07 API version diff --git a/lib/shopify_api/clients/graphql/client.rb b/lib/shopify_api/clients/graphql/client.rb index 62b50b026..3bf4d7798 100644 --- a/lib/shopify_api/clients/graphql/client.rb +++ b/lib/shopify_api/clients/graphql/client.rb @@ -29,14 +29,24 @@ def initialize(session:, base_path:, api_version: nil) headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]), tries: Integer, response_as_struct: T.nilable(T::Boolean), + debug: T::Boolean, ).returns(HttpResponse) end - def query(query:, variables: nil, headers: nil, tries: 1, response_as_struct: Context.response_as_struct) + def query( + query:, + variables: nil, + headers: nil, + tries: 1, + response_as_struct: Context.response_as_struct, + debug: false + ) body = { query: query, variables: variables } + search_params = debug ? "?debug=true" : "" + @http_client.request( HttpRequest.new( http_method: :post, - path: "#{@api_version}/graphql.json", + path: "#{@api_version}/graphql.json#{search_params}", body: body, query: nil, extra_headers: headers, diff --git a/lib/shopify_api/clients/graphql/storefront.rb b/lib/shopify_api/clients/graphql/storefront.rb index ba360e58d..b3ff3dae3 100644 --- a/lib/shopify_api/clients/graphql/storefront.rb +++ b/lib/shopify_api/clients/graphql/storefront.rb @@ -46,12 +46,20 @@ def initialize(shop, storefront_access_token = nil, private_token: nil, public_t headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]), tries: Integer, response_as_struct: T.nilable(T::Boolean), + debug: T::Boolean, ).returns(HttpResponse) end - def query(query:, variables: nil, headers: {}, tries: 1, response_as_struct: Context.response_as_struct) + def query( + query:, + variables: nil, + headers: {}, + tries: 1, + response_as_struct: Context.response_as_struct, + debug: false + ) T.must(headers).merge!({ @storefront_auth_header => @storefront_access_token }) super(query: query, variables: variables, headers: headers, tries: tries, - response_as_struct: response_as_struct) + response_as_struct: response_as_struct, debug: debug) end end end diff --git a/test/test_helpers/graphql_client.rb b/test/test_helpers/graphql_client.rb index 289ac4508..783239575 100644 --- a/test/test_helpers/graphql_client.rb +++ b/test/test_helpers/graphql_client.rb @@ -83,5 +83,25 @@ def test_can_override_api_version @client.query(query: query, variables: variables) end + + def test_can_make_debug_requests + query = <<~QUERY + { + shop { + name + } + } + QUERY + body = { query: query, variables: nil } + success_body = { "success" => true } + response_headers = { "content-type" => "application/json" } + + stub_request(:post, "https://test-shop.myshopify.com/#{@path}/#{ShopifyAPI::Context.api_version}/graphql.json?debug=true") + .with(body: body, headers: @expected_headers) + .to_return(body: success_body.to_json, headers: response_headers) + + response = @client.query(query: query, debug: true) + assert_equal(success_body, response.body) + end end end