【问题标题】:Error while passing the variable for a graphql query in Spring boot client在 Spring Boot 客户端中为 graphql 查询传递变量时出错
【发布时间】:2022-02-04 06:23:53
【问题描述】:

我正在开发一个项目并尝试在我的 SpringBoot 应用程序中使用 GraphQL 后端服务器。我编写了一个客户端,它连接到 graphQL 后端并创建/获取数据,但是当我尝试从应用程序传递变量并通过查询传递时,它会引发错误。 但是如果我只是注释变量部分并直接在查询中硬编码值,它就可以正常工作。

变异查询- 突变($名称:字符串!){ 创建列表(名称:$names){ 更新 列表 ID 姓名 } }

变量 - { “名称”:“包名” }

Java 客户端 -

'''

public CreateListDto getFavouritesDetails(String shoppingListName, String customerId, String storeId, String langId) throws IOException {

        GraphqlRequestBody graphQLRequestBody = new GraphqlRequestBody();

        System.out.println("print this"+shoppingListName+"custId"+customerId);
        final String query = GraphqlSchemaReaderUtil.getSchemaFromFileName("createList");
        String variables = GraphqlSchemaReaderUtil.getSchemaFromFileName("variables");
        graphQLRequestBody.setVariables(variables.replace("bagName", "Avisss"));
        graphQLRequestBody.setQuery(query);
        //System.out.println("graphQLRequestBody----------"+graphQLRequestBody.getQuery());
        System.out.println(graphQLRequestBody.getVariables());
        CreateListDto webClient = WebClient.builder().defaultHeaders(httpHeaders -> {
            httpHeaders.set("X-User-Id", "XXXXXXXXXXX");
            httpHeaders.set("X-Retail-Id",storeId );
            httpHeaders.set("X-Is-Anonymous", Boolean.toString(true));
        }).build()
                .post()
                .uri(url)
                .bodyValue(graphQLRequestBody)
                .retrieve()
                .bodyToFlux(CreateListDto.class)
                .blockFirst();
        System.out.println(graphQLRequestBody.getQuery());
        //System.out.println(graphQLRequestBody.getVariables());

        return webClient;

'''

Maven 依赖项 --

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

在控制台中出现以下错误并打印请求系统输出 -

GraphQL 请求正文 ::: - 突变{ 创建列表(名称:“sl114”){ 更新 列表 ID 姓名 } } GraphQL 请求变量 ::: - { “名称”:“阿维斯” }

2022-01-23 16:29:40.697 错误 3748 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[.[dispatcherServlet]]:Servlet.service() 用于 servlet [dispatcherServlet] 在上下文中带路径 [/shoppingbag] 抛出异常 [请求处理失败;嵌套异常是 org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from POST https://dev.test.com/graphql] 的根本原因

org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from POST https://dev.test.com/graphql 在 org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:196) ~[spring-webflux-5.3.14.jar:5.3.14] 抑制:reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 在以下站点观察到错误: *__checkpoint ⇢ 400 来自 POST https://dev.test.com/graphql [DefaultWebClient] 原始堆栈跟踪: 在 org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:196) ~[spring-webflux-5.3.14.jar:5.3.14] 在 org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(DefaultClientResponse.java:207) ~[spring-webflux-5.3.14.jar:5.3.14] 在 reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106) ~[reactor-core-3.4.13.jar:3.4.13] 在 reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79) ~[reactor-core-3.4.13.jar:3.4.13] 在 reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:101) ~[reactor-core-3.4.13.jar:3.4.13]>

【问题讨论】:

    标签: java spring-boot maven graphql console.log


    【解决方案1】:

    解决了。有两个问题 - 1) graphQL schema ad 2) WebClient。

    我更正了架构并使用 HttpClient 而不是 WebClient。

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 2019-11-02
      • 2023-01-31
      • 2020-08-31
      • 2021-06-09
      • 2018-09-04
      • 2020-11-30
      • 2020-07-09
      • 1970-01-01
      相关资源
      最近更新 更多