Mar*_*tin 4 java spring-boot graphql
我正在使用 GraphQL Spring-boot 库构建 GraphQL API https://github.com/graphql-java/graphql-spring-boot
我有一个架构
type Car {
id: ID!
model: String
brand: String
}
type Query {
allCars: [Car]!
}
Run Code Online (Sandbox Code Playgroud)
我的查询是在我的 Spring Boot 项目中的 Query 类中实现的
@Component
public class Query implements GraphQLQueryResolver {
public List<Machine> allCars() {}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
返回列表时如何使用过滤器和排序:
allCars(first:3){id model}
allCars(filter: {....}){}
Run Code Online (Sandbox Code Playgroud)
我想这是必须在 Java 方法中实现的东西,但我不确定如何在方法中注入过滤器等。
我的猜测是您会创建自己的过滤器输入类型,例如:
type Query {
allCars(filter: CarsFilter!): [Car]
}
input CarsFilter {
color: String!
brand: String!
}
Run Code Online (Sandbox Code Playgroud)
之后,您可以编写 的 Java 实现CarsFilter,例如:
public class CarsFilter {
private String color;
private String brand;
// Getters + Setters
}
Run Code Online (Sandbox Code Playgroud)
现在您可以使用该类编写解析器CarsFilter:
public List<Car> allCars(CarsFilter filter) {
// Filter by using JPA specifications, custom queries, ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8583 次 |
| 最近记录: |