Skip to content

Commit

Permalink
Add debug option to GraphQL requests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Jul 9, 2024
1 parent ebf1b60 commit 7318a71
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
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 7318a71

Please sign in to comment.