Skip to content

Commit a24c001

Browse files
committed
Skip redundant modifiers
1 parent 0c2b07f commit a24c001

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,16 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
168168
throw new GraphQLError(`Unsupported operation type "${operationType}"`);
169169
}
170170

171+
const {
172+
options: { namespace },
173+
fragments
174+
} = this.context;
175+
const modifiers = !!namespace ? ["final"] : ["public", "final"];
176+
171177
this.classDeclaration(
172178
{
173179
className,
174-
modifiers: ["public", "final"],
180+
modifiers: modifiers,
175181
adoptedProtocols: [protocol]
176182
},
177183
() => {
@@ -184,13 +190,13 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
184190

185191
const fragmentsReferenced = collectFragmentsReferenced(
186192
operation.selectionSet,
187-
this.context.fragments
193+
fragments
188194
);
189195

190196
if (this.context.options.generateOperationIds) {
191197
const { operationId } = generateOperationId(
192198
operation,
193-
this.context.fragments,
199+
fragments,
194200
fragmentsReferenced
195201
);
196202
operation.operationId = operationId;
@@ -343,7 +349,11 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
343349
before?: Function,
344350
after?: Function
345351
) {
346-
this.structDeclaration({ structName, adoptedProtocols }, () => {
352+
const {
353+
options: { namespace, mergeInFieldsFromFragmentSpreads }
354+
} = this.context;
355+
356+
this.structDeclaration({ structName, adoptedProtocols, namespace }, () => {
347357
if (before) {
348358
before();
349359
}
@@ -383,7 +393,7 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
383393

384394
const fields = collectAndMergeFields(
385395
variant,
386-
!!this.context.options.mergeInFieldsFromFragmentSpreads
396+
!!mergeInFieldsFromFragmentSpreads
387397
).map(field => this.helpers.propertyFromField(field as Field));
388398

389399
const fragmentSpreads = variant.fragmentSpreads.map(fragmentSpread => {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Struct {
1313
structName: string;
1414
adoptedProtocols?: string[];
1515
description?: string;
16+
namespace?: string;
1617
}
1718

1819
export interface Protocol {
@@ -136,13 +137,23 @@ export class SwiftGenerator<Context> extends CodeGenerator<
136137
}
137138

138139
structDeclaration(
139-
{ structName, description, adoptedProtocols = [] }: Struct,
140+
{
141+
structName,
142+
description,
143+
adoptedProtocols = [],
144+
namespace = undefined
145+
}: Struct,
140146
closure: Function
141147
) {
142148
this.printNewlineIfNeeded();
143149
this.comment(description);
150+
151+
const isRedundant =
152+
adoptedProtocols.includes("GraphQLFragment") && !!namespace;
153+
const modifier = isRedundant ? "" : "public ";
154+
144155
this.printOnNewline(
145-
`public struct ${escapeIdentifierIfNeeded(structName)}`
156+
`${modifier}struct ${escapeIdentifierIfNeeded(structName)}`
146157
);
147158
this.print(wrap(": ", join(adoptedProtocols, ", ")));
148159
this.pushScope({ typeName: structName });

0 commit comments

Comments
 (0)