AppSync Events Starter App comments pagination

568 Views Asked by At

I'm following this iOS sample app to integrate AppSync.

I'm trying to paginate comments of an individual event. I don't know how to pass field parameters to comments field of the Event type. Introspection doesn't bring any valuable leads.

This is what I have right now:

let eventQuery = GetEventQuery(id: event.id)
appSyncClient?.fetch(query: eventQuery, cachePolicy: cachePolicy) { result, error in }

This is what I want as imaginary code:

let eventQuery = GetEventQuery(id: event.id, comments: {limit:5})
appSyncClient?.fetch(query: eventQuery, cachePolicy: cachePolicy) { result, error in }

In the AWS console, I can pass parameters easily to the subfield, but I don't know how to do this with autogenerated code. So this question is not about graphQL itself but amplify generated code.

query ListEvents {
  listEvents {
    items {
      id
      name
      comments(limit: 1) {
        items {
          content
        }
        nextToken
      }
    }
  }
}
1

There are 1 best solutions below

0
On

One way that you can accomplish this is by first retrieving the id of the Event (unless you already have the event Id) and use that as part of your filter to retrieve the rest of the Comments using ListCommentsQuery. In your AWS AppSync Console, you should see the list query operation for Comments that take in a filter which you can pass the eventId and a limit which you can set to 5.

It's not obvious what the schema is from the sample, so to start off, you will need a schema that looks like this: (should be similar to the sample).

amplify add api and choose GraphQL, and use this schema:

type Event @model {
  id: ID!
  // additional fields
  comments: [Comment] @connection(name: "EventComments")
}
type Comment @model {
  id: ID!
  // additonal fields
  event: Event! @connection(name: "EventComments")
}

Then provision the backend with amplify push. When prompted to generate the code, enter Yes. This API.swift file and awsconfiguration.json will be used with the AppSync SDK.

If you would like to achieve the same use case, AWS Amplify recently released DataStore that supports lazy loading of the connection so your code would then look like

Amplify.DataStore.query(byId: eventId) { $0 in 
  switch $0 {
    case .success(let event):
      // access event.comments here to load the comments
    case .failure(let error):
      print("error \(error)")
  }
}

If you have additional questions, feel free to open an Issue in their respective repos as those are actively monitored