A problem with pagination AWS Amplify API

0

I am writing an application based on Amazon AWS amplify. Users can write notes and read only their notes. I am sending a request to receive all user notes. The server returns to me only 10 notes and nextToken for receiving the next page of the answer. I don't know what to do next with nextToken.

This GraphQL schema:

type Note @model
  @auth(rules: [{allow: owner, ownerField: "user"}]) {
  id: ID!
  user: String
  content: String
  users: User @connection(name: "UserNotes", keyField: "user")
}

type User @model {
  id: ID!

  # Create the connection (which will create the GSI)
  notes: [Note] @connection(name: "UserNotes", keyField: "user")
}

This build GraphQL schema:

type Note {
  id: ID!
  user: String
  content: String
  users: User
}

type User {
  id: ID!
  notes(filter: ModelNoteFilterInput, sortDirection: ModelSortDirection, limit: Int, nextToken: String): ModelNoteConnection
}

enum ModelSortDirection {
  ASC
  DESC
}

type ModelNoteConnection {
  items: [Note]
  nextToken: String
}

input ModelStringFilterInput {
  ne: String
  eq: String
  le: String
  lt: String
  ge: String
  gt: String
  contains: String
  notContains: String
  between: [String]
  beginsWith: String
}

input ModelIDFilterInput {
  ne: ID
  eq: ID
  le: ID
  lt: ID
  ge: ID
  gt: ID
  contains: ID
  notContains: ID
  between: [ID]
  beginsWith: ID
}

input ModelIntFilterInput {
  ne: Int
  eq: Int
  le: Int
  lt: Int
  ge: Int
  gt: Int
  contains: Int
  notContains: Int
  between: [Int]
}

input ModelFloatFilterInput {
  ne: Float
  eq: Float
  le: Float
  lt: Float
  ge: Float
  gt: Float
  contains: Float
  notContains: Float
  between: [Float]
}

input ModelBooleanFilterInput {
  ne: Boolean
  eq: Boolean
}

input ModelNoteFilterInput {
  id: ModelIDFilterInput
  user: ModelStringFilterInput
  content: ModelStringFilterInput
  and: [ModelNoteFilterInput]
  or: [ModelNoteFilterInput]
  not: ModelNoteFilterInput
}

type Query {
  getNote(id: ID!): Note
  listNotes(filter: ModelNoteFilterInput, limit: Int, nextToken: String): ModelNoteConnection
  getUser(id: ID!): User
  listUsers(filter: ModelUserFilterInput, limit: Int, nextToken: String): ModelUserConnection
}

input CreateNoteInput {
  id: ID
  user: String
  content: String
}

input UpdateNoteInput {
  id: ID!
  user: String
  content: String
}

input DeleteNoteInput {
  id: ID
}

type Mutation {
  createNote(input: CreateNoteInput!): Note
  updateNote(input: UpdateNoteInput!): Note
  deleteNote(input: DeleteNoteInput!): Note
  createUser(input: CreateUserInput!): User
  updateUser(input: UpdateUserInput!): User
  deleteUser(input: DeleteUserInput!): User
}

type Subscription {
  onCreateNote: Note @aws_subscribe(mutations: ["createNote"])
  onUpdateNote: Note @aws_subscribe(mutations: ["updateNote"])
  onDeleteNote: Note @aws_subscribe(mutations: ["deleteNote"])
  onCreateUser: User @aws_subscribe(mutations: ["createUser"])
  onUpdateUser: User @aws_subscribe(mutations: ["updateUser"])
  onDeleteUser: User @aws_subscribe(mutations: ["deleteUser"])
}

type ModelUserConnection {
  items: [User]
  nextToken: String
}

input ModelUserFilterInput {
  id: ModelIDFilterInput
  and: [ModelUserFilterInput]
  or: [ModelUserFilterInput]
  not: ModelUserFilterInput
}

input CreateUserInput {
  id: ID
}

input UpdateUserInput {
  id: ID!
}

input DeleteUserInput {
  id: ID
}

This my query request:

public void runQuery(String user) {

        mAWSAppSyncClient.query(GetUserQuery.builder()
                .id(user)
                .build())
                .responseFetcher(AppSyncResponseFetchers.NETWORK_ONLY)
                .enqueue(postCallback);

    }

    private GraphQLCall.Callback<GetUserQuery.Data> postCallback = new GraphQLCall.Callback<GetUserQuery.Data>() {
        @Override
        public void onResponse(@Nonnull Response<GetUserQuery.Data> response) {

            if (response == null || response.data() == null ||
                    response.data().getUser() == null ||
                    response.data().getUser() == null) {
                Log.i("WP", "No data");
                return;
            }

            for (GetUserQuery.Item post : response.data().getUser().notes().items()) {
                Log.i("WP", "Post: " + post.content());

            }

                nextToken = response.data().getUser().notes().nextToken();

            Log.i("WP", "NextToken: " + nextToken);


            if (nextToken != null){
                    
                    //TODO
                }

            // update adapter
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.i("WP", "Happy end");
                }
            });
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            Log.i("WP", "Apollo ERROR: " + e.toString());
        }
    };

Now where do I use the nextToken? I thought I would use this in

mAWSAppSyncClient.query(GetUserQuery.builder()
                              .nextToken(nextToken)

but it's does not accept nextToken and I do not know how I can get all user notes if there are more than 10, for example 100. Please help me, say me how and where to use nextToken and get next page answer from server?

Edited by: waplay on Apr 2, 2019 5:01 AM

waplay
asked 5 years ago883 views
1 Answer
0
waplay
answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions