Skip to content

Commit 860c0ac

Browse files
Merge pull request #1691 from apollographql/fix/schema-from-registry
Add handling for downloading schema from registry to Swift Scripting
2 parents 67a3ded + 3ae6da2 commit 860c0ac

3 files changed

Lines changed: 107 additions & 25 deletions

File tree

Sources/ApolloCodegenLib/ApolloSchemaOptions.swift

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,48 @@ public struct ApolloSchemaOptions {
99
case schemaDefinitionLanguage = "graphql"
1010
}
1111

12-
let apiKey: String?
13-
let endpointURL: URL
12+
/// How to attempt to download your schema
13+
public enum DownloadMethod: Equatable {
14+
15+
case registry(_ settings: RegistrySettings)
16+
/// - endpointURL: The endpoint to hit to download your schema.
17+
case introspection(endpointURL: URL)
18+
19+
public struct RegistrySettings: Equatable {
20+
public let apiKey: String
21+
public let graphID: String
22+
public let variant: String?
23+
24+
/// Designated initializer
25+
///
26+
/// - Parameters:
27+
/// - apiKey: The API key to use when retrieving your schema.
28+
/// - graphID: The identifier of the graph to fetch. Can be found in Apollo Studio.
29+
/// - variant: [Optional] The variant of the graph to fetch. Defaults to nil, which will return whatever is set to the current variant.
30+
public init(apiKey: String,
31+
graphID: String,
32+
variant: String? = nil) {
33+
self.apiKey = apiKey
34+
self.graphID = graphID
35+
self.variant = variant
36+
}
37+
}
38+
39+
public static func == (lhs: DownloadMethod, rhs: DownloadMethod) -> Bool {
40+
switch (lhs, rhs) {
41+
case (.introspection(let lhsURL), introspection(let rhsURL)):
42+
return lhsURL == rhsURL
43+
case (.registry(let lhsSettings),
44+
.registry(let rhsSettings)):
45+
return lhsSettings == rhsSettings
46+
default:
47+
return false
48+
}
49+
}
50+
51+
}
52+
53+
let downloadMethod: DownloadMethod
1454
let headers: [String]
1555
let outputURL: URL
1656

@@ -21,21 +61,18 @@ public struct ApolloSchemaOptions {
2161
/// - Parameters:
2262
/// - schemaFileName: The name, without an extension, for your schema file. Defaults to `"schema"`
2363
/// - schemaFileType: The `SchemaFileType` to download the schema as. Defaults to `.json`.
24-
/// - apiKey: [optional] The API key to use when retrieving your schema. Defaults to nil.
25-
/// - endpointURL: The endpoint to hit to download your schema.
64+
/// - downloadMethod: How to download your schema.
2665
/// - headers: [optional] Any additional headers to include when retrieving your schema. Defaults to nil
2766
/// - outputFolderURL: The URL of the folder in which the downloaded schema should be written
2867
/// - downloadTimeout: The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
2968
public init(schemaFileName: String = "schema",
3069
schemaFileType: SchemaFileType = .json,
31-
apiKey: String? = nil,
32-
endpointURL: URL,
70+
downloadMethod: DownloadMethod,
3371
headers: [String] = [],
3472
outputFolderURL: URL,
3573
downloadTimeout: Double = 30.0) {
36-
self.apiKey = apiKey
74+
self.downloadMethod = downloadMethod
3775
self.headers = headers
38-
self.endpointURL = endpointURL
3976
self.outputURL = outputFolderURL.appendingPathComponent("\(schemaFileName).\(schemaFileType.rawValue)")
4077

4178
self.downloadTimeout = downloadTimeout
@@ -44,11 +81,17 @@ public struct ApolloSchemaOptions {
4481
var arguments: [String] {
4582
var arguments = [
4683
"client:download-schema",
47-
"--endpoint=\(self.endpointURL.absoluteString)"
4884
]
4985

50-
if let key = self.apiKey {
51-
arguments.append("--key=\(key)")
86+
switch self.downloadMethod {
87+
case .introspection(let endpointURL):
88+
arguments.append("--endpoint=\(endpointURL.absoluteString)")
89+
case .registry(let settings):
90+
arguments.append("--key=\(settings.apiKey)")
91+
arguments.append("--graph=\(settings.graphID)")
92+
if let providedVariant = settings.variant {
93+
arguments.append("--variant=\(providedVariant)")
94+
}
5295
}
5396

5497
arguments.append("'\(outputURL.path)'")

SwiftScripts/Sources/SchemaDownload/main.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ let output = sourceRootURL
2020
.appendingPathComponent("Sources")
2121
.appendingPathComponent("UploadAPI")
2222

23+
// Introspection download:
2324
let options = ApolloSchemaOptions(schemaFileName: "schema",
24-
endpointURL: endpoint,
25+
downloadMethod: .introspection(endpointURL: endpoint),
2526
outputFolderURL: output)
2627

28+
// Registry download:
29+
//let registrySettings = ApolloSchemaOptions.DownloadMethod.RegistrySettings(apiKey: <#Replace Me For Testing#>,
30+
// graphID: "Apollo-Fullstack-8zo5jl")
31+
//
32+
//let options = ApolloSchemaOptions(schemaFileName: "schema",
33+
// schemaFileType: .schemaDefinitionLanguage,
34+
// downloadMethod: .registry(registrySettings),
35+
// outputFolderURL: output)
36+
2737
do {
2838
try ApolloSchemaDownloader.run(with: cliFolderURL,
2939
options: options)

Tests/ApolloCodegenTests/ApolloSchemaTests.swift

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import ApolloTestSupport
1212

1313
class ApolloSchemaTests: XCTestCase {
1414

15-
func testCreatingOptionsWithDefaultParameters() throws {
15+
func testCreatingIntrospectionOptionsWithDefaultParameters() throws {
1616
let sourceRoot = CodegenTestHelper.sourceRootURL()
17-
let options = ApolloSchemaOptions(endpointURL: TestURL.starWarsServer.url,
17+
18+
let options = ApolloSchemaOptions(downloadMethod: .introspection(endpointURL: TestURL.starWarsServer.url),
1819
outputFolderURL: sourceRoot)
1920

2021
let expectedOutputURL = sourceRoot.appendingPathComponent("schema.json")
21-
XCTAssertEqual(options.endpointURL, TestURL.starWarsServer.url)
22+
23+
XCTAssertEqual(options.downloadMethod, .introspection(endpointURL: TestURL.starWarsServer.url))
2224
XCTAssertEqual(options.outputURL, expectedOutputURL)
23-
XCTAssertNil(options.apiKey)
2425
XCTAssertTrue(options.headers.isEmpty)
2526

2627
XCTAssertEqual(options.arguments, [
@@ -29,31 +30,59 @@ class ApolloSchemaTests: XCTestCase {
2930
"'\(expectedOutputURL.path)'"
3031
])
3132
}
32-
33-
func testCreatingOptionsWithAllParameters() throws {
33+
34+
func testCreatingRegistryOptionsWithDefaultParameters() throws {
35+
let sourceRoot = CodegenTestHelper.sourceRootURL()
36+
let apiKey = "Fake_API_Key"
37+
let graphID = "Fake_Graph_ID"
38+
39+
let settings = ApolloSchemaOptions.DownloadMethod.RegistrySettings(apiKey: apiKey, graphID: graphID)
40+
41+
let options = ApolloSchemaOptions(downloadMethod: .registry(settings),
42+
outputFolderURL: sourceRoot)
43+
44+
let expectedOutputURL = sourceRoot.appendingPathComponent("schema.json")
45+
46+
XCTAssertEqual(options.downloadMethod, .registry(settings))
47+
XCTAssertEqual(options.outputURL, expectedOutputURL)
48+
XCTAssertTrue(options.headers.isEmpty)
49+
50+
XCTAssertEqual(options.arguments, [
51+
"client:download-schema",
52+
"--key=\(apiKey)",
53+
"--graph=\(graphID)",
54+
"'\(expectedOutputURL.path)'"
55+
])
56+
}
57+
58+
func testCreatingRegistryOptionsWithAllParameters() throws {
3459
let sourceRoot = CodegenTestHelper.sourceRootURL()
3560
let apiKey = "Fake_API_Key"
61+
let graphID = "Fake_Graph_ID"
62+
let variant = "Fake_Variant"
3663
let firstHeader = "Authorization: Bearer tokenGoesHere"
3764
let secondHeader = "Custom-Header: Custom_Customer"
3865
let headers = [firstHeader, secondHeader]
3966

67+
let settings = ApolloSchemaOptions.DownloadMethod.RegistrySettings(apiKey: apiKey,
68+
graphID: graphID, variant: variant)
69+
4070
let options = ApolloSchemaOptions(schemaFileName: "different_name",
4171
schemaFileType: .schemaDefinitionLanguage,
42-
apiKey: apiKey,
43-
endpointURL: TestURL.starWarsServer.url,
72+
downloadMethod: .registry(settings),
4473
headers: headers,
4574
outputFolderURL: sourceRoot)
46-
XCTAssertEqual(options.apiKey, apiKey)
47-
XCTAssertEqual(options.endpointURL, TestURL.starWarsServer.url)
75+
XCTAssertEqual(options.downloadMethod, .registry(settings))
4876
XCTAssertEqual(options.headers, headers)
4977

5078
let expectedOutputURL = sourceRoot.appendingPathComponent("different_name.graphql")
5179
XCTAssertEqual(options.outputURL, expectedOutputURL)
5280

5381
XCTAssertEqual(options.arguments, [
5482
"client:download-schema",
55-
"--endpoint=http://localhost:8080/graphql",
5683
"--key=\(apiKey)",
84+
"--graph=\(graphID)",
85+
"--variant=\(variant)",
5786
"'\(expectedOutputURL.path)'",
5887
"--header='\(firstHeader)'",
5988
"--header='\(secondHeader)'"
@@ -63,7 +92,7 @@ class ApolloSchemaTests: XCTestCase {
6392
func testDownloadingSchemaAsJSON() throws {
6493
let testOutputFolderURL = CodegenTestHelper.outputFolderURL()
6594

66-
let options = ApolloSchemaOptions(endpointURL: TestURL.starWarsServer.url,
95+
let options = ApolloSchemaOptions(downloadMethod: .introspection(endpointURL: TestURL.starWarsServer.url),
6796
outputFolderURL: testOutputFolderURL)
6897

6998
// Delete anything existing at the output URL
@@ -97,7 +126,7 @@ class ApolloSchemaTests: XCTestCase {
97126
let testOutputFolderURL = CodegenTestHelper.outputFolderURL()
98127

99128
let options = ApolloSchemaOptions(schemaFileType: .schemaDefinitionLanguage,
100-
endpointURL: TestURL.starWarsServer.url,
129+
downloadMethod: .introspection(endpointURL: TestURL.starWarsServer.url),
101130
outputFolderURL: testOutputFolderURL)
102131

103132
// Delete anything existing at the output URL

0 commit comments

Comments
 (0)