Java 筛选嵌套字段,或将类型映射到查询

Java 筛选嵌套字段,或将类型映射到查询,java,spring-boot,graphql,graphql-java,graphql-spqr,Java,Spring Boot,Graphql,Graphql Java,Graphql Spqr,请原谅这个基本问题。我已经使用GraphQL SPQR实现了产品数据的获取,如下所示: @GraphQLQuery(name = "products") public Page<ProductEntity> getProducts(@GraphQLArgument(name = "count", defaultValue = "10") int count, @GraphQLArgument(name = "offset") int offset) { Pageable qu

请原谅这个基本问题。我已经使用GraphQL SPQR实现了产品数据的获取,如下所示:

@GraphQLQuery(name = "products")
public Page<ProductEntity> getProducts(@GraphQLArgument(name = "count", defaultValue = "10") int count, @GraphQLArgument(name = "offset") int offset) {
    Pageable queryPage = PageRequest.of(offset, count);
    return productRepository.findAll(queryPage);
}
在第二种类型(商店)下,我再次获取产品列表。如何让GraphQL以与获取第一个产品列表相同的方式获取第二个嵌套的产品列表

我正在想象将
GraphQLQuery
绑定到我的
ProductEntityDAO
类或类似的东西的能力,这样针对该类型的获取都可以以相同的方式解析

谢谢你的帮助

编辑:

谢谢卡卡。这个解决方案工作得很好,我需要解决“拥有产品的实体”的一般情况

为此,我在我的产品实体上定义了一个界面,如下所示:

@GraphQLInterface(name = "hasProducts", implementationAutoDiscovery = true)
public interface ProductEdge {
    Collection<ProductEntity> getProducts();
}
在我的存储库中:

@Query("select child from CommercialPartnerEntity p inner join p.products child where p = :parent")
@GraphQLQuery(name = "relatedProducts")
Page<ProductEntity> findBy(@Param("parent") ProductEntity.ProductEdge parent, Pageable pageable);
@Query(“从CommercialPartnerEntity p内部连接p.products子项中选择子项,其中p=:父项”)
@GraphQLQuery(name=“relatedProducts”)
pagefindby(@Param(“parent”)ProductEntity.ProductEdge parent,Pageable-Pageable);
在我的服务中,允许我做这样的事情:

@GraphQLQuery(name = "productsList")
public Page<ProductEntity> getProducts(@GraphQLContext ProductEntity.ProductEdge hasProductsEntity, @GraphQLArgument(name = "count", defaultValue = "10") int count, @GraphQLArgument(name = "offset") int offset) {
    Pageable queryPage = PageRequest.of(offset, count);
    return productRepository.findBy(hasProductsEntity, queryPage);
}
@GraphQLQuery(name=“productsList”)
公共页面getProducts(@GraphQLContext ProductEntity.ProductEdge hasProductSenty,@GraphQLArgument(name=“count”,defaultValue=“10”)int count,@GraphQLArgument(name=“offset”)int offset){
Pageable queryPage=PageRequest.of(偏移量、计数);
返回productRepository.findBy(hasProductSenty,queryPage);
}
因此,我以一种通用的方式为任何特定类型定义了解析器。我很想听听其他人对解决方案的想法


在实现“按名称筛选”之类的功能时,我想这也会非常有用。

如果我没弄错的话,您正在寻找一种调用外部方法来解析
store.products
。如果是这种情况,那么使用
@GraphQLContext
很容易实现

例如,您可以执行以下操作:

//Any class with queries
public class ProductService {

    @GraphQLQuery(name = "products")
    public Page<ProductEntity> getProducts(@GraphQLContext Store store, @GraphQLArgument(name = "count", defaultValue = "10") int count, @GraphQLArgument(name = "offset") int offset) {
        //your logic here, maybe delegating to the existing getProducts method
    }
}
//任何带有查询的类
公共类产品服务{
@GraphQLQuery(name=“products”)
公共页面getProducts(@GraphQLContext Store Store、@GraphQLArgument(name=“count”、defaultValue=“10”)int count、@GraphQLArgument(name=“offset”)int offset){
//您的逻辑在这里,可能授权给现有的getProducts方法
}
}
如果
Store
已经有
getProducts
,您可能希望
@GraphQLIgnore
它,但不是必须的


如果您询问如何在
store.products
内将相同的参数传递给
products
,请检查我的答案。您可以使用
@GraphQLEnvironment ResolutionEnvironment
注入
ResolutionEnvironment
,如果需要,您可以从那里获得
DataFetchingEnvironment

经过一些调查,似乎我应该扩展Spring数据存储库的功能。如果成功,将更新问题。谢谢。我对你的答案也有一点了解,请参阅更新的问题。
@GraphQLQuery(name = "productsList")
public Page<ProductEntity> getProducts(@GraphQLContext ProductEntity.ProductEdge hasProductsEntity, @GraphQLArgument(name = "count", defaultValue = "10") int count, @GraphQLArgument(name = "offset") int offset) {
    Pageable queryPage = PageRequest.of(offset, count);
    return productRepository.findBy(hasProductsEntity, queryPage);
}
//Any class with queries
public class ProductService {

    @GraphQLQuery(name = "products")
    public Page<ProductEntity> getProducts(@GraphQLContext Store store, @GraphQLArgument(name = "count", defaultValue = "10") int count, @GraphQLArgument(name = "offset") int offset) {
        //your logic here, maybe delegating to the existing getProducts method
    }
}