Skip to content

Commit

Permalink
Merge pull request #1687 from min/min/fixes/existing-query-params
Browse files Browse the repository at this point in the history
Fixes an issue where existing query params on graphQLEndpoint are ignored
  • Loading branch information
designatednerd authored Feb 26, 2021
2 parents cd9a77b + 16adf9c commit 083c853
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Apollo/GraphQLGETTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct GraphQLGETTransformer {
return nil
}

var queryItems: [URLQueryItem] = []
var queryItems: [URLQueryItem] = components.queryItems ?? []

do {
_ = try self.body.sorted(by: {$0.key < $1.key}).compactMap({ arg in
Expand Down
17 changes: 17 additions & 0 deletions Tests/ApolloTests/GETTransformerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,21 @@ class GETTransformerTests: XCTestCase {
let queryString = url?.absoluteString == "http://localhost:8080/graphql?extensions=%7B%22persistedQuery%22:%7B%22sha256Hash%22:%22f6e76545cd03aa21368d9969cb39447f6e836a16717823281803778e7805d671%22,%22version%22:1%7D%7D&query=query%20HeroName($episode:%20Episode)%20%7B%0A%20%20hero(episode:%20$episode)%20%7B%0A%20%20%20%20__typename%0A%20%20%20%20name%0A%20%20%7D%0A%7D&variables=%7B%22episode%22:null%7D"
XCTAssertTrue(queryString)
}

func testEncodingQueryWithExistingParameters() throws {
let operation = HeroNameQuery(episode: .empire)
let body = requestBodyCreator.requestBody(for: operation,
sendOperationIdentifiers: false,
sendQueryDocument: true,
autoPersistQuery: false)

var components = URLComponents(string: url.absoluteString)!
components.queryItems = [URLQueryItem(name: "foo", value: "bar")]

let transformer = GraphQLGETTransformer(body: body, url: components.url!)

let url = transformer.createGetURL()

XCTAssertEqual(url?.absoluteString, "http://localhost:8080/graphql?foo=bar&operationName=HeroName&query=query%20HeroName($episode:%20Episode)%20%7B%0A%20%20hero(episode:%20$episode)%20%7B%0A%20%20%20%20__typename%0A%20%20%20%20name%0A%20%20%7D%0A%7D&variables=%7B%22episode%22:%22EMPIRE%22%7D")
}
}

0 comments on commit 083c853

Please sign in to comment.