Skip to content

Commit b0e1f20

Browse files
author
James Baxley
authored
Merge pull request #656 from unitedwardrobe/filter-unknown-typenames
filter objects in getter to only include known typenames
2 parents e7e8973 + 07a6800 commit b0e1f20

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,9 @@ exports[`Swift code generation #structDeclarationForFragment() should generate a
10681068
/// The friends of the character, or an empty list if they have none
10691069
public var friends: [Friend?]? {
10701070
get {
1071-
return (resultMap[\\"friends\\"] as? [ResultMap?]).flatMap { (value: [ResultMap?]) -> [Friend?] in value.map { (value: ResultMap?) -> Friend? in value.flatMap { (value: ResultMap) -> Friend in Friend(unsafeResultMap: value) } } }
1071+
return (resultMap[\\"friends\\"] as? [ResultMap?]).flatMap { (value: [ResultMap?]) -> [Friend?] in value.map { (value: ResultMap?) -> Friend? in value.flatMap { (value: ResultMap) -> Friend in Friend(unsafeResultMap: value) } } }.filter({
1072+
Friend.possibleTypes.contains($0.__typename)
1073+
})
10721074
}
10731075
set {
10741076
resultMap.updateValue(newValue.flatMap { (value: [Friend?]) -> [ResultMap?] in value.map { (value: Friend?) -> ResultMap? in value.flatMap { (value: Friend) -> ResultMap in value.resultMap } } }, forKey: \\"friends\\")
@@ -1220,7 +1222,9 @@ exports[`Swift code generation #structDeclarationForSelectionSet() should genera
12201222
/// The friends of the character, or an empty list if they have none
12211223
public var friends: [Friend?]? {
12221224
get {
1223-
return (resultMap[\\"friends\\"] as? [ResultMap?]).flatMap { (value: [ResultMap?]) -> [Friend?] in value.map { (value: ResultMap?) -> Friend? in value.flatMap { (value: ResultMap) -> Friend in Friend(unsafeResultMap: value) } } }
1225+
return (resultMap[\\"friends\\"] as? [ResultMap?]).flatMap { (value: [ResultMap?]) -> [Friend?] in value.map { (value: ResultMap?) -> Friend? in value.flatMap { (value: ResultMap) -> Friend in Friend(unsafeResultMap: value) } } }.filter({
1226+
Friend.possibleTypes.contains($0.__typename)
1227+
})
12241228
}
12251229
set {
12261230
resultMap.updateValue(newValue.flatMap { (value: [Friend?]) -> [ResultMap?] in value.map { (value: Friend?) -> ResultMap? in value.flatMap { (value: Friend) -> ResultMap in value.resultMap } } }, forKey: \\"friends\\")

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,15 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
727727
expression,
728728
"ResultMap",
729729
structName
730-
)}`
730+
)}.filter`
731+
);
732+
this.withinBlock(
733+
() =>
734+
this.printOnNewline(
735+
`${structName}.possibleTypes.contains($0.__typename)`
736+
),
737+
"({",
738+
"})"
731739
);
732740
});
733741
this.printOnNewline("set");

0 commit comments

Comments
 (0)