过滤嵌套字段,或将类型映射到查询
Filtering nested fields, or mapping types to queries
请问这个可能很基本的问题。我已经使用 GraphQL SPQR 来实现产品 DTO 的获取,如下所示:
@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);
}
请注意,我正在使用此查询对我的数据存储库进行分页。
我的 DTO 是按照我可以查询的方式设置的:
products(count: 10, offset: 0) {
store {
products /* attempting to query using the count and offset parameters here is invalid*/ {
productName
}
}
}
在第二种(商店)下,我又在拉取产品列表。我如何告诉 GraphQL 以与获取第一个产品列表相同的方式获取第二个嵌套的产品列表?
我想象能够将 GraphQLQuery
绑定到我的 ProductEntityDAO
class 或类似的东西,这样针对该类型的提取都可以以相同的方式解析。
感谢您的帮助。
编辑:
谢谢Kaqqao。该解决方案非常有效,我需要解决 "An entity that has products".
的一般情况
为此,我在我的产品实体上定义了一个接口,如下所示:
@GraphQLInterface(name = "hasProducts", implementationAutoDiscovery = true)
public interface ProductEdge {
Collection<ProductEntity> getProducts();
}
然后,我通过在需要执行此操作的实体上实现此接口,使我的实体与产品列表有联系,以通用方式获取它:
public class CommercialPartnerEntity implements ProductEntity.ProductEdge
在我的存储库中:
@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);
允许我在我的服务中做这样的事情:
@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);
}
因此,我以通用方式为任何特定类型定义了我的解析器。很想听听其他人对解决方案的想法。
在实现 "filter by name" 之类的东西时,我想这也会非常有用。
如果我没看错,您正在寻找一种方法来调用外部方法来解析 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
}
}
如果 Store
已经有 getProducts
,您可能想要 @GraphQLIgnore
它,但不是强制性的。
如果您询问如何在 store.products
中将相同的参数传递给 products
,请查看我的回答 here。您可以使用 @GraphQLEnvironment ResolutionEnvironment
注入 ResolutionEnvironment
,如果需要,您可以从那里获得 DataFetchingEnvironment
。
请问这个可能很基本的问题。我已经使用 GraphQL SPQR 来实现产品 DTO 的获取,如下所示:
@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);
}
请注意,我正在使用此查询对我的数据存储库进行分页。
我的 DTO 是按照我可以查询的方式设置的:
products(count: 10, offset: 0) {
store {
products /* attempting to query using the count and offset parameters here is invalid*/ {
productName
}
}
}
在第二种(商店)下,我又在拉取产品列表。我如何告诉 GraphQL 以与获取第一个产品列表相同的方式获取第二个嵌套的产品列表?
我想象能够将 GraphQLQuery
绑定到我的 ProductEntityDAO
class 或类似的东西,这样针对该类型的提取都可以以相同的方式解析。
感谢您的帮助。
编辑:
谢谢Kaqqao。该解决方案非常有效,我需要解决 "An entity that has products".
的一般情况为此,我在我的产品实体上定义了一个接口,如下所示:
@GraphQLInterface(name = "hasProducts", implementationAutoDiscovery = true)
public interface ProductEdge {
Collection<ProductEntity> getProducts();
}
然后,我通过在需要执行此操作的实体上实现此接口,使我的实体与产品列表有联系,以通用方式获取它:
public class CommercialPartnerEntity implements ProductEntity.ProductEdge
在我的存储库中:
@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);
允许我在我的服务中做这样的事情:
@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);
}
因此,我以通用方式为任何特定类型定义了我的解析器。很想听听其他人对解决方案的想法。
在实现 "filter by name" 之类的东西时,我想这也会非常有用。
如果我没看错,您正在寻找一种方法来调用外部方法来解析 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
}
}
如果 Store
已经有 getProducts
,您可能想要 @GraphQLIgnore
它,但不是强制性的。
如果您询问如何在 store.products
中将相同的参数传递给 products
,请查看我的回答 here。您可以使用 @GraphQLEnvironment ResolutionEnvironment
注入 ResolutionEnvironment
,如果需要,您可以从那里获得 DataFetchingEnvironment
。