Skip to content

Conversation

@618coffee
Copy link
Contributor

@618coffee 618coffee commented Nov 11, 2020

Description of changes:

  1. The PR is to fix this issue: Wrong column name in predicates with connected fields #512
  2. This PR better comes together with a CLI PR by Diego@: fix(amplify-codegen-appsync-model-plugin): add modelName to CodingKeys amplify-cli#5857 which is pending review.

Existing issue
Take this schema as example:

type User @model {
  id: ID!
  fullName: String!
  posts: [Post] @connection(name: "UserPosts")
  createdTimestamp: Int!
}

type Post @model {
  id: ID!
  user: User! @connection(name: "UserPosts")
  createdTimestamp: Int!
}

Failure occurs when trying to call

Amplify.DataStore.query(Post.self, where: Post.keys.user = "1234") { _ in }

Post.keys.user is picking up the stringValue of case user, so that the generated SQL statement is

* \"root\".\"user\" = ?" *

But Post.keys.user has a belongs to association with targetName: "postUserId", the correct SQL statement should be

* \"root\".\"postUserId\" = ?" *

Change
CodingKey should check if there is type .belongsTo association, if yes, return targetName, otherwise return stringValue

var columnName: String {
    guard let modelSchema: ModelSchema = ModelRegistry.modelSchema(from: modelName) else {
        log.warn("Please upgrade to the latest version of Amplify CLI and rerun `amplify codegen models`")
        return stringValue
    }

    switch modelType.schema.field(withName: stringValue)?.association {
    case .belongsTo(_, let targetName):
        return targetName ?? stringValue
    default:
        return stringValue
    }
}

And also the CodeGen Model should contains the computed property that returns modelName so that modelSchema can be retrieved from ModelRegistry. Here is the PR by Diego@: aws-amplify/amplify-cli#5857

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@618coffee 618coffee self-assigned this Nov 11, 2020
@codecov
Copy link

codecov bot commented Nov 11, 2020

Codecov Report

Merging #885 (278a8c1) into main (30b3aa9) will increase coverage by 0.04%.
The diff coverage is 34.78%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #885      +/-   ##
==========================================
+ Coverage   64.89%   64.94%   +0.04%     
==========================================
  Files         862      863       +1     
  Lines       34182    34227      +45     
==========================================
+ Hits        22184    22228      +44     
- Misses      11998    11999       +1     
Flag Coverage Δ
API_plugin_unit_test 62.49% <ø> (ø)
Analytics_plugin_unit_test 72.38% <ø> (ø)
Auth_plugin_unit_test 15.79% <ø> (ø)
DataStore_plugin_unit_test 83.07% <100.00%> (+0.02%) ⬆️
Predictions_plugin_unit_test 42.11% <ø> (ø)
Storage_plugin_unit_test 74.74% <ø> (ø)
build_test_amplify 64.40% <28.08%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...Common/Models/Associations/BookAuthor+Schema.swift 0.00% <0.00%> (ø)
...ommon/Models/Associations/UserProfile+Schema.swift 0.00% <0.00%> (ø)
...yTestCommon/Models/Deprecated/DeprecatedTodo.swift 0.00% <0.00%> (ø)
...plifyTestCommon/Models/NonModel/DynamicModel.swift 0.00% <0.00%> (ø)
...mplifyTestCommon/Models/NonModel/Todo+Schema.swift 0.00% <0.00%> (ø)
...tCommon/Models/OGCScenarioBMGroupPost+Schema.swift 0.00% <0.00%> (ø)
...ifyTestCommon/Models/OGCScenarioBPost+Schema.swift 0.00% <0.00%> (ø)
...yTestCommon/Models/ScenarioATest6Post+Schema.swift 0.00% <0.00%> (ø)
AmplifyTestCommon/Models/User+Schema.swift 0.00% <0.00%> (ø)
...mplifyTestCommon/Models/UserFollowers+Schema.swift 0.00% <0.00%> (ø)
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30b3aa9...278a8c1. Read the comment docs.

@618coffee 618coffee requested a review from drochetti November 11, 2020 17:00
@618coffee 618coffee added this to the 1.5.0 milestone Nov 11, 2020
}

extension ModelKey {
public var modelName: String { "" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the behavior. Why is it empty by default? (i.e. explain the backward compatibility)

/// - fields are wrapped with `items`
/// - check if generated GraphQL variables is valid:
/// - it contains the correct columnName `commentPostId`
func testListGraphQLQueryFromSimpleModelGivenQueryPredicateOnConnectedField() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new test is added here to test GraphQLList on connected field

@618coffee 618coffee requested a review from drochetti November 15, 2020 19:42
Copy link
Contributor

@drochetti drochetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing this! remember not to merge into main until we have more clarify on the CL release with the codegen changes.

@lawmicha lawmicha mentioned this pull request Nov 21, 2020
@618coffee 618coffee marked this pull request as draft November 25, 2020 18:10
@618coffee 618coffee changed the title fix(amplify): fix column missing issue for @connection schema fix(amplify): DataStore query fix column missing issue for @connection schema Dec 3, 2020
@drochetti drochetti modified the milestones: 1.5.0, 1.5.1 Dec 4, 2020
@618coffee 618coffee changed the title fix(amplify): DataStore query fix column missing issue for @connection schema fix(amplify): DataStore query fix column missing issue for @connection hasMany schema Dec 7, 2020
@618coffee 618coffee marked this pull request as ready for review December 8, 2020 19:36
@618coffee 618coffee merged commit 028a707 into main Dec 8, 2020
@618coffee 618coffee deleted the fix/columnmissing branch December 8, 2020 19:37
class CodingKeysTests: XCTestCase {

func testSchemaHasCorrectColumnName() throws {
ModelRegistry.register(modelType: Comment.self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that these tests will have side effects on other tests running in the suite.

After we register these models, how do we reset ModelRegistry? -- seems like we need to add a tearDown() function with calls ModelRegistry.reset()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix, we should add something like:

override func tearDown() {
        guard let api = Amplify.API as? Resettable else {
            XCTFail("API Needs to reset between tests")
            return
        }
        api.reset(onComplete: { })
        XCTAssert(ModelRegistry.models.isEmpty)
    }

It's not ideal that we have to use Amplify.API, but ModelRegistry is internal (and rightfully so). In any case, checking to make sure models.isEmpty before proceeding is a good way to double check that a side effect of calling Amplify.API is actually reseting the ModelRegistry.

let commentQPO: QueryPredicateOperation = Comment.keys.post == "5678"
XCTAssertEqual(commentQPO.field, "commentPostId")
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add test coverage we, should probably add a test for the MutationEvent

    func testInternalModelType() throws {
        //arrange not required
        
        let mutationEvent = MutationEvent.keys.inProcess == true

        XCTAssertEqual(mutationEvent.field, "inProcess")
    }

618coffee added a commit that referenced this pull request Dec 11, 2020
…onnection hasMany schema (#885)" (#963)

This reverts commit 028a707.

Co-authored-by: Guo <48600426+DongQuanRui@users.noreply.github.com>
}
switch modelSchema.field(withName: stringValue)?.association {
case .belongsTo(_, let targetName):
return targetName ?? stringValue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to have a unit tests which tests when targetName is not nil, but we should add a test where targetName is nil, so that it just returns the regular stringValue.

@618coffee 618coffee changed the title fix(amplify): DataStore query fix column missing issue for @connection hasMany schema fix(amplify): DataStore query fix column missing issue for @connection belongsTo schema Dec 14, 2020
@lawmicha
Copy link
Contributor

I believe this is reverted, see #965 for more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants