Skip to content

Commit 19a6607

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios-codegen/' changes from 92f94b4d..38ea6149
38ea6149 feat: Specify caching fields with typePolicy directive (#554) git-subtree-dir: apollo-ios-codegen git-subtree-split: 38ea61496eb197a65eb9360b82b31251ed627725
1 parent dcf0c85 commit 19a6607

File tree

9 files changed

+679
-19
lines changed

9 files changed

+679
-19
lines changed

Sources/ApolloCodegenLib/Templates/InterfaceTemplate.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ struct InterfaceTemplate: TemplateRenderer {
1818
"""
1919
\(documentation: graphqlInterface.documentation, config: config)
2020
\(graphqlInterface.name.typeNameDocumentation)
21-
static let \(graphqlInterface.render(as: .typename)) = \(config.ApolloAPITargetName).Interface(name: "\(graphqlInterface.name.schemaName)")
21+
static let \(graphqlInterface.render(as: .typename)) = \(config.ApolloAPITargetName).Interface(name: "\(graphqlInterface.name.schemaName)", keyFields: \(KeyFieldsTemplate()))
22+
"""
23+
}
24+
25+
private func KeyFieldsTemplate() -> TemplateString {
26+
guard let fields = graphqlInterface.keyFields, !fields.isEmpty else { return "nil" }
27+
28+
return """
29+
[\(list: fields.map { "\"\($0)\"" })]
2230
"""
2331
}
2432
}

Sources/ApolloCodegenLib/Templates/ObjectTemplate.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ struct ObjectTemplate: TemplateRenderer {
2020
\(graphqlObject.name.typeNameDocumentation)
2121
static let \(graphqlObject.render(as: .typename)) = \(config.ApolloAPITargetName).Object(
2222
typename: "\(graphqlObject.name.schemaName)\",
23-
implementedInterfaces: \(ImplementedInterfacesTemplate())
23+
implementedInterfaces: \(ImplementedInterfacesTemplate()),
24+
keyFields: \(KeyFieldsTemplate())
2425
)
2526
"""
2627
}
28+
29+
private func KeyFieldsTemplate() -> TemplateString {
30+
guard let fields = graphqlObject.keyFields, !fields.isEmpty else { return "nil" }
31+
32+
return """
33+
[\(list: fields.map { "\"\($0)\"" })]
34+
"""
35+
}
2736

2837
private func ImplementedInterfacesTemplate() -> TemplateString {
2938
return """

Sources/GraphQLCompiler/ApolloCodegenFrontendBundle.swift

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Sources/GraphQLCompiler/GraphQLSchema.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,20 @@ public final class GraphQLObjectType: GraphQLCompositeType, GraphQLInterfaceImpl
239239
public private(set) var fields: [String: GraphQLField]!
240240

241241
public private(set) var interfaces: [GraphQLInterfaceType]!
242+
243+
public private(set) var keyFields: [String]!
242244

243245
/// Initializer to be used for creating mock objects in tests only.
244246
init(
245247
name: GraphQLName,
246248
documentation: String?,
247249
fields: [String: GraphQLField],
248-
interfaces: [GraphQLInterfaceType]
250+
interfaces: [GraphQLInterfaceType],
251+
keyFields: [String]
249252
) {
250253
self.fields = fields
251254
self.interfaces = interfaces
255+
self.keyFields = keyFields
252256
super.init(name: name, documentation: documentation)
253257
}
254258

@@ -259,6 +263,7 @@ public final class GraphQLObjectType: GraphQLCompositeType, GraphQLInterfaceImpl
259263
override func finalize(_ jsValue: JSValue, bridge: isolated JavaScriptBridge) {
260264
self.fields = try! bridge.invokeMethod("getFields", on: jsValue)
261265
self.interfaces = try! bridge.invokeMethod("getInterfaces", on: jsValue)
266+
self.keyFields = jsValue["_apolloKeyFields"]
262267
}
263268

264269
public override var debugDescription: String {
@@ -274,16 +279,20 @@ public final class GraphQLInterfaceType: GraphQLAbstractType, GraphQLInterfaceIm
274279
public private(set) var fields: [String: GraphQLField]!
275280

276281
public private(set) var interfaces: [GraphQLInterfaceType]!
282+
283+
public private(set) var keyFields: [String]!
277284

278285
/// Initializer to be used for creating mock objects in tests only.
279286
init(
280287
name: GraphQLName,
281288
documentation: String?,
282289
fields: [String: GraphQLField],
283-
interfaces: [GraphQLInterfaceType]
290+
interfaces: [GraphQLInterfaceType],
291+
keyFields: [String]
284292
) {
285293
self.fields = fields
286294
self.interfaces = interfaces
295+
self.keyFields = keyFields
287296
super.init(name: name, documentation: documentation)
288297
}
289298

@@ -294,6 +303,7 @@ public final class GraphQLInterfaceType: GraphQLAbstractType, GraphQLInterfaceIm
294303
override func finalize(_ jsValue: JSValue, bridge: isolated JavaScriptBridge) {
295304
self.fields = try! bridge.invokeMethod("getFields", on: jsValue)
296305
self.interfaces = try! bridge.invokeMethod("getInterfaces", on: jsValue)
306+
self.keyFields = jsValue["_apolloKeyFields"]
297307
}
298308

299309
public override var debugDescription: String {

0 commit comments

Comments
 (0)