1

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"
          }
        ]
      }
    }
  }
}

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

Anything in your model that doesn't have the required suffix (!) is by definition, optional, regardless of whether it exists or not. You have to guard against it being nil because it's defined such that it can be nil. If you own the model, you could change those to be required (I do not suggest this!). Otherwise, you will simply have to unwrap things when you process them.

guard let user = result?.data?.getUser, let posts = user.posts else { throw NoPostsError }

1 Comment

Thank you for replying, even that still doesn't give me the correct result. What I am expecting is a 1:m return of data, like the model results above, 1 user, 3 posts. I was expecting an Items result like you would get if you run a ListPosts for example. something like result?.data?.getUser?.posts?.items. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.