Skip to content

Commit 43e3a70

Browse files
authored
refactor(API): move the model-based functions to helpers (#470)
**Notes:** Following the decision of not having the model-based calls as part of the API Category contract, I moved them out and created semantic factories to build the `GraphQLRequest` from the models, so the functionality is preserved.
1 parent 0f6445c commit 43e3a70

File tree

13 files changed

+229
-336
lines changed

13 files changed

+229
-336
lines changed

Amplify.xcodeproj/project.pbxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
2125E265231C2D3100B3DEB5 /* awsconfiguration.json in Resources */ = {isa = PBXBuildFile; fileRef = 2125E2102318D73B00B3DEB5 /* awsconfiguration.json */; };
2727
2129BE012394627B006363A1 /* PostCommentModelRegistration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE002394627B006363A1 /* PostCommentModelRegistration.swift */; };
2828
2129BE0F23948005006363A1 /* SingleDirectiveGraphQLDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE0823948005006363A1 /* SingleDirectiveGraphQLDocument.swift */; };
29-
2129BE1723948065006363A1 /* GraphQLRequest+Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1523948065006363A1 /* GraphQLRequest+Model.swift */; };
3029
2129BE1E2394806B006363A1 /* QueryPredicate+GraphQL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1A2394806B006363A1 /* QueryPredicate+GraphQL.swift */; };
3130
2129BE1F2394806B006363A1 /* Model+GraphQL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1B2394806B006363A1 /* Model+GraphQL.swift */; };
3231
2129BE212394806B006363A1 /* ModelSchema+GraphQL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1D2394806B006363A1 /* ModelSchema+GraphQL.swift */; };
@@ -281,6 +280,7 @@
281280
B9521835237E21BA00F53237 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9521830237E21B900F53237 /* Comment.swift */; };
282281
B9521836237E21BA00F53237 /* Post+Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9521831237E21B900F53237 /* Post+Schema.swift */; };
283282
B9521837237E21BA00F53237 /* Post.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9521832237E21B900F53237 /* Post.swift */; };
283+
B9675A2E24752621002FC843 /* GraphQLRequest+Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1523948065006363A1 /* GraphQLRequest+Model.swift */; };
284284
B98E9D0E2372236300934B51 /* ModelKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98E9D062372236200934B51 /* ModelKey.swift */; };
285285
B98E9D0F2372236300934B51 /* QueryOperator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98E9D072372236200934B51 /* QueryOperator.swift */; };
286286
B98E9D102372236300934B51 /* QueryPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98E9D082372236200934B51 /* QueryPredicate.swift */; };
@@ -2322,10 +2322,10 @@
23222322
FA131AAB2360FE070008381C /* AWSPluginsCore */ = {
23232323
isa = PBXGroup;
23242324
children = (
2325-
21C395B4245729F100597EA2 /* API */,
2326-
FA131ACB2360FE470008381C /* Auth */,
23272325
FA131AAC2360FE070008381C /* AWSPluginsCore.h */,
23282326
FA131AAD2360FE070008381C /* Info.plist */,
2327+
21C395B4245729F100597EA2 /* API */,
2328+
FA131ACB2360FE470008381C /* Auth */,
23292329
2129BE0223947FA3006363A1 /* Model */,
23302330
6BBECD6F23ADA7C100C8DFBE /* ServiceConfiguration */,
23312331
2129BE3F23948909006363A1 /* Sync */,
@@ -2465,8 +2465,8 @@
24652465
FA09337B23844E9F00C2FD5F /* GraphQLOperationRequest.swift */,
24662466
2142099223721F4400FA140C /* GraphQLOperationType.swift */,
24672467
21558E3D237BB4BF0032A5BB /* GraphQLRequest.swift */,
2468-
2142099323721F4400FA140C /* RESTOperationType.swift */,
24692468
2142099423721F4400FA140C /* RESTOperationRequest.swift */,
2469+
2142099323721F4400FA140C /* RESTOperationType.swift */,
24702470
217855C2237F84D700A30D19 /* RESTRequest.swift */,
24712471
);
24722472
path = Request;
@@ -3830,9 +3830,9 @@
38303830
buildActionMask = 2147483647;
38313831
files = (
38323832
2129BE4423948951006363A1 /* MutationSyncMetadata.swift in Sources */,
3833+
B9675A2E24752621002FC843 /* GraphQLRequest+Model.swift in Sources */,
38333834
2129BE1E2394806B006363A1 /* QueryPredicate+GraphQL.swift in Sources */,
38343835
21420A8F237222A900FA140C /* AWSIAMConfiguration.swift in Sources */,
3835-
2129BE1723948065006363A1 /* GraphQLRequest+Model.swift in Sources */,
38363836
219A888523EB897700BBC5F2 /* GraphQLRequest+AnyModelWithSync.swift in Sources */,
38373837
219A88F123F3379900BBC5F2 /* GraphQLDocumentInput.swift in Sources */,
38383838
21420AA0237222A900FA140C /* AWSAuthorizationType.swift in Sources */,

Amplify/Categories/API/ClientBehavior/APICategoryGraphQLBehavior.swift

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,6 @@
88
/// Behavior of the API category related to GraphQL operations
99
public protocol APICategoryGraphQLBehavior: class {
1010

11-
// MARK: - Model-based GraphQL Operations
12-
13-
/// Perform a GraphQL query for a single `Model` item. This operation will be asychronous, with the callback
14-
/// accessible both locally and via the Hub.
15-
///
16-
/// - Parameters:
17-
/// - modelType: The type for the item returned
18-
/// - id: Unique identifier of the item to retrieve
19-
/// - listener: The event listener for the operation
20-
/// - Returns: The AmplifyOperation being enqueued.
21-
func query<M: Model>(from modelType: M.Type,
22-
byId id: String,
23-
listener: GraphQLOperation<M?>.ResultListener?) -> GraphQLOperation<M?>
24-
25-
/// Performs a GraphQL query for a list of `Model` items which satisfies the `predicate`. This operation will be
26-
/// asychronous, with the callback accessible both locally and via the Hub.
27-
///
28-
/// - Parameters:
29-
/// - modelType: The type for the items returned
30-
/// - predicate: The filter for which items to query
31-
/// - listener: The event listener for the operation
32-
/// - Returns: The AmplifyOperation being enqueued.
33-
func query<M: Model>(from modelType: M.Type,
34-
where predicate: QueryPredicate?,
35-
listener: GraphQLOperation<[M]>.ResultListener?) -> GraphQLOperation<[M]>
36-
37-
/// Performs a GraphQL mutate for the `Model` item. This operation will be asynchronous, with the callback
38-
/// accessible both locally and via the Hub.
39-
///
40-
/// - Parameters:
41-
/// - model: The instance of the `Model`.
42-
/// - type: The type of mutation to apply on the instance of `Model`.
43-
/// - listener: The event listener for the operation
44-
/// - Returns: The AmplifyOperation being enqueued.
45-
func mutate<M: Model>(of model: M,
46-
type: GraphQLMutationType,
47-
listener: GraphQLOperation<M>.ResultListener?) -> GraphQLOperation<M>
48-
49-
/// Performs a GraphQL subscribe operation for `Model` items.
50-
///
51-
/// - Parameters:
52-
/// - modelType: The type of items to be subscribed to
53-
/// - type: The type of subscription for the items
54-
/// - valueListener: Invoked when the GraphQL subscription receives a new value from the service
55-
/// - completionListener: Invoked when the subscription has terminated
56-
/// - Returns: The AmplifyInProcessReportingOperation being enqueued.
57-
func subscribe<M: Model>(from modelType: M.Type,
58-
type: GraphQLSubscriptionType,
59-
valueListener: GraphQLSubscriptionOperation<M>.InProcessListener?,
60-
completionListener: GraphQLSubscriptionOperation<M>.ResultListener?)
61-
-> GraphQLSubscriptionOperation<M>
62-
6311
// MARK: - Request-based GraphQL Operations
6412

6513
/// Perform a GraphQL query operation against a previously configured API. This operation

Amplify/Categories/API/ClientBehavior/AmplifyAPICategory+GraphQLBehavior.swift

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

88
extension AmplifyAPICategory: APICategoryGraphQLBehavior {
99

10-
// MARK: - Model-based GraphQL Operations
11-
12-
public func query<M: Model>(from modelType: M.Type,
13-
byId id: String,
14-
listener: GraphQLOperation<M?>.ResultListener?) -> GraphQLOperation<M?> {
15-
plugin.query(from: modelType, byId: id, listener: listener)
16-
}
17-
18-
public func query<M: Model>(from modelType: M.Type,
19-
where predicate: QueryPredicate?,
20-
listener: GraphQLOperation<[M]>.ResultListener?) -> GraphQLOperation<[M]> {
21-
plugin.query(from: modelType, where: predicate, listener: listener)
22-
}
23-
24-
public func mutate<M: Model>(of model: M,
25-
type: GraphQLMutationType,
26-
listener: GraphQLOperation<M>.ResultListener?) -> GraphQLOperation<M> {
27-
plugin.mutate(of: model, type: type, listener: listener)
28-
}
29-
30-
public func subscribe<M: Model>(from modelType: M.Type,
31-
type: GraphQLSubscriptionType,
32-
valueListener: GraphQLSubscriptionOperation<M>.InProcessListener?,
33-
completionListener: GraphQLSubscriptionOperation<M>.ResultListener?)
34-
-> GraphQLSubscriptionOperation<M> {
35-
plugin.subscribe(from: modelType,
36-
type: type,
37-
valueListener: valueListener,
38-
completionListener: completionListener)
39-
}
40-
4110
// MARK: - Request-based GraphQL operations
4211

4312
public func query<R: Decodable>(request: GraphQLRequest<R>,

AmplifyPlugins/API/APICategoryPlugin.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
21233DC8246F571100039337 /* GraphQLAuthDirectiveIntegrationTests+Auth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21233DC6246F560A00039337 /* GraphQLAuthDirectiveIntegrationTests+Auth.swift */; };
1717
2129BE3E239486D2006363A1 /* AnyModel+JSONInit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE3D239486D2006363A1 /* AnyModel+JSONInit.swift */; };
1818
21409C4F2384BA7E000A53C9 /* APIOperationResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C4E2384BA7E000A53C9 /* APIOperationResponse.swift */; };
19-
21409C5E2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C5D2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift */; };
2019
21409C602384DF17000A53C9 /* RESTOperationRequest+RESTRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C5F2384DF17000A53C9 /* RESTOperationRequest+RESTRequest.swift */; };
2120
21409C7223850BEE000A53C9 /* Todo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C7123850BEE000A53C9 /* Todo.swift */; };
2221
21409C7623850D1D000A53C9 /* BlogPostComment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C7523850D1D000A53C9 /* BlogPostComment.swift */; };
@@ -272,7 +271,6 @@
272271
21233DC6246F560A00039337 /* GraphQLAuthDirectiveIntegrationTests+Auth.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GraphQLAuthDirectiveIntegrationTests+Auth.swift"; sourceTree = "<group>"; };
273272
2129BE3D239486D2006363A1 /* AnyModel+JSONInit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AnyModel+JSONInit.swift"; sourceTree = "<group>"; };
274273
21409C4E2384BA7E000A53C9 /* APIOperationResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIOperationResponse.swift; sourceTree = "<group>"; };
275-
21409C5D2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AWSAPIPlugin+GraphQLModelBehavior.swift"; sourceTree = "<group>"; };
276274
21409C5F2384DF17000A53C9 /* RESTOperationRequest+RESTRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RESTOperationRequest+RESTRequest.swift"; sourceTree = "<group>"; };
277275
21409C6723850A9E000A53C9 /* AWSAPICategoryPluginTestCommon.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AWSAPICategoryPluginTestCommon.framework; sourceTree = BUILT_PRODUCTS_DIR; };
278276
21409C6A23850A9E000A53C9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -735,7 +733,6 @@
735733
21D7A09A237B54D90057D00D /* AWSAPIPlugin.swift */,
736734
21D7A098237B54D90057D00D /* AWSAPIPlugin+Configure.swift */,
737735
21D7A0B1237B54D90057D00D /* AWSAPIPlugin+GraphQLBehavior.swift */,
738-
21409C5D2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift */,
739736
21D7A099237B54D90057D00D /* AWSAPIPlugin+InterceptorBehavior.swift */,
740737
FA8EE784238632620097E4F1 /* AWSAPIPlugin+Log.swift */,
741738
6B33897123AAD94800561E5B /* AWSAPIPlugin+Reachability.swift */,
@@ -2179,7 +2176,6 @@
21792176
21D7A0E6237B54D90057D00D /* AWSAPICategoryPluginConfiguration+EndpointConfig.swift in Sources */,
21802177
21D7A0EB237B54D90057D00D /* AWSAPIPlugin+URLSessionDelegate.swift in Sources */,
21812178
21D7A0DF237B54D90057D00D /* AWSGraphQLOperation.swift in Sources */,
2182-
21409C5E2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift in Sources */,
21832179
6B33897223AAD94800561E5B /* AWSAPIPlugin+Reachability.swift in Sources */,
21842180
FA8EE785238632620097E4F1 /* AWSAPIPlugin+Log.swift in Sources */,
21852181
21D7A10F237B54D90057D00D /* RESTOperationRequestUtils+Validator.swift in Sources */,

AmplifyPlugins/API/AWSAPICategoryPlugin/AWSAPIPlugin+GraphQLModelBehavior.swift

Lines changed: 0 additions & 49 deletions
This file was deleted.

AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/AnyModelIntegrationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AnyModelIntegrationTests: XCTestCase {
5050

5151
let callbackInvoked = expectation(description: "Callback invoked")
5252
var responseFromOperation: GraphQLResponse<AnyModel>?
53-
_ = Amplify.API.mutate(of: anyPost, type: .create) { response in
53+
_ = Amplify.API.mutate(request: .create(anyPost)) { response in
5454
defer {
5555
callbackInvoked.fulfill()
5656
}
@@ -99,7 +99,7 @@ class AnyModelIntegrationTests: XCTestCase {
9999
let originalAnyPost = try originalPost.eraseToAnyModel()
100100

101101
let createCallbackInvoked = expectation(description: "Create callback invoked")
102-
_ = Amplify.API.mutate(of: originalAnyPost, type: .create) { _ in
102+
_ = Amplify.API.mutate(request: .create(originalAnyPost)) { _ in
103103
createCallbackInvoked.fulfill()
104104
}
105105

@@ -113,7 +113,7 @@ class AnyModelIntegrationTests: XCTestCase {
113113

114114
let updateCallbackInvoked = expectation(description: "Update callback invoked")
115115
var responseFromOperation: GraphQLResponse<AnyModel>?
116-
_ = Amplify.API.mutate(of: updatedAnyPost, type: .update) { response in
116+
_ = Amplify.API.mutate(request: .update(updatedAnyPost)) { response in
117117
defer {
118118
updateCallbackInvoked.fulfill()
119119
}
@@ -160,15 +160,15 @@ class AnyModelIntegrationTests: XCTestCase {
160160
let originalAnyPost = try originalPost.eraseToAnyModel()
161161

162162
let createCallbackInvoked = expectation(description: "Create callback invoked")
163-
_ = Amplify.API.mutate(of: originalAnyPost, type: .create) { _ in
163+
_ = Amplify.API.mutate(request: .create(originalAnyPost)) { _ in
164164
createCallbackInvoked.fulfill()
165165
}
166166

167167
wait(for: [createCallbackInvoked], timeout: networkTimeout)
168168

169169
let deleteCallbackInvoked = expectation(description: "Delete callback invoked")
170170
var responseFromOperation: GraphQLResponse<AnyModel>?
171-
_ = Amplify.API.mutate(of: originalAnyPost, type: .delete) { response in
171+
_ = Amplify.API.mutate(request: .delete(originalAnyPost)) { response in
172172
defer {
173173
deleteCallbackInvoked.fulfill()
174174
}

0 commit comments

Comments
 (0)