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

Nullable connection fields prevent items from syncing to the cloud #558

Closed
anthonymoretti opened this issue May 6, 2021 · 9 comments
Closed
Labels
datastore Issues related to the DataStore Category duplicate This issue or pull request already exists

Comments

@anthonymoretti
Copy link

anthonymoretti commented May 6, 2021

Describe the bug
If nullable connection fields are present then some items save to DataStore but don't sync to the cloud.

To Reproduce
Steps to reproduce the behavior:

  1. Use the following schema:
type Person @model {
  id: ID!
  chairOf: [Board!]
    @connection(keyName: "byChair", fields: ["id"])
  treasurerOf: [Board!]
    @connection(keyName: "byTreasurer", fields: ["id"])
  secretaryOf: [Board!]
    @connection(keyName: "bySecretary", fields: ["id"])
}

type Board @model
  @key(name: "byChair", fields: ["chairId"])
  @key(name: "byTreasurer", fields: ["treasurerId"])
  @key(name: "bySecretary", fields: ["secretaryId"]) {
  id: ID!
  chairId: ID!
  treasurerId: ID
  secretaryId: ID
}
  1. Save a Person, then save a Board with the Person as its chair:
await Amplify.DataStore.save(Person(id: 'Person_1'));
await Amplify.DataStore.save(Board(chairId: 'Person_1'));
  1. Query DataStore and see that the items have saved locally:
print(await Amplify.DataStore.query(Person.classType));
print(await Amplify.DataStore.query(Board.classType));
  1. Check the tables in the AppSync console and see that the Person item saved but the Board item didn't.

Expected behavior
The Board item would also save to the cloud.

Platform
Amplify Flutter current supports iOS and Android. This issue is reproducible in (check all that apply):
[ ] Android
[x] iOS

Output of flutter doctor -v
[✓] Flutter (Channel stable, 2.0.6, on macOS 11.3.1 20E241 darwin-x64, locale en-US)
  • Flutter version 2.0.6 at /Users/anthonymoretti/Development/flutter
  • Framework revision 1d9032c7e1 (6 days ago), 2021-04-29 17:37:58 -0700
  • Engine revision 05e680e202
  • Dart version 2.12.3

[✗] Android toolchain - develop for Android devices
  ✗ Unable to locate Android SDK.
    Install Android Studio from: https://developer.android.com/studio/index.html
    On first launch it will assist you in installing the Android SDK components.
    (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed
    instructions).
    If the Android SDK has been installed to a custom location, please use
    `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS
  • Xcode at /Applications/Xcode.app/Contents/Developer
  • Xcode 12.5, Build version 12E262
  • CocoaPods version 1.10.0

[✓] Chrome - develop for the web
  • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
  • Android Studio not found; download from https://developer.android.com/studio/index.html
    (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed
    instructions).

[✓] VS Code (version 1.56.0)
  • VS Code at /Applications/Visual Studio Code.app/Contents
  • Flutter extension version 3.22.0

[✓] Connected device (2 available)
  • iPhone 12 Pro (mobile) • DE81D025-092A-4050-A338-77967F2BA64C • ios            •
    com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
  • Chrome (web)           • chrome                               • web-javascript • Google Chrome
    90.0.4430.93

! Doctor found issues in 2 categories.

Smartphone (please complete the following information):

  • Device: iOS Simulator
  • OS: iOS 14.5

Additional context

  • Amplify: 4.50.2
@fjnoyp
Copy link
Contributor

fjnoyp commented May 6, 2021

Hey @anthonymoretti at the moment we don't support indexing your data with keys (https://docs.amplify.aws/cli/graphql-transformer/key) so your usage of @key(name: ..., fields: ....) could be leading to unexpected behavior though I'm not too sure why that would be the case ... Perhaps you can share some logs of what's happening? If you save other fields of Board, does it sync to cloud?

We are planning on adding this feature in once the Amplify native Android and iOS libraries which we build upon support this feature! We can keep you informed of our progress on that.

@anthonymoretti
Copy link
Author

Hi @fjnoyp I thought using keyName was the recommended way of doing connections, should I be using named connections instead? If I save other fields of Board it still doesn't sync to cloud, I'll see if I can get some logs.

@anthonymoretti
Copy link
Author

@fjnoyp Does this logging help? I wasn't sure if there were any sensitive fields so I removed several, if you need any that I removed let me know.

{
    "logType": "ResponseMapping",
    "path": [
        "createBoard"
    ],
    "fieldName": "createBoard",
    "resolverArn": "arn:aws:appsync:us-east----/types/Mutation/resolvers/createBoard",
    "requestId": ----,
    "fieldInError": true,
    "errors": [
        "CustomTemplateException(message=One or more parameter values were invalid: Type mismatch for Index Key secretaryId Expected: S Actual: NULL IndexName: bySecretary (Service: DynamoDb, Status Code: 400, Request ID: ----, Extended Request ID: null), errorType=DynamoDB:DynamoDbException, data=null, errorInfo=null)"
    ],
    "parentType": "Mutation",
    "graphQLAPIId": ----
}

@fjnoyp
Copy link
Contributor

fjnoyp commented May 7, 2021

Thanks for sharing the logs @anthonymoretti

This errors section looks interesting:

"CustomTemplateException(
message=
One or more parameter values were invalid: 
Type mismatch for Index Key secretaryId Expected: S Actual: NULL
IndexName: bySecretary (Service: DynamoDb, Status Code: 400, Request ID: ----, Extended Request ID: null),
errorType=DynamoDB:DynamoDbException, data=null, errorInfo=null)"

It seems to be complaining about the index key "secretaryId" being null. Have you tried saving a Board where all keys: byChair, byTreasurer, bySecretary are provided?

@anthonymoretti
Copy link
Author

@fjnoyp Yes I have and it syncs and that's what I'm doing for a workaround at the moment - I create a dummy Person and use it whenever the value should be null, but ideally I'd like to use nullable connections.

@fjnoyp
Copy link
Contributor

fjnoyp commented May 8, 2021

@anthonymoretti right thanks for reiterating that.

Your fields in Person appear to be required (!), could you try removing the ! and regenerating the codegen models?

type Person @model {
  id: ID!
  chairOf: [Board!] <-- remove !
    @connection(keyName: "byChair", fields: ["id"])
  treasurerOf: [Board!] <-- remove !
    @connection(keyName: "byTreasurer", fields: ["id"])
  secretaryOf: [Board!] <-- remove!
    @connection(keyName: "bySecretary", fields: ["id"])
}

@anthonymoretti
Copy link
Author

@fjnoyp I just tried the changes you suggested but the same thing happens. Both Person and Board save locally but only Person syncs to the cloud.

This is the schema I used:

type Person @model {
  id: ID!
  chairOf: [Board]
    @connection(keyName: "byChair", fields: ["id"])
  treasurerOf: [Board]
    @connection(keyName: "byTreasurer", fields: ["id"])
  secretaryOf: [Board]
    @connection(keyName: "bySecretary", fields: ["id"])
}

type Board @model
  @key(name: "byChair", fields: ["chairId"])
  @key(name: "byTreasurer", fields: ["treasurerId"])
  @key(name: "bySecretary", fields: ["secretaryId"]) {
  id: ID!
  chairId: ID!
  treasurerId: ID
  secretaryId: ID
}

@Ashish-Nanda Ashish-Nanda added the datastore Issues related to the DataStore Category label Jul 15, 2021
@b-cancel
Copy link

This is a bug report I just filed that includes a couple of bugs I found and the workarounds to each
after reading your bug report I think it could help
#822

@HuiSF
Copy link
Member

HuiSF commented Aug 28, 2021

This issue should be the same as #306 and the cause is described here.

Closing this issue as a duplicate. Please feel free to follow up if we missed anything else.

@HuiSF HuiSF closed this as completed Aug 28, 2021
@HuiSF HuiSF added the duplicate This issue or pull request already exists label Aug 28, 2021
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 duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

5 participants