Skip to content

Commit 166e9b7

Browse files
authored
fix(api): make sure collections are not in the gql input - fixes #828 (#837)
**Notes:** In some scenarios the fields of type `.collection` were not being correctly excluded from the `input` of the `GraphQLRequest`. This change makes sure the fields of type `.collection` are not added to the input variable.
1 parent 8d5e788 commit 166e9b7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

AmplifyPlugins/Core/AWSPluginsCore/Model/Support/Model+GraphQL.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ extension Model {
1919
var input: GraphQLInput = [:]
2020
schema.fields.forEach {
2121
let field = $0.value
22+
23+
// TODO how to handle associations of type "many" (i.e. cascade save)?
24+
// This is not supported right now and might be added as a future feature
25+
if case .collection = field.type {
26+
return
27+
}
28+
2229
let name = field.graphQLName
2330
let fieldValue = self[field.name] ?? nil
2431

@@ -45,10 +52,6 @@ extension Model {
4552
fieldName = targetName ?? fieldName
4653
}
4754
input[fieldName] = (value as? Model)?.id
48-
case .collection:
49-
// TODO how to handle associations of type "many" (i.e. cascade save)?
50-
// This is not supported right now and might be added as a future feature
51-
break
5255
case .embedded, .embeddedCollection:
5356
if let encodable = value as? Encodable {
5457
let jsonEncoder = JSONEncoder(dateEncodingStrategy: ModelDateFormatting.encodingStrategy)

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLDocument/GraphQLUpdateMutationTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class GraphQLUpdateMutationTests: XCTestCase {
6767
}
6868
XCTAssert(input["title"] as? String == post.title)
6969
XCTAssert(input["content"] as? String == post.content)
70+
XCTAssertFalse(input.keys.contains("comments"))
7071
}
7172

7273
/// - Given: a `Model` instance
@@ -119,5 +120,6 @@ class GraphQLUpdateMutationTests: XCTestCase {
119120
XCTAssert(input["title"] as? String == post.title)
120121
XCTAssert(input["content"] as? String == post.content)
121122
XCTAssert(input["_version"] as? Int == 5)
123+
XCTAssertFalse(input.keys.contains("comments"))
122124
}
123125
}

0 commit comments

Comments
 (0)