繁体   English   中英

将 GraphQL 响应解析为 JSON 响应

[英]Parse GraphQL response to JSON response

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. 到目前为止一切都是正确的,因为我在 graphql 中得到了有效的响应。 问题是我需要将这个 graphql 响应转换和解析为 JSON 响应,因为我的 api 是 Z0ECD111C1A27DBB8。

如何转换和解析我的 graphql 对 JSON 的响应以返回到我的 API 中?

我使用以下链接发送 graphql 请求: https://www.npmjs.com/package/graphql-request

我有以下编程代码:

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

我认为只使用JSON.stringify应该可以工作:

let responseJSON = JSON.stringify(responseGraphQL);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM