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 output-path argument to Generate Operation Manifest #3172

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class ApolloCodegen {
)
}
}

public static func generateOperationManifest(
with configuration: ApolloCodegenConfiguration,
withRootURL rootURL: URL? = nil,
Expand Down
10 changes: 5 additions & 5 deletions Sources/ApolloCodegenLib/ApolloCodegenConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// Configures the generation of an operation manifest JSON file for use with persisted queries
/// or [Automatic Persisted Queries (APQs)](https://www.apollographql.com/docs/apollo-server/performance/apq).
/// Defaults to `nil`.
public let operationManifest: OperationManifestFileOutput?
public var operationManifest: OperationManifestFileOutput?

/// Default property values
public struct Default {
Expand Down Expand Up @@ -480,11 +480,11 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// Defaults to `nil`.
public struct OperationManifestFileOutput: Codable, Equatable {
/// Local path where the generated operation manifest file should be written.
let path: String
public let path: String
/// The version format to use when generating the operation manifest. Defaults to `.persistedQueries`.
let version: Version
public let version: Version

public enum Version: String, Codable, Equatable {
public enum Version: String, Codable, Equatable, CaseIterable {
/// Generates an operation manifest for use with persisted queries.
case persistedQueries
/// Generates an operation manifest for pre-registering operations with the legacy
Expand Down Expand Up @@ -944,7 +944,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// The input files required for code generation.
public let input: FileInput
/// The paths and files output by code generation.
public let output: FileOutput
public var output: FileOutput
/// Rules and options to customize the generated code.
public let options: OutputOptions
/// Allows users to enable experimental features.
Expand Down
16 changes: 5 additions & 11 deletions Sources/CodegenCLI/Commands/FetchSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,16 @@ public struct FetchSchema: ParsableCommand {
) throws {
logger.SetLoggingLevel(verbose: inputs.verbose)

switch (inputs.string, inputs.path) {
case let (.some(string), _):
try fetchSchema(data: try string.asData(), schemaDownloadProvider: schemaDownloadProvider)

case let (nil, path):
let data = try fileManager.unwrappedContents(atPath: path)
try fetchSchema(data: data, schemaDownloadProvider: schemaDownloadProvider)
}
try fetchSchema(
configuration: inputs.getCodegenConfiguration(fileManager: fileManager),
schemaDownloadProvider: schemaDownloadProvider
)
}

private func fetchSchema(
data: Data,
configuration codegenConfiguration: ApolloCodegenConfiguration,
schemaDownloadProvider: SchemaDownloadProvider.Type
) throws {
let codegenConfiguration = try JSONDecoder().decode(ApolloCodegenConfiguration.self, from: data)

guard let schemaDownloadConfiguration = codegenConfiguration.schemaDownloadConfiguration else {
throw Error(errorDescription: """
Missing schema download configuration. Hint: check the `schemaDownloadConfiguration` \
Expand Down
25 changes: 6 additions & 19 deletions Sources/CodegenCLI/Commands/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,18 @@ public struct Generate: ParsableCommand {
with: inputs
)

switch (inputs.string, inputs.path) {
case let (.some(string), _):
try generate(
data: try string.asData(),
codegenProvider: codegenProvider,
schemaDownloadProvider: schemaDownloadProvider
)

case let (nil, path):
let data = try fileManager.unwrappedContents(atPath: path)
try generate(
data: data,
codegenProvider: codegenProvider,
schemaDownloadProvider: schemaDownloadProvider
)
}
try generate(
configuration: inputs.getCodegenConfiguration(fileManager: fileManager),
codegenProvider: codegenProvider,
schemaDownloadProvider: schemaDownloadProvider
)
}

private func generate(
data: Data,
configuration: ApolloCodegenConfiguration,
codegenProvider: CodegenProvider.Type,
schemaDownloadProvider: SchemaDownloadProvider.Type
) throws {
let configuration = try JSONDecoder().decode(ApolloCodegenConfiguration.self, from: data)

if fetchSchema {
guard
let schemaDownloadConfiguration = configuration.schemaDownloadConfiguration
Expand Down
90 changes: 70 additions & 20 deletions Sources/CodegenCLI/Commands/GenerateOperationManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,34 @@ public struct GenerateOperationManifest: ParsableCommand {
public static var configuration = CommandConfiguration(
abstract: "Generate Persisted Queries operation manifest based on a code generation configuration."
)


struct OutputOptions: ParsableArguments {
@Option(
name: .shortAndLong,
help: """
Output the operation manifest to the given path. This overrides the value of the \
`output.operationManifest.path` in your configuration.

**If the `output.operationManifest` is not included in your configuration, this is required.**
"""
)
var outputPath: String?

@Option(
name: .long,
help: """
The version for the operation manifest format to generate. This overrides the value of the \
`output.operationManifest.path` in your configuration.

**If the `output.operationManifest` is not included in your configuration, this is required.**
"""
)
var manifestVersion: ApolloCodegenConfiguration.OperationManifestFileOutput.Version?
}

@OptionGroup var inputs: InputOptions

@OptionGroup var outputOptions: OutputOptions

// MARK: - Implementation

public init() { }
Expand All @@ -26,36 +51,61 @@ public struct GenerateOperationManifest: ParsableCommand {
logger: LogLevelSetter.Type = CodegenLogger.self
) throws {
logger.SetLoggingLevel(verbose: inputs.verbose)

try checkForCLIVersionMismatch(
with: inputs
)

switch (inputs.string, inputs.path) {
case let (.some(string), _):
try generateManifest(
data: try string.asData(),
codegenProvider: codegenProvider
)
case let (nil, path):
try generateManifest(
data: try fileManager.unwrappedContents(atPath: path),
codegenProvider: codegenProvider

var configuration = try inputs.getCodegenConfiguration(fileManager: fileManager)

try validate(configuration: configuration)

if let outputPath = outputOptions.outputPath,
let manifestVersion = outputOptions.manifestVersion {
configuration.output.operationManifest = .init(
path: outputPath,
version: manifestVersion
)
}

try generateManifest(
configuration: configuration,
codegenProvider: codegenProvider
)
}

private func generateManifest(
data: Data,
configuration: ApolloCodegenConfiguration,
codegenProvider: CodegenProvider.Type
) throws {
let configuration = try JSONDecoder().decode(ApolloCodegenConfiguration.self, from: data)

try codegenProvider.generateOperationManifest(
with: configuration,
withRootURL: rootOutputURL(for: inputs),
fileManager: .default
)
}

// MARK: - Validation

func validate(configuration: ApolloCodegenConfiguration) throws {
try checkForCLIVersionMismatch(with: inputs)

if configuration.output.operationManifest == nil {
guard outputOptions.outputPath != nil else {
throw ValidationError("""
`manifest-version` argument missing. When `output-path` is used, `manifest-version` \
must also be present.
""")
}
}

if outputOptions.outputPath != nil {
guard outputOptions.manifestVersion != nil else {
throw ValidationError("""
No output path for operation manifest found. You must either provide the `output-path` \
argument or your codegen configuration must have a value present for the \
`output.operationManifest` option.
""")
}
}
}

}

extension ApolloCodegenConfiguration.OperationManifestFileOutput.Version: ExpressibleByArgument {}
14 changes: 14 additions & 0 deletions Sources/CodegenCLI/OptionGroups/InputOptions.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import ArgumentParser
import ApolloCodegenLib

/// Shared group of common arguments used in commands for input parameters.
struct InputOptions: ParsableArguments {
Expand Down Expand Up @@ -28,4 +30,16 @@ struct InputOptions: ParsableArguments {
help: "Ignore Apollo version mismatch errors. Warning: This may lead to incompatible generated objects."
)
var ignoreVersionMismatch: Bool = false

func getCodegenConfiguration(fileManager: FileManager) throws -> ApolloCodegenConfiguration {
var data: Data
switch (string, path) {
case let (.some(string), _):
data = try string.asData()

case let (nil, path):
data = try fileManager.unwrappedContents(atPath: path)
}
return try JSONDecoder().decode(ApolloCodegenConfiguration.self, from: data)
}
}
Loading