Skip to content

Conversation

lawmicha
Copy link
Contributor

@lawmicha lawmicha commented Oct 14, 2022

Issue #, if available:

Table of Contents:

  1. feat(datastore): LazyModel Implementation #2174
  2. test(datastore): Lazy loading integration tests #2448 (You are here)
  3. test(api): Lazy loading integration tests #2449

Description of changes:
This PR consists of the integration tests against a live backend using the expected codegenerated models for a variety of schemas being tested.

DataStore.save API will persist the data using the SQL adapter, then query for the data. The queried data as part of the save API call will continue to be eager loaded as the model is needed for syncing to the cloud. If the data is lazy loaded, then there's a problem model only containing the SQL identifier information @@primaryKey column value. We might be able to allow lazy loading on the save data flow with some more changes to the implementation.

DataStore.query API will allow lazy loading of SQL data

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
  • Security oriented best practices and standards are followed (e.g. using input sanitization, principle of least privilege, etc)
  • 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.

@lawmicha lawmicha force-pushed the lawmicha.lazymodel2 branch from d621f0f to 69b5b0b Compare October 17, 2022 09:17
@lawmicha lawmicha force-pushed the lawmicha.lazymodel.codegen branch 6 times, most recently from 79c51a2 to 252a5d4 Compare October 17, 2022 21:39
Comment on lines +138 to +141
if let id = modelValue as? String,
(field.name == ModelIdentifierFormat.Custom.sqlColumnName || // this is the `@@primaryKey` (CPK)
(schema.primaryKey.fields.count == 1 // or there's only one primary key (not composite key)
&& schema.primaryKey.indexOfField(named: field.name) != nil)) { // and this field is the primary key
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bug where lazy List was not being created due to field.primaryKey of the generated code was not returning true for Post4V2. this change can be applied directly on main and cherry picked over to v1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +62 to +63
} else if case .collection = modelField.type { // skip all "has-many"
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bug where collections's are being set to nil in the GraphQL input, so for example, when creating a Post and saving it through DataStore. the sync process will translate the post to the GraphQL input variables containing

{
  "id": "postid123",
   "comments": null
}

The problem is this fails as there's no input type "comments". We should skip has-many relationships like in this code snippet. This can be done in main and v1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lawmicha lawmicha force-pushed the lawmicha.lazymodel.codegen branch from 3109798 to b26516a Compare October 21, 2022 16:53
…n update, removal association on update)

- allow updates from a model with not loaded lazy model. the identifier gets extracted out from the not loaded state and persisted in SQL. eager load on save continues to get the loaded model back for the sync to AppSync
- ModelDecorator on update mutations will add `nil` for the targetName fields because updates may remove associations from models.
- allow lazy model to be decoded as a notLoaded state with empty identifier. this is when there is no data to load in the associated data, and simply is not set yet. for example creating a comment and saving it, without a post attached. this is for optional associations
- update some of the codegenerated types to allow setting a `nil` value to represent the removal use case.
- start adding some many-to-many tests
@harsh62 harsh62 added the datastore Issues related to the DataStore category label Nov 2, 2022
@lawmicha lawmicha closed this Dec 22, 2022
@lawmicha lawmicha deleted the lawmicha.lazymodel.codegen branch January 26, 2023 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

datastore Issues related to the DataStore category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants