Skip to content

Commit 5125f5d

Browse files
Merge pull request #1451 from apollographql/ellen/fix-regression
Swift: Fix regression for individual files.
2 parents 851ea49 + ffbaa86 commit 5125f5d

5 files changed

Lines changed: 356 additions & 190 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- <First `apollo-codegen-scala` related entry goes here>
1313
- `apollo-codegen-swift`
1414
- Fix issue where type names were not being properly escaped [iOS 193](https://github.com/apollographql/apollo-ios/issues/193)
15+
- Fix overcorrection on removing redundant modifiers [#1449](https://github.com/apollographql/apollo-tooling/issues/1449)
1516
- `apollo-codegen-typescript`
1617
- <First `apollo-codegen-typescript` related entry goes here>
1718
- `apollo-env`

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

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("Swift code generation", () => {
4949
}
5050
`);
5151

52-
generator.classDeclarationForOperation(operations["HeroName"]);
52+
generator.classDeclarationForOperation(operations["HeroName"], false);
5353

5454
expect(generator.output).toMatchSnapshot();
5555
});
@@ -67,7 +67,7 @@ describe("Swift code generation", () => {
6767
}
6868
`);
6969

70-
generator.classDeclarationForOperation(operations["Hero"]);
70+
generator.classDeclarationForOperation(operations["Hero"], false);
7171

7272
expect(generator.output).toMatchSnapshot();
7373
});
@@ -85,7 +85,7 @@ describe("Swift code generation", () => {
8585
}
8686
`);
8787

88-
generator.classDeclarationForOperation(operations["Hero"]);
88+
generator.classDeclarationForOperation(operations["Hero"], false);
8989

9090
expect(generator.output).toMatchSnapshot();
9191
});
@@ -127,7 +127,7 @@ describe("Swift code generation", () => {
127127
}
128128
`);
129129

130-
generator.classDeclarationForOperation(operations["CreateReview"]);
130+
generator.classDeclarationForOperation(operations["CreateReview"], false);
131131

132132
expect(generator.output).toMatchSnapshot();
133133
});
@@ -147,7 +147,7 @@ describe("Swift code generation", () => {
147147
}
148148
`);
149149

150-
generator.classDeclarationForOperation(operations["Hero"]);
150+
generator.classDeclarationForOperation(operations["Hero"], false);
151151

152152
expect(generator.output).toMatchSnapshot();
153153
});
@@ -182,7 +182,7 @@ describe("Swift code generation", () => {
182182
{ generateOperationIds: true, mergeInFieldsFromFragmentSpreads: true }
183183
);
184184

185-
generator.classDeclarationForOperation(operations["Hero"]);
185+
generator.classDeclarationForOperation(operations["Hero"], false);
186186

187187
expect(generator.output).toMatchSnapshot();
188188
});
@@ -360,7 +360,7 @@ describe("Swift code generation", () => {
360360
}
361361
`);
362362

363-
generator.structDeclarationForFragment(fragments["HeroDetails"]);
363+
generator.structDeclarationForFragment(fragments["HeroDetails"], false);
364364

365365
expect(generator.output).toMatchSnapshot();
366366
});
@@ -373,7 +373,7 @@ describe("Swift code generation", () => {
373373
}
374374
`);
375375

376-
generator.structDeclarationForFragment(fragments["DroidDetails"]);
376+
generator.structDeclarationForFragment(fragments["DroidDetails"], false);
377377

378378
expect(generator.output).toMatchSnapshot();
379379
});
@@ -388,7 +388,7 @@ describe("Swift code generation", () => {
388388
}
389389
`);
390390

391-
generator.structDeclarationForFragment(fragments["HeroDetails"]);
391+
generator.structDeclarationForFragment(fragments["HeroDetails"], false);
392392

393393
expect(generator.output).toMatchSnapshot();
394394
});
@@ -405,7 +405,7 @@ describe("Swift code generation", () => {
405405
}
406406
`);
407407

408-
generator.structDeclarationForFragment(fragments["HeroDetails"]);
408+
generator.structDeclarationForFragment(fragments["HeroDetails"], false);
409409

410410
expect(generator.output).toMatchSnapshot();
411411
});
@@ -424,10 +424,13 @@ describe("Swift code generation", () => {
424424
const selectionSet = (operations["Hero"].selectionSet
425425
.selections[0] as Field).selectionSet as SelectionSet;
426426

427-
generator.structDeclarationForSelectionSet({
428-
structName: "Hero",
429-
selectionSet
430-
});
427+
generator.structDeclarationForSelectionSet(
428+
{
429+
structName: "Hero",
430+
selectionSet
431+
},
432+
false
433+
);
431434

432435
expect(generator.output).toMatchSnapshot();
433436
});
@@ -444,10 +447,13 @@ describe("Swift code generation", () => {
444447
const selectionSet = (operations["Hero"].selectionSet
445448
.selections[0] as Field).selectionSet as SelectionSet;
446449

447-
generator.structDeclarationForSelectionSet({
448-
structName: "Hero",
449-
selectionSet
450-
});
450+
generator.structDeclarationForSelectionSet(
451+
{
452+
structName: "Hero",
453+
selectionSet
454+
},
455+
false
456+
);
451457

452458
expect(generator.output).toMatchSnapshot();
453459
});
@@ -466,10 +472,13 @@ describe("Swift code generation", () => {
466472
const selectionSet = (operations["Hero"].selectionSet
467473
.selections[0] as Field).selectionSet as SelectionSet;
468474

469-
generator.structDeclarationForSelectionSet({
470-
structName: "Hero",
471-
selectionSet
472-
});
475+
generator.structDeclarationForSelectionSet(
476+
{
477+
structName: "Hero",
478+
selectionSet
479+
},
480+
false
481+
);
473482

474483
expect(generator.output).toMatchSnapshot();
475484
});
@@ -491,10 +500,13 @@ describe("Swift code generation", () => {
491500
const selectionSet = (operations["Hero"].selectionSet
492501
.selections[0] as Field).selectionSet as SelectionSet;
493502

494-
generator.structDeclarationForSelectionSet({
495-
structName: "Hero",
496-
selectionSet
497-
});
503+
generator.structDeclarationForSelectionSet(
504+
{
505+
structName: "Hero",
506+
selectionSet
507+
},
508+
false
509+
);
498510

499511
expect(generator.output).toMatchSnapshot();
500512
});
@@ -516,10 +528,13 @@ describe("Swift code generation", () => {
516528
const selectionSet = (operations["Hero"].selectionSet
517529
.selections[0] as Field).selectionSet as SelectionSet;
518530

519-
generator.structDeclarationForSelectionSet({
520-
structName: "Hero",
521-
selectionSet
522-
});
531+
generator.structDeclarationForSelectionSet(
532+
{
533+
structName: "Hero",
534+
selectionSet
535+
},
536+
false
537+
);
523538

524539
expect(generator.output).toMatchSnapshot();
525540
});
@@ -539,10 +554,13 @@ describe("Swift code generation", () => {
539554
const selectionSet = (operations["Hero"].selectionSet
540555
.selections[0] as Field).selectionSet as SelectionSet;
541556

542-
generator.structDeclarationForSelectionSet({
543-
structName: "Hero",
544-
selectionSet
545-
});
557+
generator.structDeclarationForSelectionSet(
558+
{
559+
structName: "Hero",
560+
selectionSet
561+
},
562+
false
563+
);
546564

547565
expect(generator.output).toMatchSnapshot();
548566
});
@@ -566,10 +584,13 @@ describe("Swift code generation", () => {
566584
const selectionSet = (operations["Hero"].selectionSet
567585
.selections[0] as Field).selectionSet as SelectionSet;
568586

569-
generator.structDeclarationForSelectionSet({
570-
structName: "Hero",
571-
selectionSet
572-
});
587+
generator.structDeclarationForSelectionSet(
588+
{
589+
structName: "Hero",
590+
selectionSet
591+
},
592+
false
593+
);
573594

574595
expect(generator.output).toMatchSnapshot();
575596
});
@@ -586,18 +607,21 @@ describe("Swift code generation", () => {
586607
const selectionSet = (operations["Hero"].selectionSet
587608
.selections[0] as Field).selectionSet as SelectionSet;
588609

589-
generator.structDeclarationForSelectionSet({
590-
structName: "Hero",
591-
selectionSet
592-
});
610+
generator.structDeclarationForSelectionSet(
611+
{
612+
structName: "Hero",
613+
selectionSet
614+
},
615+
false
616+
);
593617

594618
expect(generator.output).toMatchSnapshot();
595619
});
596620
});
597621

598622
describe("#typeDeclarationForGraphQLType()", () => {
599623
it("should generate an enum declaration for a GraphQLEnumType", () => {
600-
generator.typeDeclarationForGraphQLType(schema.getType("Episode"));
624+
generator.typeDeclarationForGraphQLType(schema.getType("Episode"), false);
601625

602626
expect(generator.output).toMatchSnapshot();
603627
});
@@ -608,13 +632,16 @@ describe("Swift code generation", () => {
608632
values: { PUBLIC: { value: "PUBLIC" }, PRIVATE: { value: "PRIVATE" } }
609633
});
610634

611-
generator.typeDeclarationForGraphQLType(albumPrivaciesEnum);
635+
generator.typeDeclarationForGraphQLType(albumPrivaciesEnum, false);
612636

613637
expect(generator.output).toMatchSnapshot();
614638
});
615639

616640
it("should generate a struct declaration for a GraphQLInputObjectType", () => {
617-
generator.typeDeclarationForGraphQLType(schema.getType("ReviewInput"));
641+
generator.typeDeclarationForGraphQLType(
642+
schema.getType("ReviewInput"),
643+
false
644+
);
618645

619646
expect(generator.output).toMatchSnapshot();
620647
});

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ describe("Swift code generation: Basic language constructs", () => {
1717
propertyName: "name",
1818
typeName: "String"
1919
});
20-
generator.propertyDeclaration({ propertyName: "age", typeName: "Int" });
20+
generator.propertyDeclaration({
21+
propertyName: "age",
22+
typeName: "Int"
23+
});
2124
}
2225
);
2326

@@ -37,7 +40,10 @@ describe("Swift code generation: Basic language constructs", () => {
3740
propertyName: "name",
3841
typeName: "String"
3942
});
40-
generator.propertyDeclaration({ propertyName: "age", typeName: "Int" });
43+
generator.propertyDeclaration({
44+
propertyName: "age",
45+
typeName: "Int"
46+
});
4147
}
4248
);
4349

@@ -78,12 +84,15 @@ describe("Swift code generation: Basic language constructs", () => {
7884
});
7985

8086
it(`should generate a struct declaration`, () => {
81-
generator.structDeclaration({ structName: "Hero" }, () => {
87+
generator.structDeclaration({ structName: "Hero" }, false, () => {
8288
generator.propertyDeclaration({
8389
propertyName: "name",
8490
typeName: "String"
8591
});
86-
generator.propertyDeclaration({ propertyName: "age", typeName: "Int" });
92+
generator.propertyDeclaration({
93+
propertyName: "age",
94+
typeName: "Int"
95+
});
8796
});
8897

8998
expect(generator.output).toBe(stripIndent`
@@ -101,12 +110,44 @@ describe("Swift code generation: Basic language constructs", () => {
101110
adoptedProtocols: ["GraphQLFragment"],
102111
namespace: "StarWars"
103112
},
113+
false,
114+
() => {
115+
generator.propertyDeclaration({
116+
propertyName: "name",
117+
typeName: "String"
118+
});
119+
generator.propertyDeclaration({
120+
propertyName: "age",
121+
typeName: "Int"
122+
});
123+
}
124+
);
125+
126+
expect(generator.output).toBe(stripIndent`
127+
public struct Hero: GraphQLFragment {
128+
public var name: String
129+
public var age: Int
130+
}
131+
`);
132+
});
133+
134+
it(`should generate a namespaced fragment which is not public for individual files`, () => {
135+
generator.structDeclaration(
136+
{
137+
structName: "Hero",
138+
adoptedProtocols: ["GraphQLFragment"],
139+
namespace: "StarWars"
140+
},
141+
true,
104142
() => {
105143
generator.propertyDeclaration({
106144
propertyName: "name",
107145
typeName: "String"
108146
});
109-
generator.propertyDeclaration({ propertyName: "age", typeName: "Int" });
147+
generator.propertyDeclaration({
148+
propertyName: "age",
149+
typeName: "Int"
150+
});
110151
}
111152
);
112153

@@ -119,7 +160,7 @@ describe("Swift code generation: Basic language constructs", () => {
119160
});
120161

121162
it(`should generate an escaped struct declaration`, () => {
122-
generator.structDeclaration({ structName: "Type" }, () => {
163+
generator.structDeclaration({ structName: "Type" }, false, () => {
123164
generator.propertyDeclaration({
124165
propertyName: "name",
125166
typeName: "String"
@@ -144,7 +185,7 @@ describe("Swift code generation: Basic language constructs", () => {
144185
});
145186

146187
it(`should generate nested struct declarations`, () => {
147-
generator.structDeclaration({ structName: "Hero" }, () => {
188+
generator.structDeclaration({ structName: "Hero" }, false, () => {
148189
generator.propertyDeclaration({
149190
propertyName: "name",
150191
typeName: "String"
@@ -154,7 +195,7 @@ describe("Swift code generation: Basic language constructs", () => {
154195
typeName: "[Friend]"
155196
});
156197

157-
generator.structDeclaration({ structName: "Friend" }, () => {
198+
generator.structDeclaration({ structName: "Friend" }, false, () => {
158199
generator.propertyDeclaration({
159200
propertyName: "name",
160201
typeName: "String"
@@ -205,6 +246,7 @@ describe("Swift code generation: Basic language constructs", () => {
205246
it(`should handle multi-line descriptions`, () => {
206247
generator.structDeclaration(
207248
{ structName: "Hero", description: "A hero" },
249+
false,
208250
() => {
209251
generator.propertyDeclaration({
210252
propertyName: "name",

0 commit comments

Comments
 (0)