Search code examples
iosswiftaws-appsync-ios

aws graphql @connection not returning an array as expected?


I have a graphql model as follows (a user can have many posts 1:m)

type User @model {
  id: ID!
  fullName: String!
  emailAddress: String!
  posts: [Post] @connection(name: "UserPostConnection")
}

type Post @model {
  id: ID!
  text: String!
  user: User @connection(name: "UserPostConnection")
  createdAt: String
  updatedAt: String
}

I am trying to return all the posts for a specific user, as so:

let queryInput = GetUserQuery(id: "879db4f2-1dad-47b9-b70d-8bca15bf2549")

appSyncClient?.fetch(query: queryInput, cachePolicy: .fetchIgnoringCacheData) { (result, error) in
            print(result?.data?.getUser?.posts)
        }
}        

However the result isn't returning an array, it is returning an optional:

Optional(AWSStackSocial.GetUserQuery.Data.GetUser.Post(snapshot: ["__typename": Optional("ModelPostConnection"), "nextToken": nil]))

I have confirmed that a) the user exists, and b) the posts also exists.

On AWS:

query {
    getUser(id: "879db4f2-1dad-47b9-b70d-8bca15bf2549") {
      id 
    posts {
      items {
        id
      }
    }
    }
}

Output:

{
  "data": {
    "getUser": {
      "id": "879db4f2-1dad-47b9-b70d-8bca15bf2549",
      "posts": {
        "items": [
          {
            "id": "04c4b571-6e92-4745-9a40-1a67a9d49d77"
          },
          {
            "id": "0f44fe6e-4e15-4a8c-b591-555e8fa1f60e"
          },
          {
            "id": "60c18c40-fea3-4ff3-8b21-df6857703dd2"
          }
        ]
      }
    }
  }
}

Solution

  • I found the answer. The default depth level for amplify GraphQL is 2, so this needed to be changed to 3. This can be done by changing the maximum depth:

    amplify codegen configure