尝试实现响应式 graphql 并遇到一些问题。
pom 依赖项,
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-kickstart-spring-boot-starter-webflux</artifactId>
<version>7.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.graphql-java-kickstart/graphql-kickstart-spring-boot-starter-tools -->
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-kickstart-spring-boot-starter-tools</artifactId>
<version>7.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.graphql-java-kickstart/graphiql-spring-boot-starter -->
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>7.0.1</version>
</dependency>
架构:-
type Query {
store(storeId: Int!): Store!
stores: [Store!]!
}
type Store {
storeId: Int!
name: String!
address: String!
city: String!
zip: String!
}
解析器:-
@Component
public class StoreQuery implements GraphQLQueryResolver {
@Autowired
private final StoreDao dao;
public Store store(int storeId) {
return dao.findById(storeId);
}
public List<Store> stores() {
return dao.findAll();
}
}
在这里,如果我返回 Flux 或 Mono,则会出现以下错误。
"class": "org.springframework.beans.factory.BeanCreationException",
"msg": "Error creating bean with name 'schemaParser' defined in class path resource [graphql/kickstart/tools/boot/GraphQLJavaToolsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.kickstart.tools.SchemaParser]: Factory method 'schemaParser' threw exception; nested exception is java.lang.ClassCastException: class graphql.kickstart.tools.ParameterizedTypeImpl cannot be cast to class java.lang.Class (graphql.kickstart.tools.ParameterizedTypeImpl is in unnamed module of loader 'app'; java.lang.Class is in module java.base of loader 'bootstrap')",
"class": "java.lang.ClassCastException",
"msg": "class graphql.kickstart.tools.ParameterizedTypeImpl cannot be cast to class java.lang.Class (graphql.kickstart.tools.ParameterizedTypeImpl is in unnamed module of loader 'app'; java.lang.Class is in module java.base of loader 'bootstrap')",
因此,尝试以列表形式返回。
但是使用阻塞调用将 Flux 转换为 List 也会引发错误,
public List<Store> findAll() {
return Flux.from(storeCollection.find()).collectList().block();
}
在这里,出现以下错误。
Exception while fetching data (/stores) : block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-2
使用 graphiql 进行测试。
最佳答案
正如错误所示,您不能在非阻塞 webflux 中使用阻塞操作。
public Flux<Store> findAll() {
return Flux.fromIterable(storeCollection.find());
or
return Flux.fromStream(storeCollection.find());
}
关于graphql - 响应式(Reactive) GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62117765/