graphql - 响应式(Reactive) GraphQL

标签 graphql graphql-java

尝试实现响应式 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 进行测试。

http://localhost:8089/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/

相关文章:

javascript - 了解 AWS AppSync JavaScript SDK 中的 Apollo 客户端缓存和乐观 UI

node.js - 使用 Knex.js 和 PostgreSQL 设置 Docker

spring-boot - spring jar bootRun 导致 GraphQL Schema 错误

java - 如何从 Java 使用 GraphQL?

python - 使用 FastAPI 在基于 Python 的 GraphQL 服务器中进行身份验证验证

graphql - 如何使用 where 子句执行 WpGraphQL 查询?

graphql - 如何在graphQl/Sequelize 中对嵌套查询应用where 子句?

java - Graphql-java 中的自定义标量

java - 如何通过Graphql schema生成java实体

java - GraphQL SPQR 扩展输入对象的突变参数