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

Added option to generate a custom scalar with a prefix. #1216

Merged
Merged
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
28 changes: 22 additions & 6 deletions Sources/ApolloCodegenLib/ApolloCodegenOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public struct ApolloCodegenOptions {
}
}

/// Enum to select how to handle properties using a custom scalar from the schema.
public enum CustomScalarFormat: Equatable {
/// Uses a default type instead of a custom scalar.
case none
/// Use your own types for custom scalars.
case passthrough
/// Use your own types for custom scalars with a prefix.
case passthroughWithPrefix(String)
}

let codegenEngine: CodeGenerationEngine
let includes: String
let mergeInFieldsFromFragmentSpreads: Bool
Expand All @@ -61,7 +71,7 @@ public struct ApolloCodegenOptions {
let omitDeprecatedEnumCases: Bool
let operationIDsURL: URL?
let outputFormat: OutputFormat
let passthroughCustomScalars: Bool
let customScalarFormat: CustomScalarFormat
let suppressSwiftMultilineStringLiterals: Bool
let urlToSchemaFile: URL

Expand All @@ -79,7 +89,7 @@ public struct ApolloCodegenOptions {
/// - only: [optional] Parse all input files, but only output generated code for the file at this URL if non-nil. Defaults to nil.
/// - operationIDsURL: [optional] Path to an operation id JSON map file. If specified, also stores the operation ids (hashes) as properties on operation types. Defaults to nil.
/// - outputFormat: The `OutputFormat` enum option to use to output generated code.
/// - passthroughCustomScalars: Set true to use your own types for custom scalars. Defaults to false.
/// - customScalarFormat: How to handle properties using a custom scalar from the schema.
/// - suppressSwiftMultilineStringLiterals: Don't use multi-line string literals when generating code. Defaults to false.
/// - urlToSchemaFile: The URL to your schema file.
/// - downloadTimeout: The maximum time to wait before indicating that the download timed out, in seconds. Defaults to 30 seconds.
Expand All @@ -92,7 +102,7 @@ public struct ApolloCodegenOptions {
only: URL? = nil,
operationIDsURL: URL? = nil,
outputFormat: OutputFormat,
passthroughCustomScalars: Bool = false,
customScalarFormat: CustomScalarFormat = .none,
suppressSwiftMultilineStringLiterals: Bool = false,
urlToSchemaFile: URL,
downloadTimeout: Double = 30.0) {
Expand All @@ -105,7 +115,7 @@ public struct ApolloCodegenOptions {
self.only = only
self.operationIDsURL = operationIDsURL
self.outputFormat = outputFormat
self.passthroughCustomScalars = passthroughCustomScalars
self.customScalarFormat = customScalarFormat
self.suppressSwiftMultilineStringLiterals = suppressSwiftMultilineStringLiterals
self.urlToSchemaFile = urlToSchemaFile
self.downloadTimeout = downloadTimeout
Expand Down Expand Up @@ -169,8 +179,14 @@ public struct ApolloCodegenOptions {
arguments.append("--omitDeprecatedEnumCases")
}

if self.passthroughCustomScalars {
switch customScalarFormat {
case .none:
break
case .passthrough:
arguments.append("--passthroughCustomScalars")
case .passthroughWithPrefix(let prefix):
arguments.append("--passthroughCustomScalars")
arguments.append("--customScalarsPrefix='\(prefix)'")
}

if self.mergeInFieldsFromFragmentSpreads {
Expand All @@ -187,7 +203,7 @@ public struct ApolloCodegenOptions {
case .multipleFiles(let folderURL):
arguments.append("'\(folderURL.path)'")
}

return arguments
}
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ApolloCodegenTests: XCTestCase {
XCTFail("Nope, this should be a single file!")
}
XCTAssertFalse(options.omitDeprecatedEnumCases)
XCTAssertFalse(options.passthroughCustomScalars)
XCTAssertEqual(options.customScalarFormat, .none)
XCTAssertEqual(options.urlToSchemaFile, schema)
XCTAssertEqual(options.modifier, .public)

Expand All @@ -64,6 +64,7 @@ class ApolloCodegenTests: XCTestCase {
let only = sourceRoot.appendingPathComponent("only.graphql")
let operationIDsURL = sourceRoot.appendingPathComponent("operationIDs.json")
let namespace = "ANameSpace"
let prefix = "MyPrefix"

let options = ApolloCodegenOptions(codegenEngine: .swiftExperimental,
includes: "*.graphql",
Expand All @@ -74,7 +75,7 @@ class ApolloCodegenTests: XCTestCase {
only: only,
operationIDsURL: operationIDsURL,
outputFormat: .multipleFiles(inFolderAtURL: output),
passthroughCustomScalars: true,
customScalarFormat: .passthroughWithPrefix(prefix),
urlToSchemaFile: schema)
XCTAssertEqual(options.includes, "*.graphql")
XCTAssertFalse(options.mergeInFieldsFromFragmentSpreads)
Expand All @@ -87,7 +88,7 @@ class ApolloCodegenTests: XCTestCase {
case .multipleFiles(let folderURL):
XCTAssertEqual(folderURL, output)
}
XCTAssertTrue(options.passthroughCustomScalars)
XCTAssertEqual(options.customScalarFormat, .passthroughWithPrefix(prefix))
XCTAssertEqual(options.urlToSchemaFile, schema)
XCTAssertTrue(options.omitDeprecatedEnumCases)
XCTAssertEqual(options.modifier, .internal)
Expand All @@ -104,6 +105,7 @@ class ApolloCodegenTests: XCTestCase {
"--operationIdsPath='\(operationIDsURL.path)'",
"--omitDeprecatedEnumCases",
"--passthroughCustomScalars",
"--customScalarsPrefix='\(prefix)'",
"'\(output.path)'",
])
}
Expand Down