Vertx web 和 graphql

Vertx web and graphql

有几个问题我想澄清一下。

  1. 我们正在部署 Verticle 的多个实例 (20),这将创建 GraphQLHandler 的多个实例。这样可以吗,还是我们应该只使用一个 GraphQLHandler 实例?
// Inside the constructor of the verticle
this.graphQLHandler = GraphQLHandler.create(createGraphQL(), new GraphQLHandlerOptions().setRequestBatchingEnabled(true));
private GraphQL createGraphQL() {
        log.info("**** Loading the graphQL schema ****");
        String schema = vertx.fileSystem().readFileBlocking("tetris.graphqls").toString();
        SchemaParser schemaParser = new SchemaParser();
        TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
        RuntimeWiring runtimeWiring = newRuntimeWiring()
                .wiringFactory(new WiringFactory() {
                    @Override
                    public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environment) {
                        return new VertxPropertyDataFetcher(environment.getFieldDefinition().getName());
                    }
                })
                .scalar(ScalarDataTypes.instantScalar)
                .scalar(ExtendedScalars.Json)
                .type("Query", builder -> {
                    VertxDataFetcher<JsonObject> getArrangementDetailsByLegacyCodeAndEffectiveDate = new VertxDataFetcher<>(this::getArrangementDetailsByLegacyCodeAndEffectiveDate);
                    return builder
                            .dataFetcher("getArrangementDetailsByLegacyCodeAndEffectiveDate", getArrangementDetailsByLegacyCodeAndEffectiveDate);

                })
                .build();
        SchemaGenerator schemaGenerator = new SchemaGenerator();
        GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
        return GraphQL.newGraphQL(graphQLSchema).build();
    }
  1. 我们正在使用 rxjava 获取结果并 returning 承诺。当我们 return 承诺时,执行发生在 rxjava 线程上。我们想把它移到事件循环中。如何实现。
private void getArrangementDetailsByLegacyCodeAndEffectiveDate(DataFetchingEnvironment dataFetchingEnvironment, Promise<JsonObject> promise) {
 Scheduler scheduler = RxHelper.scheduler(vertx.getOrCreateContext());

 Handler<AsyncResult<JsonObject>> resultHandler = handler -> {
                if (handler.succeeded()) {
                    promise.complete(handler.result());
                } else {
                    log.error(new LogEvent("graphql_subscribe_error", traceId).addThrowable(handler.cause()).toString());
                    promise.fail(handler.cause());
                }
            };

 getArrangementsFlowable.
   ...
  .subscribeOn(scheduler)
  .subscribe(SingleHelper.toObserver(resultHandler));

}

日志:

01:04:05.520 [RxCachedThreadScheduler-3] DEBUG graphql.execution.ExecutionStrategy - '70fad529-b19b-426a-8f7c-82a4a3d0b9ea' completing field '/getArrangementDetailsByLegacyCodeAndEffectiveDate'...
01:04:05.520 [RxCachedThreadScheduler-3] DEBUG graphql.execution.ExecutionStrategy - '70fad529-b19b-426a-8f7c-82a4a3d0b9ea' fetching field '/getArrangementDetailsByLegacyCodeAndEffectiveDate/effective_timestamp' using data fetcher 'io.vertx.ext.web.handler.graphql.VertxPropertyDataFetcher'...
01:04:05.520 [RxCachedThreadScheduler-3] DEBUG n.g.execution.ExecutionStrategy - '70fad529-b19b-426a-8f7c-82a4a3d0b9ea' field '/getArrangementDetailsByLegacyCodeAndEffectiveDate/effective_timestamp' fetch returned 'java.lang.String'
01:04:05.520 [RxCachedThreadScheduler-3] DEBUG graphql.execution.ExecutionStrategy - '70fad529-b19b-426a-8f7c-82a4a3d0b9ea' completing field '/getArrangementDetailsByLegacyCodeAndEffectiveDate/effective_timestamp'...
01:04:05.520 [RxCachedThreadScheduler-3] DEBUG graphql.execution.ExecutionStrategy - '70fad529-b19b-426a-8f7c-82a4a3d0b9ea' fetching field '/getArrangementDetailsByLegacyCodeAndEffectiveDate/end_timestamp' using data fetcher 'io.vertx.ext.web.handler.graphql.VertxPropertyDataFetcher'...
01:04:05.520 [RxCachedThreadScheduler-3] DEBUG n.g.execution.ExecutionStrategy - '70fad529-b19b-426a-8f7c-82a4a3d0b9ea' field '/getArrangementDetailsByLegacyCodeAndEffectiveDate/end_timestamp' fetch returned 'java.lang.String'
  1. 可以创建多个GraphQLHandler
  2. 实例
  3. 为了在事件循环中处理项目,请使用 observeOn 运算符,而不是 subscribeOn