Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- `apollo-codegen-scala`
- <First `apollo-codegen-scala` related entry goes here>
- `apollo-codegen-swift`
- <First `apollo-codegen-swift` related entry goes here>
- Add namespace when a fragment struct is referenced (Fixes #2396) [PR #2397](https://github.com/apollographql/apollo-tooling/pull/2397)
- `apollo-codegen-typescript`
- <First `apollo-codegen-typescript` related entry goes here>
- `apollo-codegen-core`
Expand Down
4 changes: 3 additions & 1 deletion packages/apollo-codegen-swift/src/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
outputIndividualFiles: boolean,
suppressMultilineStringLiterals: boolean
) {
const structName = this.helpers.structNameForFragmentName(fragmentName);
const structName = this.helpers.structDeclarationNameForFragmentName(
fragmentName
);

this.structDeclarationForSelectionSet(
{
Expand Down
18 changes: 18 additions & 0 deletions packages/apollo-codegen-swift/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,25 @@ export class Helpers {
return pascalCase(Inflector.singularize(propertyName));
}

/**
* Returns the Swift name for a struct representing a fragment. This must contain
* the application namespace in case the fragment name is the same as the name of any
* field in the current query, otherwise, Swift will assume the type reference is to
* the struct that we're inside the namespace of.
*/
structNameForFragmentName(fragmentName: string) {
const prefix =
this.options && this.options.namespace
? `${this.options.namespace}.`
: "";
return `${prefix}${pascalCase(fragmentName)}`;
}

/**
* Returns the Swift struct name that is being declared. When declaring a struct in Swift,
* no namespace can be included in the name.
*/
structDeclarationNameForFragmentName(fragmentName: string) {
return pascalCase(fragmentName);
}

Expand Down