Skip to content

Commit

Permalink
Add api_version param to Graphql::Client#query
Browse files Browse the repository at this point in the history
This optional param allow the default value inherited from Context to
be overridden on a per-query basis.
  • Loading branch information
ewalk153 committed Jan 21, 2023
1 parent 37e31e3 commit 6b6546e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/shopify_api/clients/graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ def initialize(session:, base_path:)
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: Integer,
api_version: T.nilable(String),
).returns(HttpResponse)
end
def query(query:, variables: nil, headers: nil, tries: 1)
def query(query:, variables: nil, headers: nil, tries: 1, api_version: nil)
body = { query: query, variables: variables }
api_version ||= Context.api_version
@http_client.request(
HttpRequest.new(
http_method: :post,
path: "#{Context.api_version}/graphql.json",
path: "#{api_version}/graphql.json",
body: body,
query: nil,
extra_headers: headers,
Expand Down
5 changes: 3 additions & 2 deletions lib/shopify_api/clients/graphql/storefront.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ def initialize(shop, storefront_access_token)
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
tries: Integer,
api_version: T.nilable(String),
).returns(HttpResponse)
end
def query(query:, variables: nil, headers: {}, tries: 1)
def query(query:, variables: nil, headers: {}, tries: 1, api_version: nil)
T.must(headers).merge!({ "X-Shopify-Storefront-Access-Token": @storefront_access_token })
super(query: query, variables: variables, headers: headers, tries: tries)
super(query: query, variables: variables, headers: headers, tries: tries, api_version: api_version)
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions test/test_helpers/graphql_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,35 @@ def test_can_make_query_with_variables

@client.query(query: query, variables: variables)
end

def test_can_override_api_version
query = <<~QUERY
query myTestQuery($first: Int) {
products (first: $first) {
edges {
node {
id
title
descriptionHtml
}
}
}
}
QUERY
variables = {
first: 10,
}
setup
api_version = "2022-01"
body = { query: query, variables: variables }
success_body = { "success" => true }
response_headers = { "content-type" => "application/json" }

stub_request(:post, "https://test-shop.myshopify.com/#{@path}/#{api_version}/graphql.json")
.with(body: body, headers: @expected_headers)
.to_return(body: success_body.to_json, headers: response_headers)

@client.query(query: query, variables: variables, api_version: api_version)
end
end
end

0 comments on commit 6b6546e

Please sign in to comment.