Skip to content

Commit

Permalink
Remove dependency on apollo CLI for fetching schema (#1935)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
calvincestari and designatednerd authored Sep 16, 2021
1 parent 384b7df commit 22e2097
Show file tree
Hide file tree
Showing 24 changed files with 922 additions and 395 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ scripts/apollo.tar.gz
SwiftScripts/ApolloCLI
Tests/ApolloCodegenTests/scripts
Tests/ApolloCodegenTests/scripts directory
Tests/ApolloCodegenTests/Schema
Tests/ApolloCodegenTests/Output
SwiftScripts/.build-**

# Local Netlify folder
.netlify
.netlify
40 changes: 36 additions & 4 deletions Apollo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Sources/ApolloCodegenLib/ApolloCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public struct ApolloCLI {
let lock = try waitForCLIFolderLock(cliFolderURL: cliFolderURL, timeout: timeout)
defer { lock.unlock() }

try CLIDownloader.downloadIfNeeded(cliFolderURL: cliFolderURL, timeout: timeout)
try CLIDownloader.downloadIfNeeded(to: cliFolderURL, timeout: timeout)

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

let binaryFolderURL = try CLIExtractor.extractCLIIfNeeded(from: cliFolderURL)
Expand Down
97 changes: 97 additions & 0 deletions Sources/ApolloCodegenLib/ApolloSchemaDownloadConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Foundation

/// A configuration object that defines behavior for schema download.
public struct ApolloSchemaDownloadConfiguration {

/// How to attempt to download your schema
public enum DownloadMethod: Equatable {

/// The Apollo Schema Registry, which serves as a central hub for managing your data graph.
case apolloRegistry(_ settings: ApolloRegistrySettings)
/// GraphQL Introspection connecting to the specified URL.
case introspection(endpointURL: URL)

public struct ApolloRegistrySettings: Equatable {
/// The API key to use when retrieving your schema from the Apollo Registry.
public let apiKey: String
/// The identifier of the graph to fetch. Can be found in Apollo Studio.
public let graphID: String
/// The variant of the graph in the registry.
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.
/// - variant: The variant of the graph to fetch. Defaults to "current", which will return whatever is set to the current variant.
public init(apiKey: String,
graphID: String,
variant: String = "current") {
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 (.apolloRegistry(let lhsSettings), .apolloRegistry(let rhsSettings)):
return lhsSettings == rhsSettings
default:
return false
}
}

}

public struct HTTPHeader: Equatable, CustomDebugStringConvertible {
let key: String
let value: String

public var debugDescription: String {
"\(key): \(value)"
}
}

/// How to download your schema. Supports the Apollo Registry and GraphQL Introspection methods.
let downloadMethod: DownloadMethod
/// The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
let downloadTimeout: Double
/// Any additional headers to include when retrieving your schema. Defaults to nil.
let headers: [HTTPHeader]
/// The URL of the folder in which the downloaded schema should be written.
let outputURL: URL

/// Designated Initializer
///
/// - Parameters:
/// - downloadMethod: How to download your schema.
/// - downloadTimeout: The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
/// - 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
/// - schemaFilename: The name, without an extension, for your schema file. Defaults to `"schema"
public init(using downloadMethod: DownloadMethod,
timeout downloadTimeout: Double = 30.0,
headers: [HTTPHeader] = [],
outputFolderURL: URL,
schemaFilename: String = "schema") {
self.downloadMethod = downloadMethod
self.downloadTimeout = downloadTimeout
self.headers = headers
self.outputURL = outputFolderURL.appendingPathComponent("\(schemaFilename).graphqls")
}
}

extension ApolloSchemaDownloadConfiguration: CustomDebugStringConvertible {
public var debugDescription: String {
return """
downloadMethod: \(self.downloadMethod)
downloadTimeout: \(self.downloadTimeout)
headers: \(self.headers)
outputURL: \(self.outputURL)
"""
}
}
Loading

0 comments on commit 22e2097

Please sign in to comment.