1

I am working on a project with GraphQL-java and Hibernate with MariaDB. In my current solution, I get 18938 results back. I just want to see the last 10 of these. So I am looking for a solution to limit the number of results.

On the internet I see examples of limiting the number of results (https://graphql.org/learn/pagination/). They call it pagination. However, I cannot find the server implementation of this. Does anyone have experience with this?

I have an Entity class, with some properties : Test.java


@Entity
@Table(name = "test")
public class Test {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NotNull
  @Size(max = 64)
  @Column(nullable = false)
  private String name;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "parent")
  private Test parent;


  public Test() {
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }


  public Test getParent() {
    return parent;
  }

  public void setParent(Test parent) {
    this.parent = parent;
  }


My repository class: TestRepository.java

public interface TestRepository extends CrudRepository<Test, Integer> {} 

My GraphQL resolver class: Query.java

@Component
public class Query implements GraphQLQueryResolver {
  private TestRepository testRepository;

  @Autowired
  public Query(TestRepository testRepository) {
    this.testRepository = testRepository;
  }

  public Iterable<Test> findAllTests(Integer first) {
    return testRepository.findAll();
  }

  public long countTests() {
    return testRepository.count();
  }
}

My GraphQL schema: test.graphqls

type Test {
  id: ID!
  name: String!
  parent: Test
}

#extend query
 type Query {
 findAllTests(first: Int): [Test]!
  countTests: Int!
}

Bregtje Hamer
  • 41
  • 1
  • 2
  • Maybe [this](https://www.howtographql.com/graphql-java/10-pagination/) and [this](https://stackoverflow.com/questions/44574964/graphql-how-to-implement-pagination-with-graphql-java) will help you, but basically you need to add two params `skip` and `limit` that will define your page and then use them in the query. – Stefan Golubović Mar 12 '20 at 12:34
  • Thank you for your response. I also found those article. But I do not know how to use them in the query. They have different methods that I do not have.. – Bregtje Hamer Mar 12 '20 at 12:41
  • Well, I'm guessing you can add method in `TestRepository` with two params `skip` and `limit` and use them in [the query](https://mariadb.com/kb/en/pagination-optimization/) to return a result. – Stefan Golubović Mar 12 '20 at 12:48
  • I am new to this, I only know how to add them in the graphQL resolver. But I have no idea how to use them in the query and modify the query. So that's why I have this question.. – Bregtje Hamer Mar 12 '20 at 14:21
  • You can find some examples [here](https://www.baeldung.com/spring-data-jpa-query). Since you don't have some specific problem, it's going to be hard to get an answer here on SO. What you can do is simply search for solutions for your subproblems, e.g. _how to write a query in spring data_ or _graphql-java pagination_, and apply trial and error method. It's the best way to learn. – Stefan Golubović Mar 12 '20 at 14:56
  • I know how to query. I just don't know how to handle the pagination parameters. if it is something build in, or if there is need to configure it. – Bregtje Hamer Mar 13 '20 at 10:31
  • You can use [`PagingAndSortingRepository` with `Pagable`](https://www.baeldung.com/spring-data-jpa-pagination-sorting) object in Spring, and that way the Spring will do the pagination for you. On the other side, if you decide to go with native query, then you are controlling pagination with [`skip` and `limit`](https://mariadb.com/kb/en/pagination-optimization/). I would suggest trying `PagingAndSortingRepository` first. – Stefan Golubović Mar 13 '20 at 13:35

1 Answers1

2

To summarize my last comment here is what I would do:

  • Instead of extending CrudRepository, extend PagingAndSortingRepository (which is extending CrudRepository)
public interface TestRepository extends PagingAndSortingRepository<Test, Integer> {
}
  • In your Query class pass two args to findAllTests method, page and size that will be used to create the Pageable object
@Component
public class Query implements GraphQLQueryResolver {
  // other properties & methods are omitted for brevity

  public Iterable<Test> findAllTests(Integer page, Integer size) {
    Pageable pageable = PageRequest.of(page, size);
    return testRepository.findAll(pageable).getContent(); // findAll returns Page and we can get the underlying List with getContent
  }
}
  • Add two params from above in your GraphQL schema (I set default page size to be 20)
#extend query
type Query {
  findAllTests(page: Int = 0, size: Int = 20): [Test]!
  countTests: Int!
}

Since I have no experience with GraphQL, I'm not sure if this works, but you can give me feedback if there are some problems.

Stefan Golubović
  • 1,225
  • 1
  • 16
  • 21