-
Notifications
You must be signed in to change notification settings - Fork 730
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 handling for downloading schema from registry to Swift Scripting #1691
Changes from all commits
4a6b033
85c2cfe
61e89d3
3ae6da2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,48 @@ public struct ApolloSchemaOptions { | |
case schemaDefinitionLanguage = "graphql" | ||
} | ||
|
||
let apiKey: String? | ||
let endpointURL: URL | ||
/// How to attempt to download your schema | ||
public enum DownloadMethod: Equatable { | ||
|
||
case registry(_ settings: RegistrySettings) | ||
/// - endpointURL: The endpoint to hit to download your schema. | ||
case introspection(endpointURL: URL) | ||
|
||
public struct RegistrySettings: Equatable { | ||
public let apiKey: String | ||
public let graphID: String | ||
public let variant: String? | ||
|
||
/// Designated initializer | ||
/// | ||
/// - Parameters: | ||
/// - apiKey: The API key to use when retrieving your schema. | ||
/// - graphID: The identifier of the graph to fetch. Can be found in Apollo Studio. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's nothing explicitly named "graph id" in Apollo Studio that I can see, but I'm guessing it's the dynamic part of the URL (after "/graph/"). If so, this is also the part between "service:" and the key itself in the token. I've changed the name of our graph in Apollo Studio, but the name in the URL and token retains the old name so I'm guessing the "graph id" is the full thing and not just the id-looking part after the old name. I'll give it a shot and see what works! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thoughts above were accurate, and it works 👍 An internet-savvy person can probably figure it out, but it's not entirely obvious what to put as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that's being addressed on the Studio end shortly. |
||
/// - variant: [Optional] The variant of the graph to fetch. Defaults to nil, which will return whatever is set to the current variant. | ||
public init(apiKey: String, | ||
graphID: String, | ||
variant: String? = nil) { | ||
self.apiKey = apiKey | ||
self.graphID = graphID | ||
self.variant = variant | ||
} | ||
} | ||
|
||
public static func == (lhs: DownloadMethod, rhs: DownloadMethod) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.introspection(let lhsURL), introspection(let rhsURL)): | ||
return lhsURL == rhsURL | ||
case (.registry(let lhsSettings), | ||
.registry(let rhsSettings)): | ||
return lhsSettings == rhsSettings | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
} | ||
|
||
let downloadMethod: DownloadMethod | ||
let headers: [String] | ||
let outputURL: URL | ||
|
||
|
@@ -21,21 +61,18 @@ public struct ApolloSchemaOptions { | |
/// - Parameters: | ||
/// - schemaFileName: The name, without an extension, for your schema file. Defaults to `"schema"` | ||
/// - 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. | ||
/// - downloadMethod: How to download your schema. | ||
/// - 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, | ||
downloadMethod: DownloadMethod, | ||
headers: [String] = [], | ||
outputFolderURL: URL, | ||
downloadTimeout: Double = 30.0) { | ||
self.apiKey = apiKey | ||
self.downloadMethod = downloadMethod | ||
self.headers = headers | ||
self.endpointURL = endpointURL | ||
self.outputURL = outputFolderURL.appendingPathComponent("\(schemaFileName).\(schemaFileType.rawValue)") | ||
|
||
self.downloadTimeout = downloadTimeout | ||
|
@@ -44,11 +81,17 @@ public struct ApolloSchemaOptions { | |
var arguments: [String] { | ||
var arguments = [ | ||
"client:download-schema", | ||
"--endpoint=\(self.endpointURL.absoluteString)" | ||
] | ||
|
||
if let key = self.apiKey { | ||
arguments.append("--key=\(key)") | ||
switch self.downloadMethod { | ||
case .introspection(let endpointURL): | ||
arguments.append("--endpoint=\(endpointURL.absoluteString)") | ||
case .registry(let settings): | ||
arguments.append("--key=\(settings.apiKey)") | ||
arguments.append("--graph=\(settings.graphID)") | ||
if let providedVariant = settings.variant { | ||
arguments.append("--variant=\(providedVariant)") | ||
} | ||
} | ||
|
||
arguments.append("'\(outputURL.path)'") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,20 @@ let output = sourceRootURL | |
.appendingPathComponent("Sources") | ||
.appendingPathComponent("UploadAPI") | ||
|
||
// Introspection download: | ||
let options = ApolloSchemaOptions(schemaFileName: "schema", | ||
endpointURL: endpoint, | ||
downloadMethod: .introspection(endpointURL: endpoint), | ||
outputFolderURL: output) | ||
|
||
// Registry download: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is left commented out for end to end testing in the future - not a great way to end to end test this without setting up a whole way of getting secrets into the repo. |
||
//let registrySettings = ApolloSchemaOptions.DownloadMethod.RegistrySettings(apiKey: <#Replace Me For Testing#>, | ||
// graphID: "Apollo-Fullstack-8zo5jl") | ||
// | ||
//let options = ApolloSchemaOptions(schemaFileName: "schema", | ||
// schemaFileType: .schemaDefinitionLanguage, | ||
// downloadMethod: .registry(registrySettings), | ||
// outputFolderURL: output) | ||
|
||
do { | ||
try ApolloSchemaDownloader.run(with: cliFolderURL, | ||
options: options) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a separate object so that it can have new parameters added without breaking the enum.