Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple headers #1153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Sources/ApolloCodegenLib/ApolloSchemaOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct ApolloSchemaOptions {

let apiKey: String?
let endpointURL: URL
let header: String?
let headers: [String]
let outputURL: URL

let downloadTimeout: Double
Expand All @@ -23,21 +23,21 @@ public struct ApolloSchemaOptions {
/// - schemaFileType: The `SchemaFileType` to download the schema as. Defaults to `.json`.
/// - apiKey: [optional] The API key to use when retrieving your schema. Defaults to nil.
/// - endpointURL: The endpoint to hit to download your schema.
/// - header: [optional] Any additional headers to include when retrieving your schema. Defaults to nil
/// - headers: [optional] Any additional headers to include when retrieving your schema. Defaults to nil
/// - outputFolderURL: The URL of the folder in which the downloaded schema should be written
/// - downloadTimeout: The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
public init(schemaFileName: String = "schema",
schemaFileType: SchemaFileType = .json,
apiKey: String? = nil,
endpointURL: URL,
header: String? = nil,
headers: [String] = [],
outputFolderURL: URL,
downloadTimeout: Double = 30.0) {
self.apiKey = apiKey
self.header = header
self.headers = headers
self.endpointURL = endpointURL
self.outputURL = outputFolderURL.appendingPathComponent("\(schemaFileName).\(schemaFileType.rawValue)")

self.downloadTimeout = downloadTimeout
}

Expand All @@ -55,8 +55,8 @@ public struct ApolloSchemaOptions {

// Header argument must be last in the CLI command due to an underlying issue in the Oclif framework.
// See: https://github.com/apollographql/apollo-tooling/issues/844#issuecomment-547143805
if let header = self.header {
arguments.append("--header='\(header)'")
for header in headers {
arguments.append("--header='\(header)'")
}

return arguments
Expand Down
13 changes: 8 additions & 5 deletions Tests/ApolloCodegenTests/ApolloSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ApolloSchemaTests: XCTestCase {
XCTAssertEqual(options.endpointURL, self.endpointURL)
XCTAssertEqual(options.outputURL, expectedOutputURL)
XCTAssertNil(options.apiKey)
XCTAssertNil(options.header)
XCTAssertTrue(options.headers.isEmpty)

XCTAssertEqual(options.arguments, [
"client:download-schema",
Expand All @@ -34,17 +34,19 @@ class ApolloSchemaTests: XCTestCase {
func testCreatingOptionsWithAllParameters() throws {
let sourceRoot = CodegenTestHelper.sourceRootURL()
let apiKey = "Fake_API_Key"
let header = "Authorization: Bearer tokenGoesHere"
let firstHeader = "Authorization: Bearer tokenGoesHere"
let secondHeader = "Custom-Header: Custom_Customer"
let headers = [firstHeader, secondHeader]

let options = ApolloSchemaOptions(schemaFileName: "different_name",
schemaFileType: .schemaDefinitionLanguage,
apiKey: apiKey,
endpointURL: self.endpointURL,
header: header,
headers: headers,
outputFolderURL: sourceRoot)
XCTAssertEqual(options.apiKey, apiKey)
XCTAssertEqual(options.endpointURL, self.endpointURL)
XCTAssertEqual(options.header, header)
XCTAssertEqual(options.headers, headers)

let expectedOutputURL = sourceRoot.appendingPathComponent("different_name.graphql")
XCTAssertEqual(options.outputURL, expectedOutputURL)
Expand All @@ -54,7 +56,8 @@ class ApolloSchemaTests: XCTestCase {
"--endpoint=http://localhost:8080/graphql",
"--key=\(apiKey)",
"'\(expectedOutputURL.path)'",
"--header='\(header)'"
"--header='\(firstHeader)'",
"--header='\(secondHeader)'"
])
}

Expand Down