diff --git a/CHANGELOG.md b/CHANGELOG.md index ce0cab19..10c8a398 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 62b50b02..3bf4d779 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 ba360e58..b3ff3dae 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 289ac450..78323957 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