Java GraphQL的最大结果数
我正在使用GraphQLJava和MariaDB进行Hibernate项目。 在我当前的解决方案中,我得到了18938个结果。我只想看看最后10个。因此,我正在寻找一种解决方案来限制结果的数量 在互联网上,我看到了限制结果数量的例子()。他们称之为分页。但是,我找不到此的服务器实现。有人有这方面的经验吗 我有一个实体类,带有一些属性:Test.javaJava GraphQL的最大结果数,java,hibernate,filter,pagination,graphql,Java,Hibernate,Filter,Pagination,Graphql,我正在使用GraphQLJava和MariaDB进行Hibernate项目。 在我当前的解决方案中,我得到了18938个结果。我只想看看最后10个。因此,我正在寻找一种解决方案来限制结果的数量 在互联网上,我看到了限制结果数量的例子()。他们称之为分页。但是,我找不到此的服务器实现。有人有这方面的经验吗 我有一个实体类,带有一些属性:Test.java @Entity @Table(name = "test") public class Test { @Id @GeneratedVa
@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;
}
public interface TestRepository extends CrudRepository<Test, Integer> {}
@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();
}
}
我的存储库类:TestRepository.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;
}
public interface TestRepository extends CrudRepository<Test, Integer> {}
@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();
}
}
总结我的最后一点意见,我会这样做:
- 扩展
,而不是扩展crudepository
(它正在扩展paging和sortingrepository
)crudepository
公共接口测试存储库扩展了分页和排序存储库{
}
- 在
类中,将两个参数传递给查询
方法、findAllTests
和page
,它们将用于创建size
对象Pageable
@组件
公共类查询实现GraphQLQueryResolver{
//为简洁起见,省略了其他属性和方法
公共Iterable findAllTests(整型页面,整型大小){
Pageable Pageable=PageRequest.of(页面,大小);
return testRepository.findAll(pageable.getContent();//findAll返回页面,我们可以使用getContent获取底层列表
}
}
- 在GraphQL模式中添加上面的两个参数(我将默认页面大小设置为20)
由于我没有使用GraphQL的经验,我不确定这是否有效,但如果有问题,您可以给我反馈。可能会对您有所帮助,但基本上您需要添加两个参数
skip
和limit
,它们将定义您的页面,然后在查询中使用。感谢您的回复。我还找到了那些文章。但我不知道如何在查询中使用它们。他们有不同的方法,我没有。好吧,我猜你可以在TestRepository
中添加带有两个参数skip
和limit
的方法,并使用它们来返回结果。我是新手,我只知道如何在graphQL解析器中添加它们。但是我不知道如何在查询中使用它们并修改查询。这就是为什么我有这个问题,你可以找到一些例子。因为你没有什么特别的问题,所以在这里很难找到答案。您可以做的只是搜索子问题的解决方案,例如,如何在spring数据或graphql java分页中编写查询,并应用试错法。这是最好的学习方法。