I have built a JSON api that receives a json from Postman and transforms it into a graphql request because I need to send a request to an external endpoint that only accepts graphql. So far everything is correct because I get a valid response in graphql. The problem is that I need to transform and parse this graphql response to a JSON response because my api is JSON.
How can I transform and parse my graphql response to JSON to return in my API?
I have used the following link to send graphql requests: https://www.npmjs.com/package/graphql-request
I have the following programming code:
const authenticationGraphQLRequest = async (req: Request, res: Response, _next: NextFunction) => {
let apiKey: string = req.body.apiKey;
let password: string = req.body.password;
if (!apiKey || !password) {
return res.sendStatus(400);
}
let mutationGraphQL: string = generateGraphQLRequest(apiKey, password);
console.log("REQUEST IN GRAPHQL===>" + mutationGraphQL);
let responseGraphQL = request(EXTERNAL_ENDPOINT, mutationGraphQL).then((data) => console.log("Response=>" + data))
console.log("Response in GraphQL===>" + responseGraphQL);
let responseJSON : string = "";
return res.status(200).json(responseJSON);
};
I think just using JSON.stringify
should work:
let responseJSON = JSON.stringify(responseGraphQL);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.