Skip to content

Commit 3ce759e

Browse files
committed
swift: add failing test for struct initializer with self field
If an operation has a field named `self`, the struct initializer declares a parameter named `self`, which conflicts with the implicit `self`.
1 parent 841e655 commit 3ce759e

2 files changed

Lines changed: 177 additions & 4 deletions

File tree

packages/apollo-codegen-swift/src/__tests__/__snapshots__/codeGeneration.ts.snap

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,139 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
11611161
}"
11621162
`;
11631163

1164+
exports[`Swift code generation #structDeclarationForSelectionSet() should escape init specially in a struct declaration initializer for a selection set 1`] = `
1165+
"public struct Human: GraphQLSelectionSet {
1166+
public static let possibleTypes = [\\"Human\\"]
1167+
1168+
public static let selections: [GraphQLSelection] = [
1169+
GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
1170+
]
1171+
1172+
public private(set) var resultMap: ResultMap
1173+
1174+
public init(unsafeResultMap: ResultMap) {
1175+
self.resultMap = unsafeResultMap
1176+
}
1177+
1178+
public init(\`self\` _self: [\`Self\`?]? = nil) {
1179+
self.init(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"self\\": _self.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }])
1180+
}
1181+
1182+
/// This human's friends, or an empty list if they have none
1183+
public var \`self\`: [\`Self\`?]? {
1184+
get {
1185+
return (resultMap[\\"self\\"] as? [ResultMap?]).flatMap { (value: [ResultMap?]) -> [\`Self\`?] in value.map { (value: ResultMap?) -> \`Self\`? in value.flatMap { (value: ResultMap) -> \`Self\` in \`Self\`(unsafeResultMap: value) } } }
1186+
}
1187+
set {
1188+
resultMap.updateValue(newValue.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }, forKey: \\"self\\")
1189+
}
1190+
}
1191+
1192+
public struct \`Self\`: GraphQLSelectionSet {
1193+
public static let possibleTypes = [\\"Human\\", \\"Droid\\"]
1194+
1195+
public static let selections: [GraphQLSelection] = [
1196+
GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
1197+
]
1198+
1199+
public private(set) var resultMap: ResultMap
1200+
1201+
public init(unsafeResultMap: ResultMap) {
1202+
self.resultMap = unsafeResultMap
1203+
}
1204+
1205+
public static func makeHuman(id: GraphQLID) -> \`Self\` {
1206+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"id\\": id])
1207+
}
1208+
1209+
public static func makeDroid(id: GraphQLID) -> \`Self\` {
1210+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Droid\\", \\"id\\": id])
1211+
}
1212+
1213+
/// The ID of the character
1214+
public var id: GraphQLID {
1215+
get {
1216+
return resultMap[\\"id\\"]! as! GraphQLID
1217+
}
1218+
set {
1219+
resultMap.updateValue(newValue, forKey: \\"id\\")
1220+
}
1221+
}
1222+
}
1223+
}
1224+
1225+
public struct Human: GraphQLSelectionSet {
1226+
public static let possibleTypes = [\\"Human\\"]
1227+
1228+
public static let selections: [GraphQLSelection] = [
1229+
GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
1230+
GraphQLField(\\"name\\", alias: \\"_self\\", type: .nonNull(.scalar(String.self))),
1231+
]
1232+
1233+
public private(set) var resultMap: ResultMap
1234+
1235+
public init(unsafeResultMap: ResultMap) {
1236+
self.resultMap = unsafeResultMap
1237+
}
1238+
1239+
public init(\`self\` _self_: [\`Self\`?]? = nil, _self: String) {
1240+
self.init(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"self\\": _self_.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }, \\"_self\\": _self])
1241+
}
1242+
1243+
/// This human's friends, or an empty list if they have none
1244+
public var \`self\`: [\`Self\`?]? {
1245+
get {
1246+
return (resultMap[\\"self\\"] as? [ResultMap?]).flatMap { (value: [ResultMap?]) -> [\`Self\`?] in value.map { (value: ResultMap?) -> \`Self\`? in value.flatMap { (value: ResultMap) -> \`Self\` in \`Self\`(unsafeResultMap: value) } } }
1247+
}
1248+
set {
1249+
resultMap.updateValue(newValue.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }, forKey: \\"self\\")
1250+
}
1251+
}
1252+
1253+
/// What this human calls themselves
1254+
public var _self: String {
1255+
get {
1256+
return resultMap[\\"_self\\"]! as! String
1257+
}
1258+
set {
1259+
resultMap.updateValue(newValue, forKey: \\"_self\\")
1260+
}
1261+
}
1262+
1263+
public struct \`Self\`: GraphQLSelectionSet {
1264+
public static let possibleTypes = [\\"Human\\", \\"Droid\\"]
1265+
1266+
public static let selections: [GraphQLSelection] = [
1267+
GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
1268+
]
1269+
1270+
public private(set) var resultMap: ResultMap
1271+
1272+
public init(unsafeResultMap: ResultMap) {
1273+
self.resultMap = unsafeResultMap
1274+
}
1275+
1276+
public static func makeHuman(id: GraphQLID) -> \`Self\` {
1277+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"id\\": id])
1278+
}
1279+
1280+
public static func makeDroid(id: GraphQLID) -> \`Self\` {
1281+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Droid\\", \\"id\\": id])
1282+
}
1283+
1284+
/// The ID of the character
1285+
public var id: GraphQLID {
1286+
get {
1287+
return resultMap[\\"id\\"]! as! GraphQLID
1288+
}
1289+
set {
1290+
resultMap.updateValue(newValue, forKey: \\"id\\")
1291+
}
1292+
}
1293+
}
1294+
}"
1295+
`;
1296+
11641297
exports[`Swift code generation #structDeclarationForSelectionSet() should escape reserved keywords in a struct declaration for a selection set 1`] = `
11651298
"public struct Hero: GraphQLSelectionSet {
11661299
public static let possibleTypes = [\\"Human\\", \\"Droid\\"]
@@ -1176,12 +1309,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
11761309
self.resultMap = unsafeResultMap
11771310
}
11781311

1179-
public static func makeHuman(\`private\`: String, \`self\`: [\`Self\`?]? = nil) -> Hero {
1180-
return Hero(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"private\\": \`private\`, \\"self\\": \`self\`.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }])
1312+
public static func makeHuman(\`private\`: String, \`self\` _self: [\`Self\`?]? = nil) -> Hero {
1313+
return Hero(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"private\\": \`private\`, \\"self\\": _self.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }])
11811314
}
11821315

1183-
public static func makeDroid(\`private\`: String, \`self\`: [\`Self\`?]? = nil) -> Hero {
1184-
return Hero(unsafeResultMap: [\\"__typename\\": \\"Droid\\", \\"private\\": \`private\`, \\"self\\": \`self\`.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }])
1316+
public static func makeDroid(\`private\`: String, \`self\` _self: [\`Self\`?]? = nil) -> Hero {
1317+
return Hero(unsafeResultMap: [\\"__typename\\": \\"Droid\\", \\"private\\": \`private\`, \\"self\\": _self.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }])
11851318
}
11861319

11871320
/// The name of the character

packages/apollo-codegen-swift/src/__tests__/codeGeneration.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,46 @@ describe("Swift code generation", () => {
485485
expect(generator.output).toMatchSnapshot();
486486
});
487487

488+
it(`should escape init specially in a struct declaration initializer for a selection set`, () => {
489+
const { operations } = compile(`
490+
query Humans {
491+
human(id: 0) {
492+
self: friends {
493+
id
494+
}
495+
}
496+
human(id: 1) {
497+
self: friends {
498+
id
499+
}
500+
_self: name
501+
}
502+
}
503+
`);
504+
505+
const human0 = (operations["Humans"].selectionSet.selections[0] as Field)
506+
.selectionSet as SelectionSet;
507+
const human1 = (operations["Humans"].selectionSet.selections[1] as Field)
508+
.selectionSet as SelectionSet;
509+
510+
generator.structDeclarationForSelectionSet(
511+
{
512+
structName: "Human",
513+
selectionSet: human0
514+
},
515+
false
516+
);
517+
generator.structDeclarationForSelectionSet(
518+
{
519+
structName: "Human",
520+
selectionSet: human1
521+
},
522+
false
523+
);
524+
525+
expect(generator.output).toMatchSnapshot();
526+
});
527+
488528
it(`should generate a nested struct declaration for a selection set with subselections`, () => {
489529
const { operations } = compile(`
490530
query Hero {

0 commit comments

Comments
 (0)