Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): fix has-one associations #1676

Draft
wants to merge 9 commits into
base: v1
Choose a base branch
from

Conversation

diegocstn
Copy link
Contributor

@diegocstn diegocstn commented Mar 10, 2022

Description of changes:
This PR addresses different issues related to the has-one model relationships in DataStore.

The associated Model in a has-one relationship now is eagerly-loaded as we do for belongs-to (#1107)

Models generated with the new version of the GraphQL transformer requires consumers to pass the value of the foreign key to the initializer therefore resulting in a poor dx. While a codegen change is needed to fully address this issue, this PR introduces the necessary changes to have the foreign key value persisted even when only an instance of the associated model is passed to the initializer (#1623).
This change will then restore the previously working dx:

let team = Team(name: "A-Team")

// a Project has-one `Team` 
let project = Project(name: "SecretSauceProject", team: team)

Amplify.DataStore.save(team) { ... }
Amplify.DataStore.save(project) { ... }

// Querying for a project will resolve also the associated team
let savedProject: Project
savedProject.team // holds an instance of the associated team

Check points: (check or cross out if not relevant)

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
  • Documentation update for the change if required
  • PR title conforms to conventional commit style
  • If breaking change, documentation/changelog update with migration instructions

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

@codecov-commenter
Copy link

codecov-commenter commented Mar 14, 2022

Codecov Report

Merging #1676 (c65ae8f) into main (9c0427b) will increase coverage by 0.07%.
The diff coverage is 96.66%.

@@            Coverage Diff             @@
##             main    #1676      +/-   ##
==========================================
+ Coverage   59.19%   59.27%   +0.07%     
==========================================
  Files         716      716              
  Lines       21786    21808      +22     
==========================================
+ Hits        12897    12926      +29     
+ Misses       8889     8882       -7     
Flag Coverage Δ
API_plugin_unit_test 62.50% <ø> (+0.09%) ⬆️
AWSPluginsCore 71.40% <100.00%> (+0.10%) ⬆️
Amplify 47.05% <94.44%> (+0.18%) ⬆️
Analytics_plugin_unit_test 71.76% <ø> (ø)
Auth_plugin_unit_test 78.74% <ø> (ø)
DataStore_plugin_unit_test 76.31% <100.00%> (+0.06%) ⬆️
Geo_plugin_unit_test 52.12% <ø> (ø)
Predictions_plugin_unit_test 26.81% <ø> (ø)
Storage_plugin_unit_test 58.04% <ø> (ø)

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

Impacted Files Coverage Δ
...Model/Internal/Schema/ModelField+Association.swift 43.92% <94.11%> (+8.05%) ⬆️
.../DataStore/Model/Internal/Schema/ModelSchema.swift 64.70% <100.00%> (+0.70%) ⬆️
...e/AWSPluginsCore/Model/Support/Model+GraphQL.swift 76.63% <100.00%> (+0.48%) ⬆️
...goryPlugin/Storage/SQLite/ModelSchema+SQLite.swift 97.67% <100.00%> (+0.17%) ⬆️
...oryPlugin/Storage/SQLite/SQLStatement+Select.swift 93.61% <100.00%> (+0.35%) ⬆️
...CategoryPlugin/Operation/AWSGraphQLOperation.swift 54.65% <0.00%> (+2.32%) ⬆️
...in/Storage/SQLite/ModelValueConverter+SQLite.swift 70.68% <0.00%> (+3.44%) ⬆️
...s/AWSHubPlugin/Internal/HubChannelDispatcher.swift 94.11% <0.00%> (+5.88%) ⬆️
...e/AWSPluginsCore/Auth/AWSAuthServiceBehavior.swift 87.50% <0.00%> (+12.50%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@@ -58,7 +58,7 @@ class DataStoreConnectionScenario2FlutterTests: SyncEngineFlutterIntegrationTest
return
}
let saveTeamCompleted = expectation(description: "save team completed")
plugin.save(team.model, modelSchema: Team1.schema) { result in
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed as in this scenario we are testing models Team2 and Project2

Comment on lines +19 to +22
ModelRegistry.register(modelType: Project2V2.self)
ModelRegistry.register(modelType: Team2V2.self)
ModelRegistry.register(modelType: Record.self)
ModelRegistry.register(modelType: RecordCover.self)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

those models were missing, so the selection set in queries wasn't properly generated (see GraphQL queries below)

@diegocstn diegocstn marked this pull request as ready for review March 16, 2022 01:35
@diegocstn diegocstn requested a review from a team March 16, 2022 01:35
&& lhs.project1V2TeamId == rhs.project1V2TeamId
// && lhs.team == rhs.team // TODO: Should the Project have the team?

// && lhs.project1V2TeamId == rhs.project1V2TeamId
Copy link
Contributor

Choose a reason for hiding this comment

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

should this check lhs.team == rhs.team?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thanks for catching that

}
}

/// This is a temporary workaround to circumvent a Codegen issue where
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a link to more information to track the removal of this temp workaround? perhaps an issue in our repo to describe the situation

@diegocstn diegocstn marked this pull request as draft March 17, 2022 00:31
@diegocstn diegocstn marked this pull request as draft March 17, 2022 00:31
@fitform-devs
Copy link

Please merge this! People running in iOS land are feeling the pain, there's been lots of activity in the Discord server about this issue.

igorTimofiejczyk added a commit to AnimealProject/animeal_iOS that referenced this pull request Aug 31, 2022
Amplify have known issue which prevent to fill properties with .hasOne connection
Here the issue:
aws-amplify/amplify-swift#1107
And here the prepared fix:
aws-amplify/amplify-swift#1676

Since fix was on github for while without any changes I switched the to the forked version of amplify
igorTimofiejczyk added a commit to AnimealProject/animeal_iOS that referenced this pull request Aug 31, 2022
Amplify have known issue which prevent to fill properties with .hasOne connection
Here the issue:
aws-amplify/amplify-swift#1107
And here the prepared fix:
aws-amplify/amplify-swift#1676

Since fix was on github for while without any changes I switched the to the forked version of amplify
igorTimofiejczyk added a commit to AnimealProject/animeal_iOS that referenced this pull request Aug 31, 2022
Amplify have known issue which prevent to fill properties with .hasOne connection
Here the issue:
aws-amplify/amplify-swift#1107
And here the prepared fix:
aws-amplify/amplify-swift#1676

Since fix was on github for while without any changes I switched the to the forked version of amplify
igorTimofiejczyk added a commit to AnimealProject/animeal_iOS that referenced this pull request Sep 1, 2022
Amplify have known issue which prevent to fill properties with .hasOne connection
Here the issue:
aws-amplify/amplify-swift#1107
And here the prepared fix:
aws-amplify/amplify-swift#1676

Since fix was on github for while without any changes I switched the to the forked version of amplify
@royjit royjit changed the base branch from main to v1 October 17, 2022 17:24
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