Skip to content

Instantly share code, notes, and snippets.

@ghenzler
Created January 24, 2023 13:53
Show Gist options
  • Save ghenzler/619bbd29f466276e776b19749eaea410 to your computer and use it in GitHub Desktop.
Save ghenzler/619bbd29f466276e776b19749eaea410 to your computer and use it in GitHub Desktop.
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2021 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.aem.graphql.clienttest;
import com.adobe.aem.graphql.client.AEMHeadlessClient;
import com.adobe.aem.graphql.client.GraphQlQuery;
import com.adobe.aem.graphql.client.GraphQlQuery.QueryCursor;
import com.adobe.aem.graphql.client.GraphQlResponse;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
public class AEMHeadlessClientPaging {
public static class Author {
private String firstName;
private String lastName;
String getFirstName() {
return firstName;
}
void setFirstName(String firstName) {
this.firstName = firstName;
}
String getLastName() {
return lastName;
}
void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Article {
private String path;
private String title;
private Author author;
String getPath() {
return path;
}
@JsonSetter("_path")
void setPath(String path) {
this.path = path;
}
String getTitle() {
return title;
}
void setTitle(String title) {
this.title = title;
}
Author getAuthor() {
return author;
}
@JsonSetter("authorFragment")
void setAuthor(Author author) {
this.author = author;
}
@Override
public String toString() {
return "Article [path=" + path + ", title=" + title + ", author=" + author + "]";
}
}
public static void main(String args[]) throws Exception {
AEMHeadlessClient client = AEMHeadlessClient
.builder()
.endpoint("http://localhost:4502/content/cq:graphql/wknd-shared/endpoint.json")
.basicAuth("admin", "admin")
.build();
GraphQlQuery query = client.queryBuilder()
.contentFragmentModelName("article")
.fields("_path", "title", "authorFragment { firstName lastName }")
.sortBy("title ASC", "_path DESC")
.build();
long pageSize = 3; // to play around
System.out.println(" *** Run query directly without paging ***");
GraphQlResponse respDirect = client.runQuery(query);
for (Article article : respDirect.getItems(Article.class)) {
System.out.println(article);
}
System.out.println(" *** Using Paging with Cursor ***");
QueryCursor cursor = query.getCursor();
while (cursor.hasMore()) {
GraphQlResponse resp = cursor.next(pageSize);
for (Article article : resp.getItems(Article.class)) {
System.out.println(article);
}
}
System.out.println(" *** Using Paging with offset/limit ***");
GraphQlResponse respPagingOffset;
for(long pageNo = 0; (respPagingOffset = client.runQuery(query, null, pageNo * pageSize, pageSize)).hasItems(); pageNo++) {
for (Article article : respPagingOffset.getItems(Article.class)) {
System.out.println(article);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment