Graphql 反应图

Graphql 反应图,graphql,graphql-java,Graphql,Graphql Java,尝试实现反应式graphql并遇到一些问题 pom依赖项 <dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphql-kickstart-spring-boot-starter-webflux</artifactId> <version>7.0.1</

尝试实现反应式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();
    }

}
所以,尝试以列表的形式返回

但是使用阻塞调用将流量转换为列表也会引发错误

public List<Store> findAll() {
        return Flux.from(storeCollection.find()).collectList().block();
    }
使用graphiql进行测试


正如错误所示,您不能在非阻塞webflux中使用阻塞操作

public Flux<Store> findAll() {
    return Flux.fromIterable(storeCollection.find());
    or
    return Flux.fromStream(storeCollection.find());
}
公共流量findAll(){
返回Flux.fromIterable(storeCollection.find());
或
返回Flux.fromStream(storeCollection.find());
}

您不能在非阻塞webflux中使用阻塞操作。如果使用kickstart v.7.0.1,则应返回CompletableFuture

public CompletableFuture findAll(){
返回Flux.fromIterable(dao.findAll())
.LIST()
.toFuture();
}
Exception while fetching data (/stores) : block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-2
public Flux<Store> findAll() {
    return Flux.fromIterable(storeCollection.find());
    or
    return Flux.fromStream(storeCollection.find());
}
public CompletableFuture<List<Store>> findAll() {
    return Flux.fromIterable(dao.findAll())
            .collectList()
            .toFuture();
}