Skip to content

Commit

Permalink
Merge pull request #1327 from Shopify/add_debug_graphql_option
Browse files Browse the repository at this point in the history
Add debug option to GraphQL requests
  • Loading branch information
paulomarg authored Jul 10, 2024
2 parents ebf1b60 + 2b7c1f5 commit c029d8a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions lib/shopify_api/clients/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions lib/shopify_api/clients/graphql/storefront.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/test_helpers/graphql_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c029d8a

Please sign in to comment.