amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Nested queries that are arrays are being returned as single objects in Swift when you call a fetch request

Open cottageGrove opened this issue 7 years ago • 5 comments

I've been trying to get the orders that are linked to the user object I've created through AppSync in my iOS application with the generated methods. The query tab in the AppSync GUI returns the orders that have a relationship with the user, but the generated API.swift file doesn't seem to be able to retrieve nested data or objects.

Here's the Query that I have

query listOrders{
  listUsers(filter: {
      firstname: {
        contains:"baba"
      }
  }) {
    items {
      
      firstname
      lastname
      orders {
        
        items {
          
          orderID
          pickupDate
          dropoffDate
          deliveryTime
          totalCost
        
          laundry {
            baskets
            washType
            detergent
          }
        }
      }
    }
  }
}

And its returning this data which is all fine and dandy

{
  "data": {
    "listUsers": {
      "items": [
        {
          "firstname": "baba",
          "lastname": "ganoush",
          "orders": {
            "items": [
              {
                "orderID": "ABC",
                "pickupDate": "4/10/2019",
                "dropoffDate": "4/22/2019",
                "deliveryTime": "14:00",
                "totalCost": 42,
                "laundry": {
                  "baskets": 4,
                  "washType": "Hot",
                  "detergent": "Not Garbage"
                }
              },
              {
                "orderID": "ABC",
                "pickupDate": "4/10/2019",
                "dropoffDate": "4/22/2019",
                "deliveryTime": "14:00",
                "totalCost": 42,
                "laundry": {
                  "baskets": 4,
                  "washType": "Hot",
                  "detergent": "Not Garbage"
                }
              },
              {
                "orderID": "ABC",
                "pickupDate": "4/10/2019",
                "dropoffDate": "4/22/2019",
                "deliveryTime": "14:00",
                "totalCost": 42,
                "laundry": {
                  "baskets": 4,
                  "washType": "Hot",
                  "detergent": "Not Garbage"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Screen Shot 2019-03-31 at 8 59 10 AM

I've filtered for the user, and have gotten the user's first name and last name however I cannot seem to traverse the next values for orders since it is being returned as a single object rather than array.

The output is showing there is an order object being returned, but I'm not sure what this data means". In particular this

Optional(kleenAmp.ListUsersQuery.Data.ListUser.Item.Order(snapshot: ["nextToken": nil, "__typename": Optional("ModelOrderConnection")]))

Not too sure whether I should just take the data type and decode it with the Codable Protocol, but that has a slew of problems returning the correct data type as well.

There should be some kind of method that allows you to query through all the orders assigned to the user or at least return an array instead of an object.

cottageGrove avatar Mar 31 '19 13:03 cottageGrove

@cottageGrove there is an option for code generation, indicating the depth of the data that is going to be retrieved. Try to run the codegen process again and see what is showing. I will redirect to cli repo, so they will have more knowledge to deal with your problem

Thanks!

elorzafe avatar Apr 01 '19 18:04 elorzafe

I've tried the codegen option, and have set it up with a max depth of 6 however it doesn't generate those methods. I'm currently looking through these commands and not sure which one to use.

amplify add codegen [--apiId ] *what exactly is the api id that it's referring to

$ amplify configure codegen

$ amplify codegen [--max-depth ]

Thanks for redirecting me to the right support team! A little confused is all :\

cottageGrove avatar Apr 01 '19 18:04 cottageGrove

@elorzafe great news! it worked! Used these commands exactly in the cli.

amplify codegen remove amplify add codegen amplify codegen statements amplify codegen --after amplify codegen change the schema so it updates amplify push

cottageGrove avatar Apr 02 '19 00:04 cottageGrove

@elorzafe Xcode wasn't updating the API.swift file or was hanging onto the older API.swift file. Once I opened it up in the finder with Xcode, I saw the orders having an items object and the rest was history! Great job with Amplify btw!

cottageGrove avatar Apr 02 '19 00:04 cottageGrove

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 27 '21 13:05 github-actions[bot]