Skip to content

Commit 7962955

Browse files
lilyballLily Ballard
authored andcommitted
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 c5074b5 commit 7962955

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
@@ -1185,6 +1185,139 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
11851185
}"
11861186
`;
11871187

1188+
exports[`Swift code generation #structDeclarationForSelectionSet() should escape init specially in a struct declaration initializer for a selection set 1`] = `
1189+
"public struct Human: GraphQLSelectionSet {
1190+
public static let possibleTypes = [\\"Human\\"]
1191+
1192+
public static let selections: [GraphQLSelection] = [
1193+
GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
1194+
]
1195+
1196+
public private(set) var resultMap: ResultMap
1197+
1198+
public init(unsafeResultMap: ResultMap) {
1199+
self.resultMap = unsafeResultMap
1200+
}
1201+
1202+
public init(\`self\` _self: [\`Self\`?]? = nil) {
1203+
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 } } }])
1204+
}
1205+
1206+
/// This human's friends, or an empty list if they have none
1207+
public var \`self\`: [\`Self\`?]? {
1208+
get {
1209+
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) } } }
1210+
}
1211+
set {
1212+
resultMap.updateValue(newValue.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }, forKey: \\"self\\")
1213+
}
1214+
}
1215+
1216+
public struct \`Self\`: GraphQLSelectionSet {
1217+
public static let possibleTypes = [\\"Human\\", \\"Droid\\"]
1218+
1219+
public static let selections: [GraphQLSelection] = [
1220+
GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
1221+
]
1222+
1223+
public private(set) var resultMap: ResultMap
1224+
1225+
public init(unsafeResultMap: ResultMap) {
1226+
self.resultMap = unsafeResultMap
1227+
}
1228+
1229+
public static func makeHuman(id: GraphQLID) -> \`Self\` {
1230+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"id\\": id])
1231+
}
1232+
1233+
public static func makeDroid(id: GraphQLID) -> \`Self\` {
1234+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Droid\\", \\"id\\": id])
1235+
}
1236+
1237+
/// The ID of the character
1238+
public var id: GraphQLID {
1239+
get {
1240+
return resultMap[\\"id\\"]! as! GraphQLID
1241+
}
1242+
set {
1243+
resultMap.updateValue(newValue, forKey: \\"id\\")
1244+
}
1245+
}
1246+
}
1247+
}
1248+
1249+
public struct Human: GraphQLSelectionSet {
1250+
public static let possibleTypes = [\\"Human\\"]
1251+
1252+
public static let selections: [GraphQLSelection] = [
1253+
GraphQLField(\\"friends\\", alias: \\"self\\", type: .list(.object(\`Self\`.selections))),
1254+
GraphQLField(\\"name\\", alias: \\"_self\\", type: .nonNull(.scalar(String.self))),
1255+
]
1256+
1257+
public private(set) var resultMap: ResultMap
1258+
1259+
public init(unsafeResultMap: ResultMap) {
1260+
self.resultMap = unsafeResultMap
1261+
}
1262+
1263+
public init(\`self\` _self_: [\`Self\`?]? = nil, _self: String) {
1264+
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])
1265+
}
1266+
1267+
/// This human's friends, or an empty list if they have none
1268+
public var \`self\`: [\`Self\`?]? {
1269+
get {
1270+
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) } } }
1271+
}
1272+
set {
1273+
resultMap.updateValue(newValue.flatMap { (value: [\`Self\`?]) -> [ResultMap?] in value.map { (value: \`Self\`?) -> ResultMap? in value.flatMap { (value: \`Self\`) -> ResultMap in value.resultMap } } }, forKey: \\"self\\")
1274+
}
1275+
}
1276+
1277+
/// What this human calls themselves
1278+
public var _self: String {
1279+
get {
1280+
return resultMap[\\"_self\\"]! as! String
1281+
}
1282+
set {
1283+
resultMap.updateValue(newValue, forKey: \\"_self\\")
1284+
}
1285+
}
1286+
1287+
public struct \`Self\`: GraphQLSelectionSet {
1288+
public static let possibleTypes = [\\"Human\\", \\"Droid\\"]
1289+
1290+
public static let selections: [GraphQLSelection] = [
1291+
GraphQLField(\\"id\\", type: .nonNull(.scalar(GraphQLID.self))),
1292+
]
1293+
1294+
public private(set) var resultMap: ResultMap
1295+
1296+
public init(unsafeResultMap: ResultMap) {
1297+
self.resultMap = unsafeResultMap
1298+
}
1299+
1300+
public static func makeHuman(id: GraphQLID) -> \`Self\` {
1301+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Human\\", \\"id\\": id])
1302+
}
1303+
1304+
public static func makeDroid(id: GraphQLID) -> \`Self\` {
1305+
return \`Self\`(unsafeResultMap: [\\"__typename\\": \\"Droid\\", \\"id\\": id])
1306+
}
1307+
1308+
/// The ID of the character
1309+
public var id: GraphQLID {
1310+
get {
1311+
return resultMap[\\"id\\"]! as! GraphQLID
1312+
}
1313+
set {
1314+
resultMap.updateValue(newValue, forKey: \\"id\\")
1315+
}
1316+
}
1317+
}
1318+
}"
1319+
`;
1320+
11881321
exports[`Swift code generation #structDeclarationForSelectionSet() should escape reserved keywords in a struct declaration for a selection set 1`] = `
11891322
"public struct Hero: GraphQLSelectionSet {
11901323
public static let possibleTypes = [\\"Human\\", \\"Droid\\"]
@@ -1200,12 +1333,12 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should escape
12001333
self.resultMap = unsafeResultMap
12011334
}
12021335

1203-
public static func makeHuman(\`private\`: String, \`self\`: [\`Self\`?]? = nil) -> Hero {
1204-
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 } } }])
1336+
public static func makeHuman(\`private\`: String, \`self\` _self: [\`Self\`?]? = nil) -> Hero {
1337+
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 } } }])
12051338
}
12061339

1207-
public static func makeDroid(\`private\`: String, \`self\`: [\`Self\`?]? = nil) -> Hero {
1208-
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 } } }])
1340+
public static func makeDroid(\`private\`: String, \`self\` _self: [\`Self\`?]? = nil) -> Hero {
1341+
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 } } }])
12091342
}
12101343

12111344
/// 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
@@ -505,6 +505,46 @@ describe("Swift code generation", () => {
505505
expect(generator.output).toMatchSnapshot();
506506
});
507507

508+
it(`should escape init specially in a struct declaration initializer for a selection set`, () => {
509+
const { operations } = compile(`
510+
query Humans {
511+
human(id: 0) {
512+
self: friends {
513+
id
514+
}
515+
}
516+
human(id: 1) {
517+
self: friends {
518+
id
519+
}
520+
_self: name
521+
}
522+
}
523+
`);
524+
525+
const human0 = (operations["Humans"].selectionSet.selections[0] as Field)
526+
.selectionSet as SelectionSet;
527+
const human1 = (operations["Humans"].selectionSet.selections[1] as Field)
528+
.selectionSet as SelectionSet;
529+
530+
generator.structDeclarationForSelectionSet(
531+
{
532+
structName: "Human",
533+
selectionSet: human0
534+
},
535+
false
536+
);
537+
generator.structDeclarationForSelectionSet(
538+
{
539+
structName: "Human",
540+
selectionSet: human1
541+
},
542+
false
543+
);
544+
545+
expect(generator.output).toMatchSnapshot();
546+
});
547+
508548
it(`should generate a nested struct declaration for a selection set with subselections`, () => {
509549
const { operations } = compile(`
510550
query Hero {

0 commit comments

Comments
 (0)