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

Create mutation fails with datastore - not adding belongsTo parent id to input #298

Closed
onlybakam opened this issue Jan 22, 2020 · 8 comments
Assignees
Labels
bug Something isn't working datastore Issues related to the DataStore category

Comments

@onlybakam
Copy link

Describe the bug
simple schema:

type Entry @model {
  id: ID!
  comments: [Comment]
    @connection(name: "EntryComments", sortField: "createdAt")
}

type Comment @model {
  id: ID!
  commentEntryId: String!
  entry: Entry @connection(name: "EntryComments", sortField: "createdAt")
}

generates

input CreateCommentInput {
  id: ID
  commentEntryId: String!
  _version: Int
}

create a comment with

let comment = Comment(entry: someExistingEntry)

then do

Amplify.DataStore.save(comment).

the resulting generated input variable does not contain commentEntryId; so the mutation fails

this happens because the entry field is actually nil in the model at this line

https://github.com/aws-amplify/amplify-ios/blob/461c3cfd8923fbbb6eb10b60495b0f4594879cf4/AmplifyPlugins/Core/AWSPluginsCore/Model/Support/Model%2BGraphQL.swift#L39

digging into it, it seems like something is off with the Model.from function here:

https://github.com/aws-amplify/amplify-ios/blob/461c3cfd8923fbbb6eb10b60495b0f4594879cf4/Amplify/Categories/DataStore/Model/Model%2BCodable.swift#L39

that is called by

https://github.com/aws-amplify/amplify-ios/blob/461c3cfd8923fbbb6eb10b60495b0f4594879cf4/Amplify/Categories/DataStore/Model/ModelRegistry.swift#L30

You'll notice that the json string being decoded contains all the necessary data to properly reconstruct the model. Also in Modal+codable, if a breakpoint is added, you can do po try resolvedDecoder.decode(Self.self, from: data) will show a properly constructed Comment Model with an entry field. However, in ModelRegistry, this field is now nil.

Expected behavior
input should be properly created for the mutation to succeed

Environment(please complete the following information):

  • Amplify Framework Version: 0.10
  • Dependency Manager: cocoapods
  • Swift Version : 5.0

Device Information (please complete the following information):

  • Device: iphone 11 pro max simulator
  • iOS Version: ios 13.1
@rodrigoelp
Copy link

I think all of these issues have the same root cause, they haven't implemented in full the specification for @connectionfor belongsTo.

Have a look at these issues I created #265, #277, #254. These are all related with different flavours of the same symptom.

@drochetti drochetti added bug Something isn't working datastore Issues related to the DataStore category labels Jan 28, 2020
@wooj2
Copy link
Contributor

wooj2 commented Feb 8, 2020

Hey onlybakam@, thank you so much for the detailed step through of this issue. Unfortunately, I was unable to get started because I was unable to reproduce the issue.

I'm getting stuck when trying to generate the graphql schema. While generating, I get the following error:

InvalidDirectiveError: sortField "createdAt" not found on type "Comment", other half of connection "EntryComments".

@onlybakam
Copy link
Author

hey. need to add the field:

type Entry @model {
  id: ID!
  comments: [Comment] @connection(name: "EntryComments", sortField: "createdAt")
}

type Comment @model {
  id: ID!
  createdAt: AWSDateTime
  entry: Entry @connection(name: "EntryComments", sortField: "createdAt")
}

@drochetti
Copy link
Contributor

Hi @onlybakam

I investigated this one and it looks like an issue in the GraphQL Transformer in the Amplify CLI (aws-amplify/amplify-cli#3702). I opened an issue in their repo and we will continua to investigate this as we move towards a stable release.

@amuresia
Copy link

Issue was closed there but for me the problem was not addressed ...

@palpatim palpatim assigned royjit and unassigned wooj2 May 29, 2020
@royjit
Copy link
Contributor

royjit commented Jun 4, 2020

Hi @amuresia

I tried to reproduce the issue, please let me know if these are the steps followed?

  1. First I create an entry and save to datastore
  2. Use the entry I created just now to create a comment
 func createEntry(_ sender: Any) {
        let entry = Entry()
        Amplify.DataStore.save(entry) { (result) in
           switch(result) {
           case .success(let savedItem):
               print("Saved entry: \(savedItem.id)")
               self.addComment(entryId: savedItem.id)
           case .failure(let error):
               print("Could not save item to datastore: \(error)")
           }
        }
    }

    func addComment(entryId: String) {
        Amplify.DataStore.query(Entry.self, where: Entry.keys.id.eq(entryId)) { result in
            switch result {
            case .success(let entries):
                guard entries.count == 1, let entry = entries.first else {
                    return
                }
                let comment = Comment(entry: entry)
                Amplify.DataStore.save(comment) { (commentResult) in
                   switch(commentResult) {
                   case .success(let savedItem):
                    print("Saved Comment : \(savedItem.id) \(savedItem.entry?.id)")
                   case .failure(let error):
                       print("Could not save item to datastore: \(error)")
                   }
                }
            case .failure(let error):
                print (error)
            }
        }
    }
  1. Then when I try to retrieve the items by querying the Datastore
        Amplify.DataStore.query(Comment.self) { result in
            switch(result) {
                  case .success(let comments):
                      for comment in comments {
                          print("Comment id: \(comment.id) Entry id \(comment.entry?.id)")

                      }
                  case .failure(let error):
                      print("Could not query: \(error)")
                  }
        }

Everything worked as expected.

@amuresia
Copy link

amuresia commented Jun 4, 2020

Hi @royjit, thank you for looking into it. Yes, I believe this was addressed in 1.0.0 and is now working for model types, but not for non-model types. I'll track a separate issue for that though.

@royjit
Copy link
Contributor

royjit commented Jun 4, 2020

Thank you for verifying. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working datastore Issues related to the DataStore category
Projects
None yet
Development

No branches or pull requests

6 participants