如何将 REST API 中的 Java class 中的当前日期传递给 GraphQL API

How to pass current date from Java class in REST API to GraphQL API

我正在尝试从我的 Springboot class 调用 graphql api。 它会返回一个错误,如下所示,我知道这个问题是由于我发送到 graphql api

的日期造成的

<200,{"errors":[{"error_code":"400-900","description":"Invalid Syntax : offending token '2020' at line 1 column 204"}]},[Date:"Sun, 28 Mar 2021 11:19:05 GMT", Content-Type:"application/json;charset=UTF-8", Content-Length:"114", Connection:"keep-alive", Strict-Transport-Security:"max-age=15724800; includeSubDomains", access-control-allow-origin:"*", access-control-allow-methods:"*", access-control-max-age:"3600", access-control-allow-headers:"authorization, content-type, xxxx-token", access-control-expose-headers:"xxxx-token", vary:"Origin,Access-Control-Request-Method,Access-Control-Request-Headers", x-envoy-upstream-service-time:"3", x-envoy-decorator-operation:"<<graphql api project name>>.<<graphql api name>>.svc.cluster.local:8080/*"]>

如果我从 postman 调用同一个 graphQL,它工作正常并且数据也被插入到数据库中。 邮递员正文中给出的输入如下

mutation{ createNewApplication(orgid: "test",applctnnm: "mynewappl",dispnm: "test",applfortxt: "test",crtdbytxt: "test",updtdbytxt: "test",applid: "test",loadts: "2020-06-28T16:48:37.000+0000",crtdts: "2020-06-28T16:48:37.000+0000",updtdts: "2020-06-28T16:48:37.000+0000",extrnlapplctnid: "test",isdeletedflg: true) }

我的springboot中的graphql文件api如下所示

mutation{
    createAggrgtrApplctnByOrgid(orgid: "orgid",applctnnm:  "applctnnm",dispnm: "dispnm",applfortxt: "applfortxt",crtdbytxt: "crtdbytxt",updtdbytxt: "updtdbytxt",applctnid: "applctnid",loadts: "loadts",crtdts: "crtdts",updtdts: "updtdts",extrnlapplctnid: "extrnlapplctnid",isdeletedflg: isdeletedflg)
}

我的Java文件中的代码如下

String gqlTemplate = resourceReader.asString(createAppGql);;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT);
        LocalDateTime dateTime = LocalDateTime.now();
        String formattedDateTime = dateTime.format(formatter);
        return txt.replace("orgid", applicationData.getOrganizationId())
                .replace("applctnnm", applicationData.getApplicationName())
                .replace("dispnm", applicationData.getApplicationDisplayName())
                .replace("applfortxt", "")
                .replace("crtdbytxt",applicationData.getRole())
                .replace("updtdbytxt", applicationData.getRole())
                .replace("loadts", "2020-06-28T16:48:37.000+0000")
                .replace("crtdts", "2020-06-28T16:48:37.000+0000")
                .replace("updtdts", "2020-06-28T16:48:37.000+0000")
                .replace("extrnlapplctnid", applicationData.getExternalApplicationId())
                .replace("isdeletedflg", "false" ); 
HttpEntity<String> entity = new HttpEntity<>(queryTemplate, headers);
ResponseEntity<AppDataResponse> response = restTemplate.exchange(appURL, HttpMethod.POST, entity,
                    AppDataResponse.class);

请求某人帮助我解决这个问题。如何将日期从我的 java class?

传递给 graphQL api

谢谢! 拉姆

您的 txt.replace() 没有替换 txt 字符串的正确部分。

您的 graphql 文本突变是:

mutation{
    createAggrgtrApplctnByOrgid(
        orgid: "orgid",
        applctnnm:  "applctnnm",
        dispnm: "dispnm",
        applfortxt: "applfortxt",
        crtdbytxt: "crtdbytxt",
        updtdbytxt: "updtdbytxt",
        applctnid: "applctnid",
        loadts: "loadts",
        crtdts: "crtdts",
        updtdts: "updtdts",
        extrnlapplctnid: "extrnlapplctnid",
        isdeletedflg: isdeletedflg)
}

在执行 txt.replace("updtdts", "2020-06-28T16:48:37.000+0000") 时,您将所有出现的 updtdts 替换为 2020-06-28T16:48:37.000+0000.

您的突变变为:

mutation{
    createAggrgtrApplctnByOrgid(
        orgid: "orgid",
        applctnnm:  "applctnnm",
        dispnm: "dispnm",
        applfortxt: "applfortxt",
        crtdbytxt: "crtdbytxt",
        updtdbytxt: "updtdbytxt",
        applctnid: "applctnid",
        loadts: "loadts",
        crtdts: "crtdts",
        2020-06-28T16:48:37.000+0000: "2020-06-28T16:48:37.000+0000",
        extrnlapplctnid: "extrnlapplctnid",
        isdeletedflg: isdeletedflg)
}

这是无效的,因为 2020-06-28T16:48:37.000+0000 不是有效的字段名称!您需要避免替换字段名称,而只替换值。

我还建议使用合适的 graphql 客户端库,例如 Apollo Graphql Client 而不是手动更新您的查询。