Skip to content

Commit 5f61beb

Browse files
gh-action-runnergh-action-runner
gh-action-runner
authored and
gh-action-runner
committed
Squashed 'apollo-ios-codegen/' changes from cfe27487..461d092e
461d092e feature: @OneOf input object support (#537) git-subtree-dir: apollo-ios-codegen git-subtree-split: 461d092e5835dcfadf43aaa37606305582d1ac59
1 parent 2d414b8 commit 5f61beb

9 files changed

+97
-34
lines changed

Sources/ApolloCodegenLib/CodegenConfiguration/ApolloCodegenConfiguration.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,15 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
386386
decoder: decoder
387387
)
388388

389-
url = try values.decode(String.self, forKey: .url)
389+
url = try values.decodeIfPresent(String.self, forKey: .url) ?? "https://github.com/apollographql/apollo-ios"
390390

391391
if let version = try? values.decodeIfPresent(SDKVersion.self, forKey: .sdkVersion) {
392392
sdkVersion = version
393393
} else if let versionString = try? values.decodeIfPresent(String.self, forKey: .sdkVersion) {
394394
let version = try SDKVersion(fromString: versionString)
395395
sdkVersion = version
396396
} else {
397-
throw DecodingError.typeMismatch(Self.self, DecodingError.Context.init(
398-
codingPath: values.codingPath,
399-
debugDescription: "No valid 'sdkVersion' provided.",
400-
underlyingError: nil
401-
))
397+
sdkVersion = .default
402398
}
403399
}
404400

Sources/ApolloCodegenLib/ConfigurationValidation.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ extension ApolloCodegen.ConfigurationContext {
3333
throw ApolloCodegen.Error.schemaNameConflict(name: self.schemaNamespace)
3434
}
3535

36-
if case .swiftPackage = self.output.testMocks,
37-
self.output.schemaTypes.moduleType != .swiftPackage() {
38-
throw ApolloCodegen.Error.testMocksInvalidSwiftPackageConfiguration
36+
if case .swiftPackage = self.output.testMocks {
37+
switch self.output.schemaTypes.moduleType {
38+
case .swiftPackage(_), .swiftPackageManager:
39+
break
40+
default:
41+
throw ApolloCodegen.Error.testMocksInvalidSwiftPackageConfiguration
42+
}
3943
}
4044

4145
if case .swiftPackage = self.output.schemaTypes.moduleType,

Sources/ApolloCodegenLib/FileGenerators/FileGenerator.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ enum FileTarget: Equatable {
112112
forConfig config: ApolloCodegen.ConfigurationContext
113113
) -> String {
114114
var moduleSubpath: String = "/"
115-
if config.output.schemaTypes.moduleType == .swiftPackage() {
115+
if case .swiftPackage = config.output.schemaTypes.moduleType {
116116
moduleSubpath += "Sources/"
117117
}
118118
if config.output.operations.isInModule {
@@ -132,7 +132,7 @@ enum FileTarget: Equatable {
132132
switch config.output.operations {
133133
case .inSchemaModule:
134134
var url = URL(fileURLWithPath: config.output.schemaTypes.path, relativeTo: config.rootURL)
135-
if config.output.schemaTypes.moduleType == .swiftPackage() {
135+
if case .swiftPackage = config.output.schemaTypes.moduleType {
136136
url = url.appendingPathComponent("Sources")
137137
}
138138

@@ -167,7 +167,7 @@ enum FileTarget: Equatable {
167167
switch config.output.operations {
168168
case .inSchemaModule:
169169
var url = URL(fileURLWithPath: config.output.schemaTypes.path, relativeTo: config.rootURL)
170-
if config.output.schemaTypes.moduleType == .swiftPackage() {
170+
if case .swiftPackage = config.output.schemaTypes.moduleType {
171171
url = url.appendingPathComponent("Sources")
172172
}
173173
if !operation.isLocalCacheMutation {

Sources/ApolloCodegenLib/FileGenerators/InputObjectFileGenerator.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ struct InputObjectFileGenerator: FileGenerator {
1010
let config: ApolloCodegen.ConfigurationContext
1111

1212
var template: any TemplateRenderer {
13-
InputObjectTemplate(graphqlInputObject: graphqlInputObject, config: config)
13+
if graphqlInputObject.isOneOf {
14+
OneOfInputObjectTemplate(graphqlInputObject: graphqlInputObject, config: config)
15+
} else {
16+
InputObjectTemplate(graphqlInputObject: graphqlInputObject, config: config)
17+
}
1418
}
1519
var target: FileTarget { .inputObject }
1620
var fileName: String { graphqlInputObject.render(as: .filename) }

Sources/ApolloCodegenLib/Templates/InputObjectTemplate.swift

+20-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct InputObjectTemplate: TemplateRenderer {
1515
func renderBodyTemplate(
1616
nonFatalErrorRecorder: ApolloCodegen.NonFatalError.Recorder
1717
) -> TemplateString {
18-
let (validFields, deprecatedFields) = filterFields(graphqlInputObject.fields)
18+
let (validFields, deprecatedFields) = graphqlInputObject.fields.filterFields()
1919
let memberAccessControl = accessControlModifier(for: .member)
2020

2121
return TemplateString(
@@ -63,23 +63,6 @@ struct InputObjectTemplate: TemplateRenderer {
6363
config.options.warningsOnDeprecatedUsage == .include
6464
}
6565

66-
private func filterFields(
67-
_ fields: GraphQLInputFieldDictionary
68-
) -> (valid: GraphQLInputFieldDictionary, deprecated: GraphQLInputFieldDictionary) {
69-
var valid: GraphQLInputFieldDictionary = [:]
70-
var deprecated: GraphQLInputFieldDictionary = [:]
71-
72-
for (key, value) in fields {
73-
if let _ = value.deprecationReason {
74-
deprecated[key] = value
75-
} else {
76-
valid[key] = value
77-
}
78-
}
79-
80-
return (valid: valid, deprecated: deprecated)
81-
}
82-
8366
private func deprecatedMessage(for fields: GraphQLInputFieldDictionary) -> String {
8467
guard !fields.isEmpty else { return "" }
8568

@@ -123,3 +106,22 @@ struct InputObjectTemplate: TemplateRenderer {
123106
"""
124107
}
125108
}
109+
110+
extension GraphQLInputFieldDictionary {
111+
112+
func filterFields() -> (valid: GraphQLInputFieldDictionary, deprecated: GraphQLInputFieldDictionary) {
113+
var valid: GraphQLInputFieldDictionary = [:]
114+
var deprecated: GraphQLInputFieldDictionary = [:]
115+
116+
for (key, value) in self {
117+
if let _ = value.deprecationReason {
118+
deprecated[key] = value
119+
} else {
120+
valid[key] = value
121+
}
122+
}
123+
124+
return (valid: valid, deprecated: deprecated)
125+
}
126+
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Foundation
2+
import GraphQLCompiler
3+
import TemplateString
4+
5+
struct OneOfInputObjectTemplate: TemplateRenderer {
6+
7+
let graphqlInputObject: GraphQLInputObjectType
8+
9+
let config: ApolloCodegen.ConfigurationContext
10+
11+
let target: TemplateTarget = .schemaFile(type: .inputObject)
12+
13+
func renderBodyTemplate(
14+
nonFatalErrorRecorder: ApolloCodegen.NonFatalError.Recorder
15+
) -> TemplateString {
16+
let memberAccessControl = accessControlModifier(for: .member)
17+
18+
return TemplateString(
19+
"""
20+
\(documentation: graphqlInputObject.documentation, config: config)
21+
\(graphqlInputObject.name.typeNameDocumentation)
22+
\(accessControlModifier(for: .parent))\
23+
enum \(graphqlInputObject.render(as: .typename)): OneOfInputObject {
24+
\(graphqlInputObject.fields.map({ "\(FieldCaseTemplate($1))" }), separator: "\n")
25+
26+
\(memberAccessControl)var __data: InputDict {
27+
switch self {
28+
\(graphqlInputObject.fields.map({ "\(FieldCaseDataTemplate($1))" }), separator: "\n")
29+
}
30+
}
31+
}
32+
"""
33+
)
34+
}
35+
36+
private func FieldCaseTemplate(_ field: GraphQLInputField) -> TemplateString {
37+
"""
38+
\(documentation: field.documentation, config: config)
39+
\(deprecationReason: field.deprecationReason, config: config)
40+
\(field.name.typeNameDocumentation)
41+
case \(field.render(config: config))(\(field.type.renderAsInputValue(inNullable: false, config: config.config)))
42+
"""
43+
}
44+
45+
private func FieldCaseDataTemplate(_ field: GraphQLInputField) -> TemplateString {
46+
"""
47+
case .\(field.render(config: config))(let value):
48+
return InputDict(["\(field.name.schemaName)": value])
49+
"""
50+
}
51+
52+
}

Sources/ApolloCodegenLib/Templates/RenderingHelpers/GraphQLType+Rendered.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension GraphQLType {
7171

7272
// MARK: Input Value
7373

74-
private func renderAsInputValue(
74+
func renderAsInputValue(
7575
inNullable: Bool,
7676
config: ApolloCodegenConfiguration
7777
) -> String {

Sources/ApolloCodegenLib/Templates/SwiftPackageManagerModuleTemplate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fileprivate extension ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleTyp
124124
"""
125125
case .local(let path):
126126
return """
127-
.package(path: "\(path)")
127+
.package(name: "apollo-ios", path: "\(path)")
128128
"""
129129
}
130130
}

Sources/GraphQLCompiler/GraphQLSchema.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ public typealias GraphQLInputFieldDictionary = OrderedDictionary<String, GraphQL
154154

155155
public final class GraphQLInputObjectType: GraphQLNamedType {
156156
public private(set) var fields: GraphQLInputFieldDictionary!
157+
158+
public let isOneOf: Bool
157159

158160
required init(_ jsValue: JSValue, bridge: isolated JavaScriptBridge) {
161+
self.isOneOf = jsValue["isOneOf"]
159162
super.init(jsValue, bridge: bridge)
160163
}
161164

@@ -167,9 +170,11 @@ public final class GraphQLInputObjectType: GraphQLNamedType {
167170
init(
168171
name: GraphQLName,
169172
documentation: String?,
170-
fields: GraphQLInputFieldDictionary
173+
fields: GraphQLInputFieldDictionary,
174+
isOneOf: Bool = false
171175
) {
172176
self.fields = fields
177+
self.isOneOf = isOneOf
173178
super.init(name: name, documentation: documentation)
174179
}
175180
}

0 commit comments

Comments
 (0)