amplify-swift: GraphQL API can't get the nested details and can't get the proper content when created a custom graphql

Describe the bug

xcode 14.2 amplify 10.8.1

with this schema

type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  username: String!
  posts: [Post] @hasMany(indexName: "byUser", fields: ["id"])
  comments: [Comment] @hasMany(indexName: "byUser", fields: ["id"])
  threads: [Thread] @hasMany(indexName: "byUser", fields: ["id"])
}

type Thread @model @auth(rules: [{allow: public}]) {
  id: ID!
  commentID: ID! @index(name: "byComment")
  content: String!
  userID: ID! @index(name: "byUser")
}

type Comment @model @auth(rules: [{allow: public}]) {
  id: ID!
  content: String!
  postID: ID! @index(name: "byPost", sortKeyFields: ["content"])
  post: Post! @belongsTo(fields: ["postID"])
  threads: [Thread] @hasMany(indexName: "byComment", fields: ["id"])
  userID: ID! @index(name: "byUser")
}

type Post @model @auth(rules: [{allow: public}]) {
  id: ID!
  title: String!
  description: String
  comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"])
  userID: ID! @index(name: "byUser")
}
 

Issue 1

when doing

let request = GraphQLRequest<Post>.list(Post.self)

try await Amplify.API.query(request: request)

i can’t to get the comments details

Post(id: "FC51010E-517E-40F4-AE59-A98317298348", title: "Post 1", description: nil, comments: Optional(Amplify.List<MyFirstApp.Comment>), userID: "c273f24c-7c22-42bc-9f91-102b42646347", createdAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 08:48:25 +0000)), updatedAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 08:48:25 +0000))), MyFirstApp.Post(id: "0ED9558E-A4D8-447E-BF93-1047F196BE66", title: "Post 2", description: Optional("description"), comments: Optional(Amplify.List<MyFirstApp.Comment>), userID: "c273f24c-7c22-42bc-9f91-102b42646347", createdAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 10:25:51 +0000)), updatedAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 10:25:51 +0000)))]

Issue 2

Follow the link https://docs.amplify.aws/lib/graphqlapi/advanced-workflows/q/platform/ios/#nested-data

Create own GraphQLRequest

extension GraphQLRequest {

    static func getPostId(byId id: String) -> GraphQLRequest<Post> {
        let document = """
        query getPost($id: ID!) {
          getPost(id: $id) {
            id
          }
        }
        """
        return GraphQLRequest<Post>(
            document: document,
            variables: ["id": id],
            responseType: Post.self,
            decodePath: "getPost"
        )
    }
}

when doing

try await Amplify.API.query(request: .getPostId(byId: id))

then return error

Failed to create graphql GraphQLResponseError<Post>: Failed to decode GraphQL response to the `ResponseType` Post
Recovery suggestion: Failed to transform to `ResponseType`.
Take a look at the `RawGraphQLResponse` and underlying error to see where it failed to decode.
Caused by:
APIError: keyNotFound key CodingKeys(stringValue: "title", intValue: nil)
Caused by:
keyNotFound(CodingKeys(stringValue: "title", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\").", underlyingError: nil))

When i run at the AWS AppSync Queries Console i can get back the nested detail and created my custom graphql Screenshot 2023-03-09 at 20 51 01

Steps To Reproduce

amplify init
amplify add auth (cognito)
amplify add api (with conflict detection-> auto merge)
amplify add storage
amplify codegen models
amplify push

Expected behavior

Issue 1

return the nested detail(comments details)

Issue 2

no errors

Amplify Framework Version

2.0

Amplify Categories

API

Dependency manager

Swift PM

Swift version

5.0

CLI version

10.8.1

Xcode version

14.2

Relevant log output

For Issue 1

Post(id: "FC51010E-517E-40F4-AE59-A98317298348", title: "Post 1", description: nil, comments: Optional(Amplify.List<MyFirstApp.Comment>), userID: "c273f24c-7c22-42bc-9f91-102b42646347", createdAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 08:48:25 +0000)), updatedAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 08:48:25 +0000))), MyFirstApp.Post(id: "0ED9558E-A4D8-447E-BF93-1047F196BE66", title: "Post 2", description: Optional("description"), comments: Optional(Amplify.List<MyFirstApp.Comment>), userID: "c273f24c-7c22-42bc-9f91-102b42646347", createdAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 10:25:51 +0000)), updatedAt: Optional(Amplify.Temporal.DateTime(foundationDate: 2023-03-09 10:25:51 +0000)))]




For Issue 2

Failed to create graphql GraphQLResponseError<Post>: Failed to decode GraphQL response to the `ResponseType` Post
Recovery suggestion: Failed to transform to `ResponseType`.
Take a look at the `RawGraphQLResponse` and underlying error to see where it failed to decode.
Caused by:
APIError: keyNotFound key CodingKeys(stringValue: "title", intValue: nil)
Caused by:
keyNotFound(CodingKeys(stringValue: "title", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\").", underlyingError: nil))

Is this a regression?

No

Regression additional context

No response

Device

iPhone 14 Pro Max simulator

iOS Version

16.2

Specific to simulators

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

i know what the problem is because if

import Amplify and
import SwiftUI together

public var comments: List<Comment>?

will become error

after i remove the import SwiftUI it can run and get the comments details! Thank you for your support

Looks like the code example you took from the documentation contains Post and Todo, which is not related to the schema provided in the doc ie, Todo is not present in the schema provided.

I tried to verify and found that the code does not work if required variables are not present in the selection set. I will create a new PR against doc repo to fix.