Skip to content

Commit 4156bfb

Browse files
authored
fix(API): pass authMode used for lazy loading functionality (#3690)
* fix(API): pass authMode used for lazy loading functionality * revert GraphQLFilter change
1 parent d6d5a23 commit 4156bfb

17 files changed

+153
-57
lines changed

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncListDecoder.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99
import Amplify
10+
import AWSPluginsCore
1011

1112
/// This decoder is registered and used to detect various data payloads objects to store
1213
/// inside an AppSyncListProvider when decoding to the Lazy `List` type as a "not yet loaded" List. If the data payload
@@ -18,6 +19,7 @@ public struct AppSyncListDecoder: ModelListDecoder {
1819
let appSyncAssociatedIdentifiers: [String]
1920
let appSyncAssociatedFields: [String]
2021
let apiName: String?
22+
let authMode: AWSAuthorizationType?
2123
}
2224

2325
/// Used by the custom decoder implemented in the `List` type to detect if the payload can be

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncListPayload.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public struct AppSyncListPayload: Codable {
1616

1717
let graphQLData: JSONValue
1818
let apiName: String?
19+
let authMode: AWSAuthorizationType?
1920

2021
public init(graphQLData: JSONValue,
2122
apiName: String?,
23+
authMode: AWSAuthorizationType?,
2224
variables: [String: JSONValue]?) {
2325
self.apiName = apiName
26+
self.authMode = authMode
2427
self.variables = variables
2528
self.graphQLData = graphQLData
2629
}

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncListProvider.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
1414
/// The API friendly name used to reference the API to call
1515
let apiName: String?
1616

17+
/// The auth mode used for this API call
18+
let authMode: AWSAuthorizationType?
19+
1720
/// The limit for each page size
1821
var limit: Int? = 1_000
1922

@@ -42,7 +45,8 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
4245
convenience init(payload: AppSyncListPayload) throws {
4346
let listResponse = try AppSyncListResponse.initWithMetadata(type: Element.self,
4447
graphQLData: payload.graphQLData,
45-
apiName: payload.apiName)
48+
apiName: payload.apiName,
49+
authMode: payload.authMode)
4650

4751
self.init(elements: listResponse.items,
4852
nextToken: listResponse.nextToken,
@@ -59,27 +63,34 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
5963
convenience init(metadata: AppSyncListDecoder.Metadata) {
6064
self.init(associatedIdentifiers: metadata.appSyncAssociatedIdentifiers,
6165
associatedFields: metadata.appSyncAssociatedFields,
62-
apiName: metadata.apiName)
66+
apiName: metadata.apiName,
67+
authMode: metadata.authMode)
6368
}
6469

6570
// Internal initializer for testing
6671
init(elements: [Element],
6772
nextToken: String? = nil,
6873
apiName: String? = nil,
74+
authMode: AWSAuthorizationType? = nil,
6975
limit: Int? = nil,
7076
filter: [String: Any]? = nil) {
7177
self.loadedState = .loaded(elements: elements,
7278
nextToken: nextToken,
7379
filter: filter)
7480
self.apiName = apiName
81+
self.authMode = authMode
7582
self.limit = limit
7683
}
7784

7885
// Internal initializer for testing
79-
init(associatedIdentifiers: [String], associatedFields: [String], apiName: String? = nil) {
86+
init(associatedIdentifiers: [String],
87+
associatedFields: [String],
88+
apiName: String? = nil,
89+
authMode: AWSAuthorizationType? = nil) {
8090
self.loadedState = .notLoaded(associatedIdentifiers: associatedIdentifiers,
8191
associatedFields: associatedFields)
8292
self.apiName = apiName
93+
self.authMode = authMode
8394
}
8495

8596
// MARK: APIs
@@ -128,14 +139,16 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
128139
modelSchema: Element.schema,
129140
filter: filter,
130141
limit: limit,
131-
apiName: apiName)
142+
apiName: apiName,
143+
authMode: authMode)
132144
do {
133145
let graphQLResponse = try await Amplify.API.query(request: request)
134146
switch graphQLResponse {
135147
case .success(let graphQLData):
136148
guard let listResponse = try? AppSyncListResponse.initWithMetadata(type: Element.self,
137149
graphQLData: graphQLData,
138-
apiName: self.apiName) else {
150+
apiName: self.apiName,
151+
authMode: self.authMode) else {
139152
throw CoreError.listOperation("""
140153
The AppSync response return successfully, but could not decode to
141154
AWSAppSyncListResponse from: \(graphQLData)
@@ -196,7 +209,8 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
196209
filter: filter,
197210
limit: limit,
198211
nextToken: nextToken,
199-
apiName: apiName)
212+
apiName: apiName,
213+
authMode: authMode)
200214
do {
201215
let graphQLResponse = try await Amplify.API.query(request: request)
202216
switch graphQLResponse {
@@ -225,7 +239,8 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
225239
let metadata = AppSyncListDecoder.Metadata.init(
226240
appSyncAssociatedIdentifiers: associatedIdentifiers,
227241
appSyncAssociatedFields: associatedFields,
228-
apiName: apiName)
242+
apiName: apiName,
243+
authMode: authMode)
229244
var container = encoder.singleValueContainer()
230245
try container.encode(metadata)
231246
case .loaded(let elements, _, _):

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncListResponse.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Amplify
99
import Foundation
10+
import AWSPluginsCore
1011

1112
/// Resembles the AppSync's GraphQL response for a list operation.
1213
struct AppSyncListResponse<Element: Model>: Codable {
@@ -25,10 +26,14 @@ extension AppSyncListResponse {
2526
/// model metadata on its array associations.
2627
static func initWithMetadata(type: Element.Type,
2728
graphQLData: JSONValue,
28-
apiName: String?) throws -> AppSyncListResponse<Element> {
29+
apiName: String?,
30+
authMode: AWSAuthorizationType?) throws -> AppSyncListResponse<Element> {
2931
var elements = [Element]()
3032
if case let .array(jsonArray) = graphQLData["items"] {
31-
let jsonArrayWithMetadata = AppSyncModelMetadataUtils.addMetadata(toModelArray: jsonArray, apiName: apiName)
33+
let jsonArrayWithMetadata = AppSyncModelMetadataUtils.addMetadata(
34+
toModelArray: jsonArray,
35+
apiName: apiName,
36+
authMode: authMode)
3237

3338
let encoder = JSONEncoder()
3439
encoder.dateEncodingStrategy = ModelDateFormatting.encodingStrategy

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncModelDecoder.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99
import Amplify
10+
import AWSPluginsCore
1011

1112
/// This decoder is registered and used to detect various data payloads to store
1213
/// inside an `AppSyncModelProvider` when decoding to the `LazyReference` as a "not yet loaded" Reference. If the data payload
@@ -17,11 +18,16 @@ public struct AppSyncModelDecoder: ModelProviderDecoder {
1718
struct Metadata: Codable {
1819
let identifiers: [LazyReferenceIdentifier]
1920
let apiName: String?
21+
let authMode: AWSAuthorizationType?
2022
let source: String
2123

22-
init(identifiers: [LazyReferenceIdentifier], apiName: String?, source: String = ModelProviderRegistry.DecoderSource.appSync) {
24+
init(identifiers: [LazyReferenceIdentifier],
25+
apiName: String?,
26+
authMode: AWSAuthorizationType?,
27+
source: String = ModelProviderRegistry.DecoderSource.appSync) {
2328
self.identifiers = identifiers
2429
self.apiName = apiName
30+
self.authMode = authMode
2531
self.source = source
2632
}
2733
}

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncModelMetadata.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Amplify
99
import Foundation
10+
import AWSPluginsCore
1011

1112
/// Holds the methods to traverse and maniupulate the response data object by injecting
1213
public struct AppSyncModelMetadataUtils {
@@ -25,15 +26,17 @@ public struct AppSyncModelMetadataUtils {
2526
}
2627

2728
static func addMetadata(toModelArray graphQLDataArray: [JSONValue],
28-
apiName: String?) -> [JSONValue] {
29+
apiName: String?,
30+
authMode: AWSAuthorizationType?) -> [JSONValue] {
2931
return graphQLDataArray.map { (graphQLData) -> JSONValue in
30-
addMetadata(toModel: graphQLData, apiName: apiName)
32+
addMetadata(toModel: graphQLData, apiName: apiName, authMode: authMode)
3133
}
3234
}
3335

3436
static func addMetadata(
3537
toModel graphQLData: JSONValue,
3638
apiName: String?,
39+
authMode: AWSAuthorizationType?,
3740
source: String = ModelProviderRegistry.DecoderSource.appSync) -> JSONValue {
3841

3942
guard case var .object(modelJSON) = graphQLData else {
@@ -89,6 +92,7 @@ public struct AppSyncModelMetadataUtils {
8992
if let modelIdentifierMetadata = createModelIdentifierMetadata(associatedModelType,
9093
modelObject: modelObject,
9194
apiName: apiName,
95+
authMode: authMode,
9296
source: source) {
9397
if let serializedMetadata = try? encoder.encode(modelIdentifierMetadata),
9498
let metadataJSON = try? decoder.decode(JSONValue.self, from: serializedMetadata) {
@@ -104,7 +108,9 @@ public struct AppSyncModelMetadataUtils {
104108
// add metadata to its fields, to create not loaded LazyReference objects
105109
// only if the model type allowsfor lazy loading functionality
106110
else if associatedModelType.rootPath != nil {
107-
let nestedModelWithMetadata = addMetadata(toModel: nestedModelJSON, apiName: apiName)
111+
let nestedModelWithMetadata = addMetadata(toModel: nestedModelJSON,
112+
apiName: apiName,
113+
authMode: authMode)
108114
modelJSON.updateValue(nestedModelWithMetadata, forKey: modelField.name)
109115
}
110116
// otherwise do nothing to the data.
@@ -120,7 +126,8 @@ public struct AppSyncModelMetadataUtils {
120126
if modelJSON[modelField.name] == nil {
121127
let appSyncModelMetadata = AppSyncListDecoder.Metadata(appSyncAssociatedIdentifiers: identifiers,
122128
appSyncAssociatedFields: modelField.associatedFieldNames,
123-
apiName: apiName)
129+
apiName: apiName,
130+
authMode: authMode)
124131
if let serializedMetadata = try? encoder.encode(appSyncModelMetadata),
125132
let metadataJSON = try? decoder.decode(JSONValue.self, from: serializedMetadata) {
126133
log.verbose("Adding [\(modelField.name): \(metadataJSON)]")
@@ -141,13 +148,16 @@ public struct AppSyncModelMetadataUtils {
141148
associatedModelType.rootPath != nil {
142149

143150
for (index, item) in graphQLDataArray.enumerated() {
144-
let modelJSON = AppSyncModelMetadataUtils.addMetadata(toModel: item, apiName: apiName)
151+
let modelJSON = AppSyncModelMetadataUtils.addMetadata(toModel: item,
152+
apiName: apiName,
153+
authMode: authMode)
145154
graphQLDataArray[index] = modelJSON
146155
}
147156

148157
graphQLDataObject["items"] = JSONValue.array(graphQLDataArray)
149158
let payload = AppSyncListPayload(graphQLData: JSONValue.object(graphQLDataObject),
150159
apiName: apiName,
160+
authMode: authMode,
151161
variables: nil)
152162

153163
if let serializedPayload = try? encoder.encode(payload),
@@ -171,6 +181,7 @@ public struct AppSyncModelMetadataUtils {
171181
static func createModelIdentifierMetadata(_ associatedModel: Model.Type,
172182
modelObject: [String: JSONValue],
173183
apiName: String?,
184+
authMode: AWSAuthorizationType?,
174185
source: String) -> AppSyncModelDecoder.Metadata? {
175186
let primarykeys = associatedModel.schema.primaryKey
176187
var identifiers = [LazyReferenceIdentifier]()
@@ -188,6 +199,7 @@ public struct AppSyncModelMetadataUtils {
188199
return AppSyncModelDecoder.Metadata(
189200
identifiers: identifiers,
190201
apiName: apiName,
202+
authMode: authMode,
191203
source: source)
192204
} else {
193205
return nil

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncModelProvider.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import AWSPluginsCore
1212
public class AppSyncModelProvider<ModelType: Model>: ModelProvider {
1313

1414
let apiName: String?
15+
let authMode: AWSAuthorizationType?
1516
let source: String
1617
var loadedState: ModelProviderState<ModelType>
1718

@@ -20,12 +21,14 @@ public class AppSyncModelProvider<ModelType: Model>: ModelProvider {
2021
self.loadedState = .notLoaded(identifiers: metadata.identifiers)
2122
self.apiName = metadata.apiName
2223
self.source = metadata.source
24+
self.authMode = metadata.authMode
2325
}
2426

2527
// Creates a "loaded" provider
2628
init(model: ModelType?) {
2729
self.loadedState = .loaded(model: model)
2830
self.apiName = nil
31+
self.authMode = nil
2932
self.source = ModelProviderRegistry.DecoderSource.appSync
3033
}
3134

@@ -41,7 +44,8 @@ public class AppSyncModelProvider<ModelType: Model>: ModelProvider {
4144
}
4245
let request = GraphQLRequest<ModelType?>.getRequest(ModelType.self,
4346
byIdentifiers: identifiers,
44-
apiName: apiName)
47+
apiName: apiName,
48+
authMode: authMode)
4549
log.verbose("Loading \(ModelType.modelName) with \(identifiers)")
4650
let graphQLResponse = try await Amplify.API.query(request: request)
4751
switch graphQLResponse {
@@ -67,6 +71,7 @@ public class AppSyncModelProvider<ModelType: Model>: ModelProvider {
6771
let metadata = AppSyncModelDecoder.Metadata(
6872
identifiers: identifiers ?? [],
6973
apiName: apiName,
74+
authMode: authMode,
7075
source: source)
7176
try metadata.encode(to: encoder)
7277

AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Decode/GraphQLResponseDecoder+DecodeData.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,26 @@ extension GraphQLResponseDecoder {
5252
modelJSON = AppSyncModelMetadataUtils.addMetadata(
5353
toModel: item,
5454
apiName: request.apiName,
55+
authMode: request.authMode as? AWSAuthorizationType,
5556
source: ModelProviderRegistry.DecoderSource.dataStore)
5657
} else {
5758
modelJSON = AppSyncModelMetadataUtils.addMetadata(
5859
toModel: item,
59-
apiName: request.apiName)
60+
apiName: request.apiName,
61+
authMode: request.authMode as? AWSAuthorizationType)
6062
}
6163
graphQLDataArray[index] = modelJSON
6264
}
6365
graphQLDataObject["items"] = JSONValue.array(graphQLDataArray)
6466
let payload = AppSyncListPayload(graphQLData: JSONValue.object(graphQLDataObject),
6567
apiName: request.apiName,
68+
authMode: request.authMode as? AWSAuthorizationType,
6669
variables: try getVariablesJSON())
6770
serializedJSON = try encoder.encode(payload)
6871
} else if AppSyncModelMetadataUtils.shouldAddMetadata(toModel: graphQLData) { // 4
6972
let modelJSON = AppSyncModelMetadataUtils.addMetadata(toModel: graphQLData,
70-
apiName: request.apiName)
73+
apiName: request.apiName,
74+
authMode: request.authMode as? AWSAuthorizationType)
7175
serializedJSON = try encoder.encode(modelJSON)
7276
} else { // 5
7377
serializedJSON = try encoder.encode(graphQLData)

0 commit comments

Comments
 (0)