首页 > 解决方案 > 如何在 Graphql 解析器中侦听类似 REST 的端点?

问题描述

在成功集成第 3 方 REST Oauth Api 后,现在我正在尝试将 api 逻辑转换为 graphql 查询,但我有点坚持如何最好地构建查询。

从 REST 的角度来看,我有这样的东西可以 100% 工作

  app.get("/auth/redirect", async (req: Request, res:Response) => {
    const code = req.query.code
    const { access_token, account_id } = await getTokens({
      code,
      clientId,
      clientSecret,
    })

    try{
      const result = await axios
      .get(
        `https://api.specificservice.dev/helloworld/id/v1/accounts?accountId=${account_id}`,
        {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            Authorization: `Bearer ${access_token}`,
          },
        }
      )

      console.log('Fetching user..', result.data)
      return result.data
    }
    catch (error: any) {
      let message = error.result.data
      console.log(message)
      return message
    }
  })

来自 Graphql,我有一个这样的草图。

getPlatform: async (_: any, _args: any, { req }: any) => {
  const clientId = "test"
  const clientSecret = "test"
  const code = req.query.code
  const { access_token, account_id } = await getTokens({
    code,
    clientId,
    clientSecret,
  })
  
  try{
    const result = await axios
    .get(
      `https://api.specificservice.dev/helloworld/id/v1/accounts?accountId=${account_id}`,
      {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
          Authorization: `Bearer ${access_token}`,
        },
      }
    )
  
    console.log('Fetching user..', result.data)
    return result.data
  }
  catch (error: any) {
    let message = error.result.data
    console.log(message)
    return message
  }
},

我如何重构它以适应我在其余端点上尝试做的事情

标签: node.jstypescriptapigraphqlapollo-server

解决方案


推荐阅读