Skip to content

Commit 22e2097

Browse files
Remove dependency on apollo CLI for fetching schema (#1935)
* Pull downloader into its own file * Use URL downloader in CLI downloader * Update headers to take both key and value * Add Apollo-Use-Only method to create a non-type safe GraphQL request * Add methods for direct download from registry and introspection rather than using the JS CLI * Revert "Add Apollo-Use-Only method to create a non-type safe GraphQL request" This reverts commit 9aac6367a611260ffea12e3b0ae965b999d9b9d3. * Pull untyped request body creator into codegen lib * update types on codegen tests * Pull out usage of the typescript CLI 🎉 * Make sure URL request headers are properly set * don't try to import all of apollo 🤦‍♀️ * Make sure keys are sorted * Make sure folder is created before trying to write to it * Update tests * Update that we should really only be getting SDL files, not JSON, because the JSON is so damned huge * start adding and testing downloading the schema from the graph registry * Turns out variant is actually required * fix expected file name * get rid of arguments parameter, update custom debug strings * Turns out you have to _throw_ the error rather than just creating it 🤦‍♀️ * Check that downloaded info from introspection query can be loaded into an actual schema rather than just being json * test that downloaded SDL schema from the Registry can be turned into a schema * Clarify where we're converting from a downloaded Registry file vs a downloaded Introspection file. * first swipe at adding SDL printing for introspection * Updated JS bundle * add tslib to the package.json so it gets pulled in if you haven't globally installed it * actual, successful swipe at getting introspection JSON converted to SDL. * Update integration tests for new schema downloader * Organize code by section * Fix spelling mistake * Clean-up CLIDownloader API and tests * Clean-up URLDownloader and add tests * Ignore URLDownloader test output * Fix library import for ApolloPerformanceTests * Improve test feedback when not setting a request handler * Remove comment about public use of UntypedGraphQLRequestBodyCreator * Improve test names and operation * Refactored ApolloSchemaDownloadConfiguration * Refactor ApolloSchemaDownloader * Update schema download script for changed API * Remove irrelevant schema download integration test * Move linked library from ApolloPerformanceTest target to ApolloTestSupport target * Move MockNetworkSession to ApolloCodegenTestSupport target * Add SDL validation to StarWarsApolloSchemaDownloaderTests * Add registry-based ApolloSchemaDownloader integration test * Finish unit tests for ApolloSchemaDownloader * Add Apollo prefix to 'registry' usage in schema downloader * Add documentation to ApolloSchemaDownloadConfiguration * Fix ApolloServerIntegrationTests for enum value renaming Co-authored-by: Ellen Shapiro <designatednerd@gmail.com>
1 parent 384b7df commit 22e2097

24 files changed

Lines changed: 922 additions & 395 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ scripts/apollo.tar.gz
5858
SwiftScripts/ApolloCLI
5959
Tests/ApolloCodegenTests/scripts
6060
Tests/ApolloCodegenTests/scripts directory
61+
Tests/ApolloCodegenTests/Schema
62+
Tests/ApolloCodegenTests/Output
6163
SwiftScripts/.build-**
6264

6365
# Local Netlify folder
64-
.netlify
66+
.netlify

Apollo.xcodeproj/project.pbxproj

Lines changed: 36 additions & 4 deletions
Large diffs are not rendered by default.

Sources/ApolloCodegenLib/ApolloCLI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public struct ApolloCLI {
2020
let lock = try waitForCLIFolderLock(cliFolderURL: cliFolderURL, timeout: timeout)
2121
defer { lock.unlock() }
2222

23-
try CLIDownloader.downloadIfNeeded(cliFolderURL: cliFolderURL, timeout: timeout)
23+
try CLIDownloader.downloadIfNeeded(to: cliFolderURL, timeout: timeout)
2424

2525
if !(try CLIExtractor.validateSHASUMOfDownloadedFile(in: cliFolderURL)) {
2626
CodegenLogger.log("Downloaded zip file has incorrect SHASUM, forcing redownload")
27-
try CLIDownloader.forceRedownload(cliFolderURL: cliFolderURL, timeout: timeout)
27+
try CLIDownloader.forceRedownload(to: cliFolderURL, timeout: timeout)
2828
}
2929

3030
let binaryFolderURL = try CLIExtractor.extractCLIIfNeeded(from: cliFolderURL)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import Foundation
2+
3+
/// A configuration object that defines behavior for schema download.
4+
public struct ApolloSchemaDownloadConfiguration {
5+
6+
/// How to attempt to download your schema
7+
public enum DownloadMethod: Equatable {
8+
9+
/// The Apollo Schema Registry, which serves as a central hub for managing your data graph.
10+
case apolloRegistry(_ settings: ApolloRegistrySettings)
11+
/// GraphQL Introspection connecting to the specified URL.
12+
case introspection(endpointURL: URL)
13+
14+
public struct ApolloRegistrySettings: Equatable {
15+
/// The API key to use when retrieving your schema from the Apollo Registry.
16+
public let apiKey: String
17+
/// The identifier of the graph to fetch. Can be found in Apollo Studio.
18+
public let graphID: String
19+
/// The variant of the graph in the registry.
20+
public let variant: String?
21+
22+
/// Designated initializer
23+
///
24+
/// - Parameters:
25+
/// - apiKey: The API key to use when retrieving your schema.
26+
/// - graphID: The identifier of the graph to fetch. Can be found in Apollo Studio.
27+
/// - variant: The variant of the graph to fetch. Defaults to "current", which will return whatever is set to the current variant.
28+
public init(apiKey: String,
29+
graphID: String,
30+
variant: String = "current") {
31+
self.apiKey = apiKey
32+
self.graphID = graphID
33+
self.variant = variant
34+
}
35+
}
36+
37+
public static func == (lhs: DownloadMethod, rhs: DownloadMethod) -> Bool {
38+
switch (lhs, rhs) {
39+
case (.introspection(let lhsURL), introspection(let rhsURL)):
40+
return lhsURL == rhsURL
41+
case (.apolloRegistry(let lhsSettings), .apolloRegistry(let rhsSettings)):
42+
return lhsSettings == rhsSettings
43+
default:
44+
return false
45+
}
46+
}
47+
48+
}
49+
50+
public struct HTTPHeader: Equatable, CustomDebugStringConvertible {
51+
let key: String
52+
let value: String
53+
54+
public var debugDescription: String {
55+
"\(key): \(value)"
56+
}
57+
}
58+
59+
/// How to download your schema. Supports the Apollo Registry and GraphQL Introspection methods.
60+
let downloadMethod: DownloadMethod
61+
/// The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
62+
let downloadTimeout: Double
63+
/// Any additional headers to include when retrieving your schema. Defaults to nil.
64+
let headers: [HTTPHeader]
65+
/// The URL of the folder in which the downloaded schema should be written.
66+
let outputURL: URL
67+
68+
/// Designated Initializer
69+
///
70+
/// - Parameters:
71+
/// - downloadMethod: How to download your schema.
72+
/// - downloadTimeout: The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
73+
/// - headers: [optional] Any additional headers to include when retrieving your schema. Defaults to nil
74+
/// - outputFolderURL: The URL of the folder in which the downloaded schema should be written
75+
/// - schemaFilename: The name, without an extension, for your schema file. Defaults to `"schema"
76+
public init(using downloadMethod: DownloadMethod,
77+
timeout downloadTimeout: Double = 30.0,
78+
headers: [HTTPHeader] = [],
79+
outputFolderURL: URL,
80+
schemaFilename: String = "schema") {
81+
self.downloadMethod = downloadMethod
82+
self.downloadTimeout = downloadTimeout
83+
self.headers = headers
84+
self.outputURL = outputFolderURL.appendingPathComponent("\(schemaFilename).graphqls")
85+
}
86+
}
87+
88+
extension ApolloSchemaDownloadConfiguration: CustomDebugStringConvertible {
89+
public var debugDescription: String {
90+
return """
91+
downloadMethod: \(self.downloadMethod)
92+
downloadTimeout: \(self.downloadTimeout)
93+
headers: \(self.headers)
94+
outputURL: \(self.outputURL)
95+
"""
96+
}
97+
}

0 commit comments

Comments
 (0)