Bregtje Hamer
Bregtje Hamer

Reputation: 41

Max number of results from GraphQL

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!
}

Upvotes: 1

Views: 12940

Answers (1)

Stefan Golubović
Stefan Golubović

Reputation: 1275

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.

Upvotes: 2

Related Questions