Ram
Ram

Reputation: 367

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

I am trying to call a graphql api from my Springboot class. It throws back an error as given here below and I understand that this issue is due to the date that I am sending to the 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/*"]>

If I call the same graphQL from postman, it works fine and the data is inserted in the database as well. Input given in the postman body is as here below

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) }

The graphql file in my springboot api is given as here below

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)
}

The code in my Java file is as here below

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);

Request someone to help me around with this issue. How to pass the date to a graphQL api from my java class?

Thanks! Ram

Upvotes: 0

Views: 553

Answers (1)

AllirionX
AllirionX

Reputation: 1143

Your txt.replace() are not replacing the right part of the txt string.

Your graphql text mutation is:

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)
}

When doing txt.replace("updtdts", "2020-06-28T16:48:37.000+0000"), you replace all occurences of updtdts by 2020-06-28T16:48:37.000+0000.

Your mutation becomes:

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)
}

This is invalid as 2020-06-28T16:48:37.000+0000 is not a valid field name!You need to avoid replacing the field name and only replace the value.

I would also recommend to use a proper graphql client library such as Apollo Graphql Client instead of manually updating your queries.

Upvotes: 1

Related Questions