Skip to content

Commit 843026b

Browse files
committed
swift codegen: Add operation name to generated operation
1 parent 1938030 commit 843026b

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
44
"public final class CreateReviewMutation: GraphQLMutation {
55
public let operationDefinition =
66
\\"mutation CreateReview($episode: Episode) {\\\\n createReview(episode: $episode, review: {stars: 5, commentary: \\\\\\"Wow!\\\\\\"}) {\\\\n stars\\\\n commentary\\\\n }\\\\n}\\"
7+
public let operationName: String? = \\"CreateReview\\"
78
89
public var episode: Episode?
910
@@ -87,6 +88,7 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
8788
"public final class HeroQuery: GraphQLQuery {
8889
public let operationDefinition =
8990
\\"query Hero {\\\\n hero {\\\\n ... on Droid {\\\\n ...HeroDetails\\\\n }\\\\n }\\\\n}\\"
91+
public let operationName: String? = \\"Hero\\"
9092
9193
public var queryDocument: String { return operationDefinition.appending(HeroDetails.fragmentDefinition) }
9294
@@ -217,6 +219,7 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
217219
"public final class HeroQuery: GraphQLQuery {
218220
public let operationDefinition =
219221
\\"query Hero {\\\\n hero {\\\\n ...DroidDetails\\\\n }\\\\n}\\"
222+
public let operationName: String? = \\"Hero\\"
220223
221224
public var queryDocument: String { return operationDefinition.appending(DroidDetails.fragmentDefinition) }
222225
@@ -375,6 +378,7 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
375378
"public final class HeroQuery: GraphQLQuery {
376379
public let operationDefinition =
377380
\\"query Hero {\\\\n hero {\\\\n ...HeroDetails\\\\n }\\\\n}\\"
381+
public let operationName: String? = \\"Hero\\"
378382
379383
public var queryDocument: String { return operationDefinition.appending(HeroDetails.fragmentDefinition) }
380384
@@ -472,6 +476,7 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
472476
"public final class HeroNameQuery: GraphQLQuery {
473477
public let operationDefinition =
474478
\\"query HeroName($episode: Episode) {\\\\n hero(episode: $episode) {\\\\n name\\\\n }\\\\n}\\"
479+
public let operationName: String? = \\"HeroName\\"
475480
476481
public var episode: Episode?
477482
@@ -548,6 +553,7 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
548553
"public final class HeroQuery: GraphQLQuery {
549554
public let operationDefinition =
550555
\\"query Hero {\\\\n hero {\\\\n ...HeroDetails\\\\n }\\\\n}\\"
556+
public let operationName: String? = \\"Hero\\"
551557
552558
public let operationIdentifier: String? = \\"90d0d674eb6a7b33776f63200d6cec3d09f881247c360a2ac9a29037a02210c4\\"
553559
@@ -643,6 +649,79 @@ exports[`Swift code generation #classDeclarationForOperation() should generate a
643649
}"
644650
`;
645651
652+
exports[`Swift code generation #classDeclarationForOperation() should generate a operationName matching operation name case 1`] = `
653+
"public final class CreateEpReviewMutation: GraphQLMutation {
654+
public let operationDefinition =
655+
\\"mutation createEPReview($episode: Episode) {\\\\n createReview(episode: $episode, review: {stars: 5, commentary: \\\\\\"Wow!\\\\\\"}) {\\\\n stars\\\\n }\\\\n}\\"
656+
public let operationName: String? = \\"createEPReview\\"
657+
658+
public var episode: Episode?
659+
660+
public init(episode: Episode? = nil) {
661+
self.episode = episode
662+
}
663+
664+
public var variables: GraphQLMap? {
665+
return [\\"episode\\": episode]
666+
}
667+
668+
public struct Data: GraphQLSelectionSet {
669+
public static let possibleTypes = [\\"Mutation\\"]
670+
671+
public static let selections: [GraphQLSelection] = [
672+
GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\"]], type: .object(CreateReview.selections)),
673+
]
674+
675+
public private(set) var resultMap: ResultMap
676+
677+
public init(unsafeResultMap: ResultMap) {
678+
self.resultMap = unsafeResultMap
679+
}
680+
681+
public init(createReview: CreateReview? = nil) {
682+
self.init(unsafeResultMap: [\\"__typename\\": \\"Mutation\\", \\"createReview\\": createReview.flatMap { (value: CreateReview) -> ResultMap in value.resultMap }])
683+
}
684+
685+
public var createReview: CreateReview? {
686+
get {
687+
return (resultMap[\\"createReview\\"] as? ResultMap).flatMap { CreateReview(unsafeResultMap: $0) }
688+
}
689+
set {
690+
resultMap.updateValue(newValue?.resultMap, forKey: \\"createReview\\")
691+
}
692+
}
693+
694+
public struct CreateReview: GraphQLSelectionSet {
695+
public static let possibleTypes = [\\"Review\\"]
696+
697+
public static let selections: [GraphQLSelection] = [
698+
GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
699+
]
700+
701+
public private(set) var resultMap: ResultMap
702+
703+
public init(unsafeResultMap: ResultMap) {
704+
self.resultMap = unsafeResultMap
705+
}
706+
707+
public init(stars: Int) {
708+
self.init(unsafeResultMap: [\\"__typename\\": \\"Review\\", \\"stars\\": stars])
709+
}
710+
711+
/// The number of stars this review gave, 1-5
712+
public var stars: Int {
713+
get {
714+
return resultMap[\\"stars\\"]! as! Int
715+
}
716+
set {
717+
resultMap.updateValue(newValue, forKey: \\"stars\\")
718+
}
719+
}
720+
}
721+
}
722+
}"
723+
`;
724+
646725
exports[`Swift code generation #initializerDeclarationForProperties() should generate initializer for a property 1`] = `
647726
"public init(episode: Episode) {
648727
self.episode = episode

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ describe("Swift code generation", () => {
125125
expect(generator.output).toMatchSnapshot();
126126
});
127127

128+
test(`should generate a operationName matching operation name case`, function() {
129+
const { operations } = compile(`
130+
mutation createEPReview($episode: Episode) {
131+
createReview(episode: $episode, review: { stars: 5, commentary: "Wow!" }) {
132+
stars
133+
}
134+
}
135+
`);
136+
137+
generator.classDeclarationForOperation(operations["createEPReview"]);
138+
139+
expect(generator.output).toMatchSnapshot();
140+
});
141+
128142
it(`should generate a class declaration with an operationIdentifier property when generateOperationIds is specified`, () => {
129143
const { operations } = compile(
130144
`

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
187187
this.context.fragments
188188
);
189189

190+
const { operationName } = operation;
191+
this.printOnNewline(
192+
`public let operationName: String? = "${operationName}"`
193+
);
194+
190195
if (this.context.options.generateOperationIds) {
191196
const { operationId } = generateOperationId(
192197
operation,

packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ import Apollo
154154
public final class SimpleQueryQuery: GraphQLQuery {
155155
public let operationDefinition =
156156
\\"query SimpleQuery {\\\\n hello\\\\n}\\"
157+
public let operationName: String? = \\"SimpleQuery\\"
157158
158159
public init() {
159160
}
@@ -195,6 +196,7 @@ import Apollo
195196
public final class SimpleQueryQuery: GraphQLQuery {
196197
public let operationDefinition =
197198
\\"query SimpleQuery {\\\\n hello\\\\n}\\"
199+
public let operationName: String? = \\"SimpleQuery\\"
198200
199201
public init() {
200202
}

0 commit comments

Comments
 (0)