Skip to content

Commit

Permalink
refactor(API): move the model-based functions to helpers (#470)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
drochetti authored May 20, 2020
1 parent 0f6445c commit 43e3a70
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 336 deletions.
10 changes: 5 additions & 5 deletions Amplify.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
2125E265231C2D3100B3DEB5 /* awsconfiguration.json in Resources */ = {isa = PBXBuildFile; fileRef = 2125E2102318D73B00B3DEB5 /* awsconfiguration.json */; };
2129BE012394627B006363A1 /* PostCommentModelRegistration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE002394627B006363A1 /* PostCommentModelRegistration.swift */; };
2129BE0F23948005006363A1 /* SingleDirectiveGraphQLDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE0823948005006363A1 /* SingleDirectiveGraphQLDocument.swift */; };
2129BE1723948065006363A1 /* GraphQLRequest+Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1523948065006363A1 /* GraphQLRequest+Model.swift */; };
2129BE1E2394806B006363A1 /* QueryPredicate+GraphQL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1A2394806B006363A1 /* QueryPredicate+GraphQL.swift */; };
2129BE1F2394806B006363A1 /* Model+GraphQL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1B2394806B006363A1 /* Model+GraphQL.swift */; };
2129BE212394806B006363A1 /* ModelSchema+GraphQL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1D2394806B006363A1 /* ModelSchema+GraphQL.swift */; };
Expand Down Expand Up @@ -281,6 +280,7 @@
B9521835237E21BA00F53237 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9521830237E21B900F53237 /* Comment.swift */; };
B9521836237E21BA00F53237 /* Post+Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9521831237E21B900F53237 /* Post+Schema.swift */; };
B9521837237E21BA00F53237 /* Post.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9521832237E21B900F53237 /* Post.swift */; };
B9675A2E24752621002FC843 /* GraphQLRequest+Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE1523948065006363A1 /* GraphQLRequest+Model.swift */; };
B98E9D0E2372236300934B51 /* ModelKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98E9D062372236200934B51 /* ModelKey.swift */; };
B98E9D0F2372236300934B51 /* QueryOperator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98E9D072372236200934B51 /* QueryOperator.swift */; };
B98E9D102372236300934B51 /* QueryPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B98E9D082372236200934B51 /* QueryPredicate.swift */; };
Expand Down Expand Up @@ -2322,10 +2322,10 @@
FA131AAB2360FE070008381C /* AWSPluginsCore */ = {
isa = PBXGroup;
children = (
21C395B4245729F100597EA2 /* API */,
FA131ACB2360FE470008381C /* Auth */,
FA131AAC2360FE070008381C /* AWSPluginsCore.h */,
FA131AAD2360FE070008381C /* Info.plist */,
21C395B4245729F100597EA2 /* API */,
FA131ACB2360FE470008381C /* Auth */,
2129BE0223947FA3006363A1 /* Model */,
6BBECD6F23ADA7C100C8DFBE /* ServiceConfiguration */,
2129BE3F23948909006363A1 /* Sync */,
Expand Down Expand Up @@ -2465,8 +2465,8 @@
FA09337B23844E9F00C2FD5F /* GraphQLOperationRequest.swift */,
2142099223721F4400FA140C /* GraphQLOperationType.swift */,
21558E3D237BB4BF0032A5BB /* GraphQLRequest.swift */,
2142099323721F4400FA140C /* RESTOperationType.swift */,
2142099423721F4400FA140C /* RESTOperationRequest.swift */,
2142099323721F4400FA140C /* RESTOperationType.swift */,
217855C2237F84D700A30D19 /* RESTRequest.swift */,
);
path = Request;
Expand Down Expand Up @@ -3830,9 +3830,9 @@
buildActionMask = 2147483647;
files = (
2129BE4423948951006363A1 /* MutationSyncMetadata.swift in Sources */,
B9675A2E24752621002FC843 /* GraphQLRequest+Model.swift in Sources */,
2129BE1E2394806B006363A1 /* QueryPredicate+GraphQL.swift in Sources */,
21420A8F237222A900FA140C /* AWSIAMConfiguration.swift in Sources */,
2129BE1723948065006363A1 /* GraphQLRequest+Model.swift in Sources */,
219A888523EB897700BBC5F2 /* GraphQLRequest+AnyModelWithSync.swift in Sources */,
219A88F123F3379900BBC5F2 /* GraphQLDocumentInput.swift in Sources */,
21420AA0237222A900FA140C /* AWSAuthorizationType.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,6 @@
/// Behavior of the API category related to GraphQL operations
public protocol APICategoryGraphQLBehavior: class {

// MARK: - Model-based GraphQL Operations

/// Perform a GraphQL query for a single `Model` item. This operation will be asychronous, with the callback
/// accessible both locally and via the Hub.
///
/// - Parameters:
/// - modelType: The type for the item returned
/// - id: Unique identifier of the item to retrieve
/// - listener: The event listener for the operation
/// - Returns: The AmplifyOperation being enqueued.
func query<M: Model>(from modelType: M.Type,
byId id: String,
listener: GraphQLOperation<M?>.ResultListener?) -> GraphQLOperation<M?>

/// Performs a GraphQL query for a list of `Model` items which satisfies the `predicate`. This operation will be
/// asychronous, with the callback accessible both locally and via the Hub.
///
/// - Parameters:
/// - modelType: The type for the items returned
/// - predicate: The filter for which items to query
/// - listener: The event listener for the operation
/// - Returns: The AmplifyOperation being enqueued.
func query<M: Model>(from modelType: M.Type,
where predicate: QueryPredicate?,
listener: GraphQLOperation<[M]>.ResultListener?) -> GraphQLOperation<[M]>

/// Performs a GraphQL mutate for the `Model` item. This operation will be asynchronous, with the callback
/// accessible both locally and via the Hub.
///
/// - Parameters:
/// - model: The instance of the `Model`.
/// - type: The type of mutation to apply on the instance of `Model`.
/// - listener: The event listener for the operation
/// - Returns: The AmplifyOperation being enqueued.
func mutate<M: Model>(of model: M,
type: GraphQLMutationType,
listener: GraphQLOperation<M>.ResultListener?) -> GraphQLOperation<M>

/// Performs a GraphQL subscribe operation for `Model` items.
///
/// - Parameters:
/// - modelType: The type of items to be subscribed to
/// - type: The type of subscription for the items
/// - valueListener: Invoked when the GraphQL subscription receives a new value from the service
/// - completionListener: Invoked when the subscription has terminated
/// - Returns: The AmplifyInProcessReportingOperation being enqueued.
func subscribe<M: Model>(from modelType: M.Type,
type: GraphQLSubscriptionType,
valueListener: GraphQLSubscriptionOperation<M>.InProcessListener?,
completionListener: GraphQLSubscriptionOperation<M>.ResultListener?)
-> GraphQLSubscriptionOperation<M>

// MARK: - Request-based GraphQL Operations

/// Perform a GraphQL query operation against a previously configured API. This operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@

extension AmplifyAPICategory: APICategoryGraphQLBehavior {

// MARK: - Model-based GraphQL Operations

public func query<M: Model>(from modelType: M.Type,
byId id: String,
listener: GraphQLOperation<M?>.ResultListener?) -> GraphQLOperation<M?> {
plugin.query(from: modelType, byId: id, listener: listener)
}

public func query<M: Model>(from modelType: M.Type,
where predicate: QueryPredicate?,
listener: GraphQLOperation<[M]>.ResultListener?) -> GraphQLOperation<[M]> {
plugin.query(from: modelType, where: predicate, listener: listener)
}

public func mutate<M: Model>(of model: M,
type: GraphQLMutationType,
listener: GraphQLOperation<M>.ResultListener?) -> GraphQLOperation<M> {
plugin.mutate(of: model, type: type, listener: listener)
}

public func subscribe<M: Model>(from modelType: M.Type,
type: GraphQLSubscriptionType,
valueListener: GraphQLSubscriptionOperation<M>.InProcessListener?,
completionListener: GraphQLSubscriptionOperation<M>.ResultListener?)
-> GraphQLSubscriptionOperation<M> {
plugin.subscribe(from: modelType,
type: type,
valueListener: valueListener,
completionListener: completionListener)
}

// MARK: - Request-based GraphQL operations

public func query<R: Decodable>(request: GraphQLRequest<R>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
21233DC8246F571100039337 /* GraphQLAuthDirectiveIntegrationTests+Auth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21233DC6246F560A00039337 /* GraphQLAuthDirectiveIntegrationTests+Auth.swift */; };
2129BE3E239486D2006363A1 /* AnyModel+JSONInit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2129BE3D239486D2006363A1 /* AnyModel+JSONInit.swift */; };
21409C4F2384BA7E000A53C9 /* APIOperationResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C4E2384BA7E000A53C9 /* APIOperationResponse.swift */; };
21409C5E2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C5D2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift */; };
21409C602384DF17000A53C9 /* RESTOperationRequest+RESTRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C5F2384DF17000A53C9 /* RESTOperationRequest+RESTRequest.swift */; };
21409C7223850BEE000A53C9 /* Todo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C7123850BEE000A53C9 /* Todo.swift */; };
21409C7623850D1D000A53C9 /* BlogPostComment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21409C7523850D1D000A53C9 /* BlogPostComment.swift */; };
Expand Down Expand Up @@ -272,7 +271,6 @@
21233DC6246F560A00039337 /* GraphQLAuthDirectiveIntegrationTests+Auth.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GraphQLAuthDirectiveIntegrationTests+Auth.swift"; sourceTree = "<group>"; };
2129BE3D239486D2006363A1 /* AnyModel+JSONInit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AnyModel+JSONInit.swift"; sourceTree = "<group>"; };
21409C4E2384BA7E000A53C9 /* APIOperationResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIOperationResponse.swift; sourceTree = "<group>"; };
21409C5D2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AWSAPIPlugin+GraphQLModelBehavior.swift"; sourceTree = "<group>"; };
21409C5F2384DF17000A53C9 /* RESTOperationRequest+RESTRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RESTOperationRequest+RESTRequest.swift"; sourceTree = "<group>"; };
21409C6723850A9E000A53C9 /* AWSAPICategoryPluginTestCommon.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AWSAPICategoryPluginTestCommon.framework; sourceTree = BUILT_PRODUCTS_DIR; };
21409C6A23850A9E000A53C9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -735,7 +733,6 @@
21D7A09A237B54D90057D00D /* AWSAPIPlugin.swift */,
21D7A098237B54D90057D00D /* AWSAPIPlugin+Configure.swift */,
21D7A0B1237B54D90057D00D /* AWSAPIPlugin+GraphQLBehavior.swift */,
21409C5D2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift */,
21D7A099237B54D90057D00D /* AWSAPIPlugin+InterceptorBehavior.swift */,
FA8EE784238632620097E4F1 /* AWSAPIPlugin+Log.swift */,
6B33897123AAD94800561E5B /* AWSAPIPlugin+Reachability.swift */,
Expand Down Expand Up @@ -2179,7 +2176,6 @@
21D7A0E6237B54D90057D00D /* AWSAPICategoryPluginConfiguration+EndpointConfig.swift in Sources */,
21D7A0EB237B54D90057D00D /* AWSAPIPlugin+URLSessionDelegate.swift in Sources */,
21D7A0DF237B54D90057D00D /* AWSGraphQLOperation.swift in Sources */,
21409C5E2384DE2C000A53C9 /* AWSAPIPlugin+GraphQLModelBehavior.swift in Sources */,
6B33897223AAD94800561E5B /* AWSAPIPlugin+Reachability.swift in Sources */,
FA8EE785238632620097E4F1 /* AWSAPIPlugin+Log.swift in Sources */,
21D7A10F237B54D90057D00D /* RESTOperationRequestUtils+Validator.swift in Sources */,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AnyModelIntegrationTests: XCTestCase {

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

let createCallbackInvoked = expectation(description: "Create callback invoked")
_ = Amplify.API.mutate(of: originalAnyPost, type: .create) { _ in
_ = Amplify.API.mutate(request: .create(originalAnyPost)) { _ in
createCallbackInvoked.fulfill()
}

Expand All @@ -113,7 +113,7 @@ class AnyModelIntegrationTests: XCTestCase {

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

let createCallbackInvoked = expectation(description: "Create callback invoked")
_ = Amplify.API.mutate(of: originalAnyPost, type: .create) { _ in
_ = Amplify.API.mutate(request: .create(originalAnyPost)) { _ in
createCallbackInvoked.fulfill()
}

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

let deleteCallbackInvoked = expectation(description: "Delete callback invoked")
var responseFromOperation: GraphQLResponse<AnyModel>?
_ = Amplify.API.mutate(of: originalAnyPost, type: .delete) { response in
_ = Amplify.API.mutate(request: .delete(originalAnyPost)) { response in
defer {
deleteCallbackInvoked.fulfill()
}
Expand Down
Loading

0 comments on commit 43e3a70

Please sign in to comment.