Skip to content

Commit d31e4cf

Browse files
authored
Remove dependency on ModelRegistry for adding syncable fields to selection set. (#252)
This introduces a flag for whether a GraphQLDocument helper method should contain the sync-able fields. Methods like GraphQLSyncQuery by default will be true, and methods like GraphQLGetQuery can be used with syncEnabled parameter.
1 parent 20872a4 commit d31e4cf

File tree

18 files changed

+122
-77
lines changed

18 files changed

+122
-77
lines changed

Amplify.xcodeproj/project.pbxproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
21D79FDA237617C60057D00D /* SubscriptionEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D79FD9237617C60057D00D /* SubscriptionEvent.swift */; };
8484
21D79FE12377BF4B0057D00D /* AuthProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D79FE02377BF4B0057D00D /* AuthProvider.swift */; };
8585
21D79FE32377F4120057D00D /* SubscriptionConnectionState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D79FE22377F4120057D00D /* SubscriptionConnectionState.swift */; };
86+
21F40A3223A160FC0074678E /* GraphQLDocument+DeleteSyncMutation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F40A3123A160FC0074678E /* GraphQLDocument+DeleteSyncMutation.swift */; };
8687
21FFF988230B7B2C005878EA /* AsychronousOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21FFF986230B7A5D005878EA /* AsychronousOperation.swift */; };
8788
21FFF98E230C81E6005878EA /* StorageAccessLevel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21FFF98D230C81E6005878EA /* StorageAccessLevel.swift */; };
8889
21FFF994230C96CB005878EA /* StorageUploadDataOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21FFF98F230C96CA005878EA /* StorageUploadDataOperation.swift */; };
@@ -582,6 +583,7 @@
582583
21D79FD9237617C60057D00D /* SubscriptionEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionEvent.swift; sourceTree = "<group>"; };
583584
21D79FE02377BF4B0057D00D /* AuthProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthProvider.swift; sourceTree = "<group>"; };
584585
21D79FE22377F4120057D00D /* SubscriptionConnectionState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionConnectionState.swift; sourceTree = "<group>"; };
586+
21F40A3123A160FC0074678E /* GraphQLDocument+DeleteSyncMutation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "GraphQLDocument+DeleteSyncMutation.swift"; sourceTree = "<group>"; };
585587
21FFF986230B7A5D005878EA /* AsychronousOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsychronousOperation.swift; sourceTree = "<group>"; };
586588
21FFF98D230C81E6005878EA /* StorageAccessLevel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageAccessLevel.swift; sourceTree = "<group>"; };
587589
21FFF98F230C96CA005878EA /* StorageUploadDataOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageUploadDataOperation.swift; sourceTree = "<group>"; };
@@ -1161,13 +1163,14 @@
11611163
2129BE0523948005006363A1 /* GraphQLDocument */ = {
11621164
isa = PBXGroup;
11631165
children = (
1164-
2129BE0623948005006363A1 /* GraphQLDocument+Subscription.swift */,
1165-
2129BE0723948005006363A1 /* GraphQLDocument+SyncMutation.swift */,
11661166
2129BE0823948005006363A1 /* GraphQLDocument.swift */,
1167-
2129BE0923948005006363A1 /* GraphQLDocument+SyncQuery.swift */,
1167+
21F40A3123A160FC0074678E /* GraphQLDocument+DeleteSyncMutation.swift */,
11681168
2129BE0A23948005006363A1 /* GraphQLDocument+GetQuery.swift */,
11691169
2129BE0B23948005006363A1 /* GraphQLDocument+ListQuery.swift */,
11701170
2129BE0C23948005006363A1 /* GraphQLDocument+Mutation.swift */,
1171+
2129BE0623948005006363A1 /* GraphQLDocument+Subscription.swift */,
1172+
2129BE0723948005006363A1 /* GraphQLDocument+SyncMutation.swift */,
1173+
2129BE0923948005006363A1 /* GraphQLDocument+SyncQuery.swift */,
11711174
);
11721175
path = GraphQLDocument;
11731176
sourceTree = "<group>";
@@ -3348,6 +3351,7 @@
33483351
21420A98237222A900FA140C /* AuthTokenProvider.swift in Sources */,
33493352
21420A91237222A900FA140C /* AWSAuthorizationConfiguration.swift in Sources */,
33503353
21420A97237222A900FA140C /* IAMCredentialProvider.swift in Sources */,
3354+
21F40A3223A160FC0074678E /* GraphQLDocument+DeleteSyncMutation.swift in Sources */,
33513355
21420A99237222A900FA140C /* APIKeyProvider.swift in Sources */,
33523356
21420A90237222A900FA140C /* APIKeyConfiguration.swift in Sources */,
33533357
2129BE552395CAEF006363A1 /* PaginatedList.swift in Sources */,

AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/GraphQLSyncBasedTests.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ class GraphQLSyncBasedTests: XCTestCase {
4848
let uuid = UUID().uuidString
4949
let testMethodName = String("\(#function)".dropLast(2))
5050
let title = testMethodName + "Title"
51-
guard let post = createPost(id: uuid, title: title) else {
51+
guard let createdPost = createPost(id: uuid, title: title) else {
5252
XCTFail("Failed to create post with version 1")
5353
return
5454
}
55+
5556
let updatedTitle = title + "Updated"
56-
let modifiedPost = Post(id: post.id, title: updatedTitle, content: post.content, createdAt: Date())
57+
58+
let modifiedPost = Post(id: createdPost.model.id,
59+
title: updatedTitle,
60+
content: createdPost.model.content,
61+
createdAt: Date())
5762

5863
let completeInvoked = expectation(description: "request completed")
5964
var responseFromOperation: GraphQLResponse<MutationSync<AnyModel>>?
@@ -102,7 +107,7 @@ class GraphQLSyncBasedTests: XCTestCase {
102107
}
103108

104109
XCTAssertEqual(mutationSync.model["title"] as? String, updatedTitle)
105-
XCTAssertEqual(mutationSync.model["content"] as? String, post.content)
110+
XCTAssertEqual(mutationSync.model["content"] as? String, createdPost.model.content)
106111
XCTAssertEqual(mutationSync.syncMetadata.version, 2)
107112
}
108113

@@ -194,7 +199,7 @@ class GraphQLSyncBasedTests: XCTestCase {
194199
let completedInvoked = expectation(description: "Completed invoked")
195200
let progressInvoked = expectation(description: "Progress invoked")
196201

197-
let document = GraphQLSubscription(of: Post.self, type: .onCreate)
202+
let document = GraphQLSubscription(of: Post.self, type: .onCreate, syncEnabled: true)
198203
let request = GraphQLRequest(document: document.stringValue,
199204
variables: document.variables,
200205
responseType: MutationSync<AnyModel>.self,
@@ -247,23 +252,28 @@ class GraphQLSyncBasedTests: XCTestCase {
247252

248253
// MARK: Helpers
249254

250-
func createPost(id: String, title: String) -> AmplifyTestCommon.Post? {
255+
func createPost(id: String, title: String) -> MutationSync<AmplifyTestCommon.Post>? {
251256
let post = Post(id: id, title: title, content: "content", createdAt: Date())
252257
return createPost(post: post)
253258
}
254259

255-
func createPost(post: AmplifyTestCommon.Post) -> AmplifyTestCommon.Post? {
256-
var result: AmplifyTestCommon.Post?
260+
func createPost(post: AmplifyTestCommon.Post) -> MutationSync<AmplifyTestCommon.Post>? {
261+
var result: MutationSync<AmplifyTestCommon.Post>?
257262
let completeInvoked = expectation(description: "request completed")
258263

259-
_ = Amplify.API.mutate(of: post, type: .create, listener: { event in
264+
let document = GraphQLSyncMutation(of: post, type: .create)
265+
let request = GraphQLRequest(document: document.stringValue,
266+
variables: document.variables,
267+
responseType: MutationSync<AmplifyTestCommon.Post>.self,
268+
decodePath: document.decodePath)
269+
_ = Amplify.API.mutate(request: request, listener: { event in
260270
switch event {
261271
case .completed(let data):
262272
switch data {
263273
case .success(let post):
264274
result = post
265-
default:
266-
XCTFail("Could not get data back")
275+
case .failure(let error):
276+
XCTFail("Failed to create post \(error)")
267277
}
268278
completeInvoked.fulfill()
269279
case .failed(let error):
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
//
77

88
import Amplify
9-
import AWSPluginsCore
109
import Foundation
1110

1211
/// A convenience implementation of `GraphQLDocument` that represents a delete operation, requiring no model data
13-
public class MinimalGraphQLDeleteMutation: GraphQLDocument {
12+
public class GraphQLDeleteSyncMutation: GraphQLDocument {
1413
public let documentType = GraphQLDocumentType.mutation
1514
public let mutationType = GraphQLMutationType.delete
1615

@@ -38,6 +37,10 @@ public class MinimalGraphQLDeleteMutation: GraphQLDocument {
3837
name
3938
}
4039

40+
public var hasSyncableModels: Bool {
41+
true
42+
}
43+
4144
public var stringValue: String {
4245
let mutationName = name.pascalCased()
4346
let inputName = "input"

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument+GetQuery.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public struct GraphQLGetQuery: GraphQLDocument {
1616
public let documentType = GraphQLDocumentType.query
1717
public let modelType: Model.Type
1818
public let id: String
19+
public let syncEnabled: Bool
1920

2021
public init(from modelType: Model.Type,
21-
id: String) {
22+
id: String,
23+
syncEnabled: Bool = false) {
2224
self.modelType = modelType
2325
self.id = id
26+
self.syncEnabled = syncEnabled
2427
}
2528

2629
public var name: String {
@@ -31,6 +34,10 @@ public struct GraphQLGetQuery: GraphQLDocument {
3134
return name
3235
}
3336

37+
public var hasSyncableModels: Bool {
38+
return syncEnabled
39+
}
40+
3441
public var stringValue: String {
3542

3643
let input = "$id: ID!"

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument+ListQuery.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ public class GraphQLListQuery: GraphQLDocument {
1616
public let documentType = GraphQLDocumentType.query
1717
public let modelType: Model.Type
1818
public let predicate: QueryPredicate?
19+
public let limit: Int?
20+
public let nextToken: String?
21+
public let syncEnabled: Bool
1922

2023
public init(from modelType: Model.Type,
21-
predicate: QueryPredicate? = nil) {
24+
predicate: QueryPredicate? = nil,
25+
limit: Int? = nil,
26+
nextToken: String? = nil,
27+
syncEnabled: Bool = false) {
2228
self.modelType = modelType
2329
self.predicate = predicate
30+
self.limit = limit
31+
self.nextToken = nextToken
32+
self.syncEnabled = syncEnabled
2433
}
2534

2635
public var name: String {
@@ -31,6 +40,10 @@ public class GraphQLListQuery: GraphQLDocument {
3140
return name + ".items"
3241
}
3342

43+
public var hasSyncableModels: Bool {
44+
return syncEnabled
45+
}
46+
3447
public var stringValue: String {
3548
let schema = modelType.schema
3649

@@ -65,8 +78,16 @@ public class GraphQLListQuery: GraphQLDocument {
6578
variables.updateValue(predicate.graphQLFilterVariables, forKey: "filter")
6679
}
6780

68-
// TODO: Remove this once we support limit and nextToken passed in from the developer
69-
variables.updateValue(1_000, forKey: "limit")
81+
if let limit = limit {
82+
variables.updateValue(limit, forKey: "limit")
83+
} else {
84+
// TODO: Remove this once we support limit and nextToken passed in from the developer
85+
variables.updateValue(1_000, forKey: "limit")
86+
}
87+
88+
if let nextToken = nextToken {
89+
variables.updateValue(nextToken, forKey: "nextToken")
90+
}
7091

7192
return variables
7293
}

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument+Mutation.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class GraphQLMutation: GraphQLDocument {
2323
public let modelType: Model.Type
2424
public let mutationType: GraphQLMutationType
2525

26-
public init(of model: Model, type mutationType: GraphQLMutationType) {
26+
public init(of model: Model,
27+
type mutationType: GraphQLMutationType) {
2728
self.model = model
2829
self.modelType = ModelRegistry.modelType(from: model.modelName) ?? Swift.type(of: model)
2930
self.mutationType = mutationType
@@ -37,6 +38,10 @@ public class GraphQLMutation: GraphQLDocument {
3738
name
3839
}
3940

41+
public var hasSyncableModels: Bool {
42+
false
43+
}
44+
4045
public var stringValue: String {
4146
let mutationName = name.pascalCased()
4247
let inputName = "input"

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument+Subscription.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public struct GraphQLSubscription: GraphQLDocument {
1616
public let documentType = GraphQLDocumentType.subscription
1717
public let modelType: Model.Type
1818
public let subscriptionType: GraphQLSubscriptionType
19+
public let syncEnabled: Bool
1920

2021
public init(of modelType: Model.Type,
21-
type subscriptionType: GraphQLSubscriptionType) {
22+
type subscriptionType: GraphQLSubscriptionType,
23+
syncEnabled: Bool = false) {
2224
self.modelType = modelType
2325
self.subscriptionType = subscriptionType
26+
self.syncEnabled = syncEnabled
2427
}
2528

2629
public var name: String {
@@ -31,6 +34,10 @@ public struct GraphQLSubscription: GraphQLDocument {
3134
name
3235
}
3336

37+
public var hasSyncableModels: Bool {
38+
return syncEnabled
39+
}
40+
3441
public var stringValue: String {
3542
let subscriptionName = name.pascalCased()
3643

@@ -44,5 +51,4 @@ public struct GraphQLSubscription: GraphQLDocument {
4451

4552
return document
4653
}
47-
4854
}

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument+SyncMutation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class GraphQLSyncMutation: GraphQLMutation {
1717
super.init(of: model, type: mutationType)
1818
}
1919

20+
public override var hasSyncableModels: Bool {
21+
return true
22+
}
23+
2024
public override var variables: [String: Any] {
2125

2226
if mutationType == .delete {

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument+SyncQuery.swift

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import Amplify
99
import Foundation
1010

1111
public class GraphQLSyncQuery: GraphQLListQuery {
12-
public let limit: Int?
13-
public let nextToken: String?
12+
1413
public let lastSync: Int?
1514

1615
public init(from modelType: Model.Type,
1716
predicate: QueryPredicate? = nil,
1817
limit: Int? = nil,
1918
nextToken: String? = nil,
2019
lastSync: Int? = nil) {
21-
self.limit = limit
22-
self.nextToken = nextToken
2320
self.lastSync = lastSync
24-
super.init(from: modelType, predicate: predicate)
21+
super.init(from: modelType,
22+
predicate: predicate,
23+
limit: limit,
24+
nextToken: nextToken)
2525
}
2626

2727
public override var name: String {
@@ -38,6 +38,10 @@ public class GraphQLSyncQuery: GraphQLListQuery {
3838
return name
3939
}
4040

41+
public override var hasSyncableModels: Bool {
42+
true
43+
}
44+
4145
public override var stringValue: String {
4246
let schema = modelType.schema
4347

@@ -70,18 +74,7 @@ public class GraphQLSyncQuery: GraphQLListQuery {
7074
}
7175

7276
public override var variables: [String: Any] {
73-
var variables = [String: Any]()
74-
if let predicate = predicate {
75-
variables.updateValue(predicate.graphQLFilterVariables, forKey: "filter")
76-
}
77-
78-
if let limit = limit {
79-
variables.updateValue(limit, forKey: "limit")
80-
}
81-
82-
if let nextToken = nextToken {
83-
variables.updateValue(nextToken, forKey: "nextToken")
84-
}
77+
var variables = super.variables
8578

8679
if let lastSync = lastSync {
8780
variables.updateValue(lastSync, forKey: "lastSync")

AmplifyPlugins/Core/AWSPluginsCore/Model/GraphQLDocument/GraphQLDocument.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public protocol GraphQLDocument: DataStoreStatement where Variables == [String:
2929
/// The name of the document. This is useful to inspect the response, since it will
3030
/// contain the name of the document as the key to the response value.
3131
var name: String { get }
32+
33+
/// The state of the model's backend provisioning, whether it is provisioned with conflict resolution or not.
34+
var hasSyncableModels: Bool { get }
3235
}
3336

3437
extension GraphQLDocument {
@@ -66,7 +69,7 @@ extension GraphQLDocument {
6669
}
6770
}
6871
fieldSet.append(indent + "__typename")
69-
if ModelRegistry.hasSyncableModels {
72+
if hasSyncableModels {
7073
fieldSet.append(indent + "_version")
7174
fieldSet.append(indent + "_deleted")
7275
fieldSet.append(indent + "_lastChangedAt")

0 commit comments

Comments
 (0)